1# -*- Makefile -*-
2######################################################################
3# DESCRIPTION: Makefile commands for all verilated target files
4#
5# Copyright 2003-2021 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
8# Version 2.0.
9# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
10######################################################################
11
12AR = @AR@
13CXX = @CXX@
14LINK = @CXX@
15OBJCACHE ?= @OBJCACHE@
16PERL = @PERL@
17PYTHON3 = @PYTHON3@
18
19CFG_WITH_CCWARN = @CFG_WITH_CCWARN@
20CFG_WITH_LONGTESTS = @CFG_WITH_LONGTESTS@
21
22# Compiler flags to enable profiling
23CFG_CXXFLAGS_PROFILE = @CFG_CXXFLAGS_PROFILE@
24# Select newest language
25CFG_CXXFLAGS_STD_NEWEST = @CFG_CXXFLAGS_STD_NEWEST@
26# Compiler flags to use to turn off unused and generated code warnings, such as -Wno-div-by-zero
27CFG_CXXFLAGS_NO_UNUSED = @CFG_CXXFLAGS_NO_UNUSED@
28# Compiler flags that turn on extra warnings
29CFG_CXXFLAGS_WEXTRA = @CFG_CXXFLAGS_WEXTRA@
30# Linker libraries for multithreading
31CFG_LDLIBS_THREADS = @CFG_LDLIBS_THREADS@
32
33######################################################################
34# Programs
35
36VERILATOR_COVERAGE = $(PERL) $(VERILATOR_ROOT)/bin/verilator_coverage
37VERILATOR_INCLUDER = $(PERL) $(VERILATOR_ROOT)/bin/verilator_includer
38VERILATOR_CCACHE_REPORT = $(PYTHON3) $(VERILATOR_ROOT)/bin/verilator_ccache_report
39
40######################################################################
41# Make checks
42
43ifneq ($(words $(CURDIR)),1)
44 $(error Unsupported: GNU Make cannot build in directories containing spaces, build elsewhere: '$(CURDIR)')
45endif
46
47######################################################################
48# C Preprocessor flags
49
50# Add -MMD -MP if you're using a recent version of GCC.
51VK_CPPFLAGS_ALWAYS += \
52		-MMD \
53		-I$(VERILATOR_ROOT)/include \
54		-I$(VERILATOR_ROOT)/include/vltstd \
55		-DVM_COVERAGE=$(VM_COVERAGE) \
56		-DVM_SC=$(VM_SC) \
57		-DVM_TRACE=$(VM_TRACE) \
58		-DVM_TRACE_FST=$(VM_TRACE_FST) \
59		$(CFG_CXXFLAGS_NO_UNUSED) \
60
61ifeq ($(CFG_WITH_CCWARN),yes)	# Local... Else don't burden users
62VK_CPPFLAGS_WALL += -Wall $(CFG_CXXFLAGS_WEXTRA) -Werror
63endif
64
65CPPFLAGS += -I. $(VK_CPPFLAGS_WALL) $(VK_CPPFLAGS_ALWAYS)
66
67VPATH += ..
68VPATH += $(VERILATOR_ROOT)/include
69VPATH += $(VERILATOR_ROOT)/include/vltstd
70
71#OPT = -ggdb -DPRINTINITSTR -DDETECTCHANGE
72#OPT = -ggdb -DPRINTINITSTR
73CPPFLAGS += $(OPT)
74
75CPPFLAGS += $(M32)
76LDFLAGS  += $(M32)
77
78# Allow upper level user makefiles to specify flags they want.
79# These aren't ever set by Verilator, so users are free to override them.
80CPPFLAGS += $(USER_CPPFLAGS)
81LDFLAGS  += $(USER_LDFLAGS)
82LDLIBS   += $(USER_LDLIBS)
83
84# Add flags from -CFLAGS and -LDFLAGS on Verilator command line
85CPPFLAGS += $(VM_USER_CFLAGS)
86LDFLAGS  += $(VM_USER_LDFLAGS)
87LDLIBS   += $(VM_USER_LDLIBS)
88
89######################################################################
90# Optimization control.
91
92# See also the BENCHMARKING & OPTIMIZATION section of the manual.
93
94# Optimization flags for non performance-critical/rarely executed code.
95# No optimization by default, which improves compilation speed.
96OPT_SLOW =
97# Optimization for performance critical/hot code. Most time is spent in these
98# routines. Optimizing by default for improved execution speed.
99OPT_FAST = -Os
100# Optimization applied to the common run-time library used by verilated models.
101# For compatibility this is called OPT_GLOBAL even though it only applies to
102# files in the run-time library. Normally there should be no need for the user
103# to change this as the library is small, but can have significant speed impact.
104OPT_GLOBAL = -Os
105
106#######################################################################
107##### Profile builds
108
109ifeq ($(VM_PROFC),1)
110  CPPFLAGS += $(CFG_CXXFLAGS_PROFILE)
111  LDFLAGS  += $(CFG_CXXFLAGS_PROFILE)
112endif
113
114#######################################################################
115##### SystemC builds
116
117ifeq ($(VM_SC),1)
118  CPPFLAGS += $(SYSTEMC_CXX_FLAGS) $(addprefix -I, $(SYSTEMC_INCLUDE))
119  LDFLAGS  += $(SYSTEMC_CXX_FLAGS) $(addprefix -L, $(SYSTEMC_LIBDIR))
120  SC_LIBS   = -lsystemc
121 ifneq ($(wildcard $(SYSTEMC_LIBDIR)/*numeric_bit*),)
122  # Systemc 1.2.1beta
123  SC_LIBS   += -lnumeric_bit -lqt
124 endif
125endif
126
127#######################################################################
128##### Threaded builds
129
130ifneq ($(VM_C11),0)
131 ifneq ($(VM_C11),)
132  VK_C11=1
133 endif
134endif
135
136ifneq ($(VM_THREADS),0)
137 ifneq ($(VM_THREADS),)
138  CPPFLAGS += -DVL_THREADED
139  VK_C11=1
140  VK_LIBS_THREADED=1
141 endif
142endif
143
144ifneq ($(VM_TRACE_THREADS),0)
145 ifneq ($(VM_TRACE_THREADS),)
146  ifeq ($(findstring -DVL_THREADED,$(CPPFLAGS)),)
147   $(error VM_TRACE_THREADS requires VM_THREADS)
148  endif
149  CPPFLAGS += -DVL_TRACE_THREADED
150  VK_C11=1
151  VK_LIBS_THREADED=1
152 endif
153endif
154
155
156ifneq ($(VM_TRACE_FST_WRITER_THREAD),0)
157 ifneq ($(VM_TRACE_FST_WRITER_THREAD),)
158  CPPFLAGS += -DVL_TRACE_FST_WRITER_THREAD
159  VK_C11=1
160  VK_LIBS_THREADED=1
161 endif
162endif
163
164ifneq ($(VK_C11),0)
165 ifneq ($(VK_C11),)
166  # Need C++11 at least, so always default to newest
167  CPPFLAGS += $(CFG_CXXFLAGS_STD_NEWEST)
168 endif
169endif
170
171ifneq ($(VK_LIBS_THREADED),0)
172 ifneq ($(VK_LIBS_THREADED),)
173  LDLIBS += $(CFG_LDLIBS_THREADS)
174 endif
175endif
176
177#######################################################################
178### Aggregates
179
180VM_FAST += $(VM_CLASSES_FAST) $(VM_SUPPORT_FAST)
181VM_SLOW += $(VM_CLASSES_SLOW) $(VM_SUPPORT_SLOW)
182
183#######################################################################
184### Overall Objects Linking
185
186VK_FAST_OBJS = $(addsuffix .o, $(VM_FAST))
187VK_SLOW_OBJS = $(addsuffix .o, $(VM_SLOW))
188
189VK_USER_OBJS   = $(addsuffix .o, $(VM_USER_CLASSES))
190
191# Note VM_GLOBAL_FAST and VM_GLOBAL_SLOW holds the files required from the
192# run-time library. In practice everything is actually in VM_GLOBAL_FAST,
193# but keeping the distinction for compatibility for now.
194VK_GLOBAL_OBJS = $(addsuffix .o, $(VM_GLOBAL_FAST) $(VM_GLOBAL_SLOW))
195
196ifneq ($(VM_PARALLEL_BUILDS),1)
197  # Fast build for small designs: All .cpp files in one fell swoop. This
198  # saves total compute, but can be slower if only a little changes. It is
199  # also a lot slower for medium to large designs when the speed of the C
200  # compiler dominates, which in this mode is not parallelizable.
201
202  VK_OBJS += $(VM_PREFIX)__ALL.o
203  $(VM_PREFIX)__ALL.cpp: $(addsuffix .cpp, $(VM_FAST) $(VM_SLOW))
204	$(VERILATOR_INCLUDER) -DVL_INCLUDE_OPT=include $^ > $@
205  all_cpp: $(VM_PREFIX)__ALL.cpp
206else
207  # Parallel build: Each .cpp file by itself. This can be somewhat slower for
208  # very small designs and examples, but is a lot faster for large designs.
209
210  VK_OBJS += $(VK_FAST_OBJS) $(VK_SLOW_OBJS)
211endif
212
213# When archiving just objects (.o), use single $(AR) run
214#   1. Make .verilator_deplist.tmp file with list of objects so don't exceed
215#      the command line limits when calling $(AR).
216#      The approach to write the dependency file is compatible with GNU Make 3,
217#      and can be simplified using the file function once GNU Make 4.x becomes
218#      the minimum supported version.
219# When merging objects (.o) and archives (.a) additionally:
220#   1. Extract object files from .a
221#   2. Create a new archive from extracted .o and given .o
222%.a: | %.verilator_deplist.tmp
223	$(info Archive $(AR) -rcs $@ $^)
224	$(foreach L, $(filter-out %.a,$^), $(shell echo $L >>$@.verilator_deplist.tmp))
225	@if test $(words $(filter %.a,$^)) -eq 0; then \
226		$(AR) -rcs $@ @$@.verilator_deplist.tmp; \
227	else \
228		$(RM) -rf $@.tmpdir; \
229		for archive in $(filter %.a,$^); do \
230			mkdir -p $@.tmpdir/$$(basename $${archive}); \
231			cd $@.tmpdir/$$(basename $${archive}); \
232			$(AR) -x ../../$${archive}; \
233			cd ../..; \
234		done; \
235		$(AR) -rcs $@ @$@.verilator_deplist.tmp $@.tmpdir/*/*.o; \
236	fi \
237	; $(RM) -rf $@.verilator_deplist.tmp $@.tmpdir
238
239# Truncate the dependency list file used in the %.a target above.
240%.verilator_deplist.tmp:
241	echo "" > $@
242
243$(VM_PREFIX)__ALL.a: $(VK_OBJS) $(VM_HIER_LIBS)
244
245
246######################################################################
247### Compile rules
248
249ifneq ($(VM_DEFAULT_RULES),0)
250# Anything not in $(VK_SLOW_OBJS) or $(VK_GLOBAL_OBJS), including verilated.o
251# and user files passed on the Verilator command line use this rule.
252%.o: %.cpp
253	$(OBJCACHE) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OPT_FAST) -c -o $@ $<
254
255$(VK_SLOW_OBJS): %.o: %.cpp
256	$(OBJCACHE) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OPT_SLOW) -c -o $@ $<
257
258$(VK_GLOBAL_OBJS): %.o: %.cpp
259	$(OBJCACHE) $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OPT_GLOBAL) -c -o $@ $<
260endif
261
262#Default rule embedded in make:
263#.cpp.o:
264#	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
265
266######################################################################
267### ccache report
268
269ifneq ($(findstring ccache-report,$(MAKECMDGOALS)),)
270  ifneq ($(OBJCACHE),ccache)
271    $(error ccache-report requires OBJCACHE to equal 'ccache')
272  endif
273  VK_OTHER_GOALS := $(strip $(subst ccache-report,,$(MAKECMDGOALS)))
274  ifeq ($(VK_OTHER_GOALS),)
275    $(error ccache-report must be used with at least one other explicit target)
276  endif
277
278  # Report ccache behaviour for this invocation of make
279  VK_CCACHE_LOGDIR := ccache-logs
280  VK_CCACHE_REPORT := $(VM_PREFIX)__ccache_report.txt
281  # Remove previous logfiles and report
282  $(shell rm -rf $(VK_CCACHE_LOGDIR) $(VK_CCACHE_REPORT))
283
284$(VK_CCACHE_LOGDIR):
285	mkdir -p $@
286
287$(VK_OBJS): | $(VK_CCACHE_LOGDIR)
288
289$(VK_OBJS): export CCACHE_LOGFILE=$(VK_CCACHE_LOGDIR)/$@.log
290
291$(VK_CCACHE_REPORT): $(VK_OBJS)
292	$(VERILATOR_CCACHE_REPORT) -o $@ $(VK_CCACHE_LOGDIR)
293
294.PHONY: ccache-report
295ccache-report: $(VK_CCACHE_REPORT)
296	@cat $<
297
298# ccache-report runs last
299ccache-report: $(VK_OTHER_GOALS)
300endif
301
302######################################################################
303### Debugging
304
305debug-make::
306	@echo
307	@echo CXXFLAGS: $(CXXFLAGS)
308	@echo CPPFLAGS: $(CPPFLAGS)
309	@echo OPT_FAST: $(OPT_FAST)
310	@echo OPT_SLOW: $(OPT_SLOW)
311	@echo VM_PREFIX:  $(VM_PREFIX)
312	@echo VM_PARALLEL_BUILDS:  $(VM_PARALLEL_BUILDS)
313	@echo VM_CLASSES_FAST: $(VM_CLASSES_FAST)
314	@echo VM_CLASSES_SLOW: $(VM_CLASSES_SLOW)
315	@echo VM_SUPPORT_FAST: $(VM_SUPPORT_FAST)
316	@echo VM_SUPPORT_SLOW: $(VM_SUPPORT_SLOW)
317	@echo VM_GLOBAL_FAST: $(VM_GLOBAL_FAST)
318	@echo VM_GLOBAL_SLOW: $(VM_GLOBAL_SLOW)
319	@echo VK_OBJS: $(VK_OBJS)
320	@echo
321
322######################################################################
323### Detect out of date files and rebuild.
324
325DEPS := $(wildcard *.d)
326ifneq ($(DEPS),)
327include $(DEPS)
328endif
329