2
######################################################################
3
# DESCRIPTION: Makefile commands for all verilated target files
5
# Copyright 2003-2024 by Wilson Snyder. This program is free software; you
6
# can redistribute it and/or modify it under the terms of either the GNU
7
# Lesser General Public License Version 3 or the Perl Artistic License
9
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
10
######################################################################
13
# These are computed at configuration time, and most are not ?=
14
# to avoid picking up potentially incorrect Make implicit variables
22
# Configuration time options
23
CFG_WITH_CCWARN = @CFG_WITH_CCWARN@
24
CFG_WITH_LONGTESTS = @CFG_WITH_LONGTESTS@
26
# Compiler flags to enable profiling
27
CFG_CXXFLAGS_PROFILE = @CFG_CXXFLAGS_PROFILE@
28
# Select language required to compile (often empty)
29
CFG_CXXFLAGS_STD = @CFG_CXXFLAGS_STD@
30
# Select newest language (unused by this Makefile, for some test's Makefiles)
31
CFG_CXXFLAGS_STD_NEWEST = @CFG_CXXFLAGS_STD_NEWEST@
32
# Compiler flags to use to turn off unused and generated code warnings, such as -Wno-div-by-zero
33
CFG_CXXFLAGS_NO_UNUSED = @CFG_CXXFLAGS_NO_UNUSED@
34
# Compiler flags that turn on extra warnings
35
CFG_CXXFLAGS_WEXTRA = @CFG_CXXFLAGS_WEXTRA@
36
# Compiler flags that enable coroutine support
37
CFG_CXXFLAGS_COROUTINES = @CFG_CXXFLAGS_COROUTINES@
38
# Compiler flags when creating a precompiled header
39
CFG_CXXFLAGS_PCH = -x c++-header
40
# Compiler option to put in front of filename to read precompiled header
41
CFG_CXXFLAGS_PCH_I = @CFG_CXXFLAGS_PCH_I@
42
# Compiler's filename prefix for precompiled headers, .gch if clang, empty if GCC
43
CFG_GCH_IF_CLANG = @CFG_GCH_IF_CLANG@
45
CFG_LDFLAGS_VERILATED = @CFG_LDFLAGS_VERILATED@
46
# Linker libraries for multithreading
47
CFG_LDLIBS_THREADS = @CFG_LDLIBS_THREADS@
49
######################################################################
52
VERILATOR_COVERAGE = $(PERL) $(VERILATOR_ROOT)/bin/verilator_coverage
53
VERILATOR_INCLUDER = $(PYTHON3) $(VERILATOR_ROOT)/bin/verilator_includer
54
VERILATOR_CCACHE_REPORT = $(PYTHON3) $(VERILATOR_ROOT)/bin/verilator_ccache_report
56
######################################################################
57
# CCACHE flags (via environment as no command line option available)
59
CCACHE_SLOPPINESS ?= pch_defines,time_macros
60
export CCACHE_SLOPPINESS
62
######################################################################
65
ifneq ($(words $(CURDIR)),1)
66
$(error Unsupported: GNU Make cannot build in directories containing spaces, build elsewhere: '$(CURDIR)')
69
######################################################################
71
UNAME_S := $(shell uname -s)
73
######################################################################
76
# Add -MMD -MP if you're using a recent version of GCC.
77
VK_CPPFLAGS_ALWAYS += \
79
-I$(VERILATOR_ROOT)/include \
80
-I$(VERILATOR_ROOT)/include/vltstd \
81
-DVM_COVERAGE=$(VM_COVERAGE) \
83
-DVM_TRACE=$(VM_TRACE) \
84
-DVM_TRACE_FST=$(VM_TRACE_FST) \
85
-DVM_TRACE_VCD=$(VM_TRACE_VCD) \
86
$(CFG_CXXFLAGS_NO_UNUSED) \
88
ifeq ($(CFG_WITH_CCWARN),yes) # Local... Else don't burden users
89
VK_CPPFLAGS_WALL += -Wall $(CFG_CXXFLAGS_WEXTRA) -Werror
92
CPPFLAGS += -I. $(VK_CPPFLAGS_WALL) $(VK_CPPFLAGS_ALWAYS)
95
VPATH += $(VERILATOR_ROOT)/include
96
VPATH += $(VERILATOR_ROOT)/include/vltstd
98
LDFLAGS += $(CFG_LDFLAGS_VERILATED)
100
#OPT = -ggdb -DPRINTINITSTR -DDETECTCHANGE
101
#OPT = -ggdb -DPRINTINITSTR
104
# On macOS, specify all weak symbols as dynamic_lookup.
105
# Otherwise, you get undefined symbol errors.
106
ifeq ($(UNAME_S),Darwin)
107
LDFLAGS += -Wl,-U,__Z15vl_time_stamp64v,-U,__Z13sc_time_stampv
110
# Allow upper level user makefiles to specify flags they want.
111
# These aren't ever set by Verilator, so users are free to override them.
112
CPPFLAGS += $(USER_CPPFLAGS)
113
LDFLAGS += $(USER_LDFLAGS)
114
LDLIBS += $(USER_LDLIBS)
116
# Add flags from -CFLAGS and -LDFLAGS on Verilator command line
117
CPPFLAGS += $(VM_USER_CFLAGS)
118
LDFLAGS += $(VM_USER_LDFLAGS)
119
LDLIBS += $(VM_USER_LDLIBS)
121
######################################################################
122
# Optimization control.
124
# See also the BENCHMARKING & OPTIMIZATION section of the manual.
126
# Optimization flags for non performance-critical/rarely executed code.
127
# No optimization by default, which improves compilation speed.
129
# Optimization for performance critical/hot code. Most time is spent in these
130
# routines. Optimizing by default for improved execution speed.
132
# Optimization applied to the common run-time library used by verilated models.
133
# For compatibility this is called OPT_GLOBAL even though it only applies to
134
# files in the run-time library. Normally there should be no need for the user
135
# to change this as the library is small, but can have significant speed impact.
138
#######################################################################
142
CPPFLAGS += $(CFG_CXXFLAGS_PROFILE)
143
LDFLAGS += $(CFG_CXXFLAGS_PROFILE)
146
#######################################################################
150
CPPFLAGS += $(SYSTEMC_CXX_FLAGS) $(addprefix -I, $(SYSTEMC_INCLUDE))
151
LDFLAGS += $(SYSTEMC_CXX_FLAGS) $(addprefix -L, $(SYSTEMC_LIBDIR))
153
ifneq ($(wildcard $(SYSTEMC_LIBDIR)/*numeric_bit*),)
155
SC_LIBS += -lnumeric_bit -lqt
159
#######################################################################
162
CPPFLAGS += $(CFG_CXXFLAGS_STD)
163
LDLIBS += $(CFG_LDLIBS_THREADS)
165
ifneq ($(VM_TIMING),0)
166
ifneq ($(VM_TIMING),)
167
CPPFLAGS += $(CFG_CXXFLAGS_COROUTINES)
172
#######################################################################
175
VM_FAST += $(VM_CLASSES_FAST) $(VM_SUPPORT_FAST)
176
VM_SLOW += $(VM_CLASSES_SLOW) $(VM_SUPPORT_SLOW)
178
# Precompiled header filename
179
VK_PCH_H = $(VM_PREFIX)__pch.h
180
# Compiler read-a-precompiled-header option for precompiled header filename
181
VK_PCH_I_FAST = $(CFG_CXXFLAGS_PCH_I) $(VM_PREFIX)__pch.h.fast$(CFG_GCH_IF_CLANG)
182
VK_PCH_I_SLOW = $(CFG_CXXFLAGS_PCH_I) $(VM_PREFIX)__pch.h.slow$(CFG_GCH_IF_CLANG)
184
#######################################################################
185
### Overall Objects Linking
187
VK_OBJS_FAST = $(addsuffix .o, $(VM_FAST))
188
VK_OBJS_SLOW = $(addsuffix .o, $(VM_SLOW))
190
VK_USER_OBJS = $(addsuffix .o, $(VM_USER_CLASSES))
192
# Note VM_GLOBAL_FAST and VM_GLOBAL_SLOW holds the files required from the
193
# run-time library. In practice everything is actually in VM_GLOBAL_FAST,
194
# but keeping the distinction for compatibility for now.
195
VK_GLOBAL_OBJS = $(addsuffix .o, $(VM_GLOBAL_FAST) $(VM_GLOBAL_SLOW))
197
# Need to re-build if the generated makefile changes, as compiler options might
199
$(VK_GLOBAL_OBJS): $(VM_PREFIX).mk
201
ifneq ($(VM_PARALLEL_BUILDS),1)
202
# Fast build for small designs: All .cpp files in one fell swoop. This
203
# saves total compute, but can be slower if only a little changes. It is
204
# also a lot slower for medium to large designs when the speed of the C
205
# compiler dominates, which in this mode is not parallelizable.
207
VK_OBJS += $(VM_PREFIX)__ALL.o
208
$(VM_PREFIX)__ALL.cpp: $(addsuffix .cpp, $(VM_FAST) $(VM_SLOW))
209
$(VERILATOR_INCLUDER) -DVL_INCLUDE_OPT=include $^ > $@
210
all_cpp: $(VM_PREFIX)__ALL.cpp
212
# Parallel build: Each .cpp file by itself. This can be somewhat slower for
213
# very small designs and examples, but is a lot faster for large designs.
215
VK_OBJS += $(VK_OBJS_FAST) $(VK_OBJS_SLOW)
218
# When archiving just objects (.o), use single $(AR) run
219
# 1. Make .verilator_deplist.tmp file with list of objects so don't exceed
220
# the command line limits when calling $(AR).
221
# The approach to write the dependency file is compatible with GNU Make 3,
222
# and can be simplified using the file function once GNU Make 4.x becomes
223
# the minimum supported version.
224
# When merging objects (.o) and archives (.a) additionally:
225
# 1. Extract object files from .a
226
# 2. Create a new archive from extracted .o and given .o
227
%.a: | %.verilator_deplist.tmp
228
$(foreach L, $(filter-out %.a,$^), $(shell echo $L >>$@.verilator_deplist.tmp))
229
@if test $(words $(filter %.a,$^)) -eq 0; then \
231
cat $@.verilator_deplist.tmp | xargs $(AR) -rc $@; \
234
$(RM) -rf $@.tmpdir; \
235
for archive in $(filter %.a,$^); do \
236
mkdir -p $@.tmpdir/$$(basename $${archive}); \
237
cd $@.tmpdir/$$(basename $${archive}); \
238
$(AR) -x ../../$${archive}; \
242
cat $@.verilator_deplist.tmp | xargs $(AR) -rc $@; \
243
$(AR) -rcs $@ $@.tmpdir/*/*.o; \
245
; $(RM) -rf $@.verilator_deplist.tmp $@.tmpdir
247
# Truncate the dependency list file used in the %.a target above.
248
%.verilator_deplist.tmp:
251
$(VM_PREFIX)__ALL.a: $(VK_OBJS) $(VM_HIER_LIBS)
254
######################################################################
257
ifneq ($(VM_DEFAULT_RULES),0)
258
# Compilation rule for anything not in $(VK_OBJS_FAST), $(VK_OBJS_SLOW), or
259
# $(VK_GLOBAL_OBJS) including verilated.o. This typically means user files
260
# passed on the Verilator command line.
262
# These rules put OPT_FAST/OPT_SLOW/OPT_GLOBAL before the other flags to
263
# allow USER_CPPFLAGS to override them
265
$(OBJCACHE) $(CXX) $(OPT_FAST) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
267
$(VK_OBJS_FAST): %.o: %.cpp $(VK_PCH_H).fast.gch
268
$(OBJCACHE) $(CXX) $(OPT_FAST) $(CXXFLAGS) $(CPPFLAGS) $(VK_PCH_I_FAST) -c -o $@ $<
270
$(VK_OBJS_SLOW): %.o: %.cpp $(VK_PCH_H).slow.gch
271
$(OBJCACHE) $(CXX) $(OPT_SLOW) $(CXXFLAGS) $(CPPFLAGS) $(VK_PCH_I_SLOW) -c -o $@ $<
273
$(VK_GLOBAL_OBJS): %.o: %.cpp
274
$(OBJCACHE) $(CXX) $(OPT_GLOBAL) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
276
# Precompile a header file
277
# PCH's compiler flags must match exactly the rules' above FAST/SLOW
278
# arguments used for the .cpp files, or the PCH file won't be used.
280
$(OBJCACHE) $(CXX) $(OPT_FAST) $(CXXFLAGS) $(CPPFLAGS) $(CFG_CXXFLAGS_PCH) $< -o $@
282
$(OBJCACHE) $(CXX) $(OPT_SLOW) $(CXXFLAGS) $(CPPFLAGS) $(CFG_CXXFLAGS_PCH) $< -o $@
286
#Default rule embedded in make:
288
# $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
290
######################################################################
293
ifneq ($(findstring ccache-report,$(MAKECMDGOALS)),)
294
ifneq ($(OBJCACHE),ccache)
295
$(error ccache-report requires OBJCACHE to equal 'ccache')
297
VK_OTHER_GOALS := $(strip $(subst ccache-report,,$(MAKECMDGOALS)))
298
ifeq ($(VK_OTHER_GOALS),)
299
$(error ccache-report must be used with at least one other explicit target)
302
# Report ccache behaviour for this invocation of make
303
VK_CCACHE_LOGDIR := ccache-logs
304
VK_CCACHE_REPORT := $(VM_PREFIX)__ccache_report.txt
305
# Remove previous logfiles and report
306
$(shell rm -rf $(VK_CCACHE_LOGDIR) $(VK_CCACHE_REPORT))
311
$(VK_OBJS): | $(VK_CCACHE_LOGDIR)
313
$(VK_OBJS): export CCACHE_LOGFILE=$(VK_CCACHE_LOGDIR)/$@.log
315
$(VK_CCACHE_REPORT): $(VK_OBJS)
316
$(VERILATOR_CCACHE_REPORT) -o $@ $(VK_CCACHE_LOGDIR)
319
ccache-report: $(VK_CCACHE_REPORT)
322
# ccache-report runs last
323
ccache-report: $(VK_OTHER_GOALS)
326
######################################################################
331
@echo CPPFLAGS: $(CPPFLAGS)
332
@echo CXXFLAGS: $(CXXFLAGS)
333
@echo OPT_FAST: $(OPT_FAST)
334
@echo OPT_SLOW: $(OPT_SLOW)
335
@echo VK_OBJS: $(VK_OBJS)
336
@echo VK_OBJS_FAST: $(VK_OBJS_FAST)
337
@echo VK_OBJS_SLOW: $(VK_OBJS_SLOW)
338
@echo VM_CLASSES_FAST: $(VM_CLASSES_FAST)
339
@echo VM_CLASSES_SLOW: $(VM_CLASSES_SLOW)
340
@echo VM_GLOBAL_FAST: $(VM_GLOBAL_FAST)
341
@echo VM_GLOBAL_SLOW: $(VM_GLOBAL_SLOW)
342
@echo VM_PARALLEL_BUILDS: $(VM_PARALLEL_BUILDS)
343
@echo VM_PREFIX: $(VM_PREFIX)
344
@echo VM_SUPPORT_FAST: $(VM_SUPPORT_FAST)
345
@echo VM_SUPPORT_SLOW: $(VM_SUPPORT_SLOW)
348
######################################################################
349
### Detect out of date files and rebuild.
351
DEPS := $(wildcard *.d)