[xcc] minor performance tweaks
[riscv-isa-sim.git] / Makefile.in
1 #=========================================================================
2 # Toplevel Makefile for the Modular C++ Build System
3 #=========================================================================
4 # Please read the documenation in 'mcppbs-doc.txt' for more details on
5 # how the Modular C++ Build System works. For most projects, a developer
6 # will not need to make any changes to this makefile. The key targets
7 # are as follows:
8 #
9 # - default : build all libraries and programs
10 # - check : build and run all unit tests
11 # - install : install headers, project library, and some programs
12 # - clean : remove all generated content (except autoconf files)
13 # - dist : make a source tarball
14 # - distcheck : make a source tarball, untar it, check it, clean it
15 # - distclean : remove everything
16 #
17
18 #-------------------------------------------------------------------------
19 # Basic setup
20 #-------------------------------------------------------------------------
21
22 # Remove all default implicit rules since they can cause subtle bugs
23 # and they just make things run slower
24 .SUFFIXES:
25 % : %,v
26 % : RCS/%,v
27 % : RCS/%
28 % : s.%
29 % : SCCS/s.%
30
31 # Default is to build the prereqs of the all target (defined at bottom)
32 default : all
33 .PHONY : default
34
35 project_name := @PACKAGE_TARNAME@
36 src_dir := @srcdir@
37 scripts_dir := $(src_dir)/scripts
38
39 # If the version information is not in the configure script, then we
40 # assume that we are in a working directory. We use the vcs-version.sh
41 # script in the scripts directory to generate an appropriate version
42 # string. Currently the way things are setup we have to run this script
43 # everytime we run make so the script needs to be as fast as possible.
44
45 ifeq (@PACKAGE_VERSION@,?)
46 project_ver:=$(shell $(scripts_dir)/vcs-version.sh $(src_dir))
47 else
48 project_ver:=@PACKAGE_VERSION@
49 endif
50
51 # Installation directories
52
53 prefix := @prefix@
54 enable_stow := @enable_stow@
55
56 ifeq ($(enable_stow),yes)
57 stow_pkg_dir := $(prefix)/pkgs
58 DESTDIR ?= $(stow_pkg_dir)/$(project_name)-$(project_ver)
59 else
60 DESTDIR ?= $(prefix)
61 endif
62
63 install_hdrs_dir := $(DESTDIR)/include/$(project_name)
64 install_libs_dir := $(DESTDIR)/lib/$(project_name)
65 install_exes_dir := $(DESTDIR)/bin
66
67 #-------------------------------------------------------------------------
68 # List of subprojects
69 #-------------------------------------------------------------------------
70
71 sprojs := @subprojects@
72 sprojs_enabled := @subprojects_enabled@
73
74 sprojs_include := -I. $(addprefix -I$(src_dir)/, $(sprojs_enabled))
75 VPATH := $(addprefix $(src_dir)/, $(sprojs_enabled))
76
77 #-------------------------------------------------------------------------
78 # Programs and flags
79 #-------------------------------------------------------------------------
80
81 # C++ compiler
82 # - CPPFLAGS : flags for the preprocessor (eg. -I,-D)
83 # - CXXFLAGS : flags for C++ compiler (eg. -Wall,-g,-O3)
84
85 CC := @CC@
86 CXX := @CXX@
87 CPPFLAGS := @CPPFLAGS@
88 CXXFLAGS := @CXXFLAGS@
89 COMPILE := $(CXX) -MMD -MP $(CPPFLAGS) $(CXXFLAGS) \
90 $(sprojs_include)
91 COMPILE_C := $(CC) -MMD -MP $(CPPFLAGS) $(CXXFLAGS) \
92 $(sprojs_include)
93 # Linker
94 # - LDFLAGS : Flags for the linker (eg. -L)
95 # - LIBS : Library flags (eg. -l)
96
97 LD := $(CXX)
98 LDFLAGS := @LDFLAGS@
99 LIBS := @LIBS@
100 LINK := $(LD) $(LDFLAGS)
101
102 # Library creation
103
104 AR := @AR@
105 RANLIB := @RANLIB@
106
107 # Host simulator
108
109 RUN := @RUN@
110 RUNFLAGS := @RUNFLAGS@
111
112 # Installation
113
114 MKINSTALLDIRS := $(scripts_dir)/mk-install-dirs.sh
115 INSTALL := @INSTALL@
116 INSTALL_HDR := $(INSTALL) -m 444
117 INSTALL_LIB := $(INSTALL) -m 644
118 INSTALL_EXE := $(INSTALL) -m 555
119 STOW := @stow@
120
121 #-------------------------------------------------------------------------
122 # Include subproject makefile fragments
123 #-------------------------------------------------------------------------
124
125 sprojs_mk = $(addsuffix .mk, $(sprojs_enabled))
126
127 -include $(sprojs_mk)
128
129 dist_junk += $(sprojs_mk)
130
131 #-------------------------------------------------------------------------
132 # Reverse list helper function
133 #-------------------------------------------------------------------------
134 # This function is used by the subproject template to reverse the list
135 # of dependencies. It uses recursion to perform the reversal.
136 #
137 # Arguments:
138 # $(1) : space separated input list
139 # retval : input list in reverse order
140 #
141
142 reverse_list = $(call reverse_list_h,$(1),)
143 define reverse_list_h
144 $(if $(strip $(1)), \
145 $(call reverse_list_h, \
146 $(wordlist 2,$(words $(1)),$(1)), \
147 $(firstword $(1)) $(2)), \
148 $(2))
149 endef
150
151 #-------------------------------------------------------------------------
152 # Template for per subproject rules
153 #-------------------------------------------------------------------------
154 # The template is instantiated for each of the subprojects. It relies on
155 # subprojects defining a certain set of make variables which are all
156 # prefixed with the subproject name. Since subproject names can have
157 # dashes in them (and the make variables are assumed to only use
158 # underscores) the template takes two arguments - one with the regular
159 # subproject name and one with dashes replaced with underscores.
160 #
161 # Arguments:
162 # $(1) : real subproject name (ie with dashes)
163 # $(2) : normalized subproject name (ie dashes replaced with underscores)
164 #
165
166 define subproject_template
167
168 # In some (rare) cases, a subproject might not have any actual object
169 # files. It might only include header files or program sources. To keep
170 # things consistent we still want a library for this subproject, so in
171 # this spectial case we create a dummy source file and thus the build
172 # system will create a library for this subproject with just the
173 # corresponding dummy object file.
174
175 ifeq ($$(strip $$($(2)_srcs) $$($(2)_c_srcs)),)
176 $(2)_srcs += _$(1).cc
177 $(2)_junk += _$(1).cc
178 endif
179
180 _$(1).cc :
181 echo "int _$(2)( int arg ) { return arg; }" > $$@
182
183 # Build the object files for this subproject
184
185 $(2)_objs := $$(patsubst %.cc, %.o, $$($(2)_srcs))
186 $(2)_c_objs := $$(patsubst %.c, %.o, $$($(2)_c_srcs))
187 $(2)_deps := $$(patsubst %.o, %.d, $$($(2)_objs))
188 $(2)_c_deps := $$(patsubst %.o, %.d, $$($(2)_c_objs))
189 $$($(2)_objs) : %.o : %.cc
190 $(COMPILE) -c $$<
191 $$($(2)_c_objs) : %.o : %.c
192 $(COMPILE_C) -c $$<
193
194 $(2)_junk += $$($(2)_objs) $$($(2)_c_objs) $$($(2)_deps) $$($(2)_c_deps)
195
196 # Build a library for this subproject
197
198 lib$(1).a : $$($(2)_objs) $$($(2)_c_objs)
199 $(AR) rcv $$@ $$^
200 $(RANLIB) $$@
201
202 $(2)_junk += lib$(1).a
203
204 # Reverse the dependency list so that a given subproject only depends on
205 # subprojects listed to its right. This is the correct order for linking
206 # the list of subproject libraries.
207
208 $(2)_reverse_deps := $$(call reverse_list,$$($(2)_subproject_deps))
209
210 # Build unit tests
211
212 $(2)_test_objs := $$(patsubst %.cc, %.o, $$($(2)_test_srcs))
213 $(2)_test_deps := $$(patsubst %.o, %.d, $$($(2)_test_objs))
214 $(2)_test_exes := $$(patsubst %.t.cc, %-utst, $$($(2)_test_srcs))
215 $(2)_test_outs := $$(patsubst %, %.out, $$($(2)_test_exes))
216 $(2)_test_libs := $(1) $$($(2)_reverse_deps) utst
217 $(2)_test_libnames := $$(patsubst %, lib%.a, $$($(2)_test_libs))
218 $(2)_test_libarg := -L. $$(patsubst %, -l%, $$($(2)_test_libs))
219
220 $$($(2)_test_objs) : %.o : %.cc
221 $(COMPILE) -c $$<
222
223 $$($(2)_test_exes) : %-utst : %.t.o $$($(2)_test_libnames)
224 $(LINK) -o $$@ $$< $$($(2)_test_libarg) $(LIBS)
225
226 $(2)_deps += $$($(2)_test_deps)
227 $(2)_junk += \
228 $$($(2)_test_objs) $$($(2)_test_deps) \
229 $$($(2)_test_exes) *.junk-dat
230
231 # Run unit tests
232
233 $$($(2)_test_outs) : %.out : %
234 $(RUN) $(RUNFLAGS) ./$$< default | tee $$@
235
236 $(2)_junk += $$($(2)_test_outs)
237
238 # Build programs
239
240 $(2)_prog_objs := $$(patsubst %.cc, %.o, $$($(2)_prog_srcs))
241 $(2)_prog_deps := $$(patsubst %.o, %.d, $$($(2)_prog_objs))
242 $(2)_prog_exes := $$(patsubst %.cc, %, $$($(2)_prog_srcs))
243 $(2)_prog_libs := $(1) $$($(2)_reverse_deps)
244 $(2)_prog_libnames := $$(patsubst %, lib%.a, $$($(2)_prog_libs))
245 $(2)_prog_libarg := -L. $$(patsubst %, -l%, $$($(2)_prog_libs))
246
247 $$($(2)_prog_objs) : %.o : %.cc
248 $(COMPILE) -c $$<
249
250 $$($(2)_prog_exes) : % : %.o $$($(2)_prog_libnames)
251 $(LINK) -o $$@ $$< $$($(2)_prog_libarg) $(LIBS)
252
253 $(2)_deps += $$($(2)_prog_deps)
254 $(2)_junk += $$($(2)_prog_objs) $$($(2)_prog_deps) $$($(2)_prog_exes)
255
256 # Build programs which will be installed
257
258 $(2)_install_prog_objs := $$(patsubst %.cc, %.o, $$($(2)_install_prog_srcs))
259 $(2)_install_prog_deps := $$(patsubst %.o, %.d, $$($(2)_install_prog_objs))
260 $(2)_install_prog_exes := $$(patsubst %.cc, %, $$($(2)_install_prog_srcs))
261
262 $$($(2)_install_prog_objs) : %.o : %.cc
263 $(COMPILE) -c $$<
264
265 $$($(2)_install_prog_exes) : % : %.o $$($(2)_prog_libnames)
266 $(LINK) -o $$@ $$< $$($(2)_prog_libarg) $(LIBS)
267
268 $(2)_deps += $$($(2)_install_prog_deps)
269 $(2)_junk += \
270 $$($(2)_install_prog_objs) $$($(2)_install_prog_deps) \
271 $$($(2)_install_prog_exes)
272
273 # Subproject specific targets
274
275 all-$(1) : lib$(1).a $$($(2)_install_prog_exes)
276
277 check-$(1) : $$($(2)_test_outs)
278 echo; grep -h -e'Unit Tests' -e'FAILED' -e'Segementation' $$^; echo
279
280 clean-$(1) :
281 rm -rf $$($(2)_junk)
282
283 .PHONY : all-$(1) check-$(1) clean-$(1)
284
285 # Update running variables
286
287 libs += lib$(1).a
288 objs += $$($(2)_objs)
289 srcs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_srcs))
290 hdrs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_hdrs))
291 junk += $$($(2)_junk)
292 deps += $$($(2)_deps)
293
294 test_outs += $$($(2)_test_outs)
295
296 install_hdrs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_hdrs))
297 install_libs += lib$(1).a
298 install_exes += $$($(2)_install_prog_exes)
299
300 endef
301
302 # Iterate over the subprojects and call the template for each one
303
304 $(foreach sproj,$(sprojs_enabled), \
305 $(eval $(call subproject_template,$(sproj),$(subst -,_,$(sproj)))))
306
307 #-------------------------------------------------------------------------
308 # Autodependency files
309 #-------------------------------------------------------------------------
310
311 -include $(deps)
312
313 deps : $(deps)
314 .PHONY : deps
315
316 #-------------------------------------------------------------------------
317 # Check
318 #-------------------------------------------------------------------------
319
320 check : $(test_outs)
321 echo; grep -h -e'Unit Tests' -e'FAILED' -e'Segementation' $^; echo
322
323 .PHONY : check
324
325 #-------------------------------------------------------------------------
326 # Installation
327 #-------------------------------------------------------------------------
328
329 install-hdrs : $(install_hdrs)
330 $(MKINSTALLDIRS) $(install_hdrs_dir)
331 for file in $(install_hdrs); \
332 do \
333 $(INSTALL_HDR) $$file $(install_hdrs_dir); \
334 done
335
336 install-libs : $(install_libs)
337 $(MKINSTALLDIRS) $(install_libs_dir)
338 for file in $(install_libs); \
339 do \
340 $(INSTALL_LIB) $$file $(install_libs_dir); \
341 done
342
343 install-exes : $(install_exes)
344 $(MKINSTALLDIRS) $(install_exes_dir)
345 for file in $(install_exes); \
346 do \
347 $(INSTALL_EXE) $$file $(install_exes_dir); \
348 done
349
350 install : install-hdrs install-libs install-exes
351 ifeq ($(enable_stow),yes)
352 $(MKINSTALLDIRS) $(stow_pkg_dir)
353 cd $(stow_pkg_dir) && \
354 $(STOW) --delete $(project_name)-* && \
355 $(STOW) $(project_name)-$(project_ver)
356 endif
357
358 .PHONY : install install-hdrs install-libs install-exes
359
360 #-------------------------------------------------------------------------
361 # Regenerate configure information
362 #-------------------------------------------------------------------------
363
364 configure_prereq = \
365 $(src_dir)/configure.ac \
366 $(src_dir)/aclocal.m4 \
367 $(join $(addprefix $(src_dir)/, $(sprojs_enabled)), \
368 $(patsubst %, /%.ac, $(sprojs_enabled)))
369
370 $(src_dir)/configure : $(configure_prereq)
371 cd $(src_dir) && autoconf && autoheader
372
373 config.status : $(src_dir)/configure
374 ./config.status --recheck
375
376 sprojs_mk_in = \
377 $(join $(addprefix $(src_dir)/, $(sprojs_enabled)), \
378 $(patsubst %, /%.mk.in, $(sprojs_enabled)))
379
380 Makefile : $(src_dir)/Makefile.in $(sprojs_mk_in) config.status
381 ./config.status
382
383 dist_junk += config.status config.h Makefile config.log
384
385 #-------------------------------------------------------------------------
386 # Distribution
387 #-------------------------------------------------------------------------
388 # The distribution tarball is named project-ver.tar.gz and it includes
389 # both enabled and disabled subprojects.
390
391 dist_files = \
392 $(sprojs) \
393 README \
394 style-guide.txt \
395 mcppbs-uguide.txt \
396 scripts \
397 configure.ac \
398 aclocal.m4 \
399 configure \
400 config.h.in \
401 Makefile.in \
402
403 dist_dir := $(project_name)-$(project_ver)
404 dist_tgz := $(project_name)-$(project_ver).tar.gz
405
406 # Notice that when we make the distribution we rewrite the configure.ac
407 # script with the current version and we rerun autoconf in the new
408 # source directory so that the distribution will have the proper version
409 # information. We also rewrite the "Version : " line in the README.
410
411 dist :
412 rm -rf $(dist_dir)
413 mkdir $(dist_dir)
414 tar -C $(src_dir) -cf - $(dist_files) | tar -C $(dist_dir) -xpf -
415 sed -i.bak 's/^\(# Version :\).*/\1 $(project_ver)/' $(dist_dir)/README
416 sed -i.bak 's/\( proj_version,\).*/\1 [$(project_ver)])/' $(dist_dir)/configure.ac
417 cd $(dist_dir) && \
418 autoconf && autoheader && \
419 rm -rf autom4te.cache configure.ac.bak README.bak
420 tar -czvf $(dist_tgz) $(dist_dir)
421 rm -rf $(dist_dir)
422
423 # You can use the distcheck target to try untarring the distribution and
424 # then running configure, make, make check, and make distclean. A
425 # "directory is not empty" error means distclean is not removing
426 # everything.
427
428 distcheck : dist
429 rm -rf $(dist_dir)
430 tar -xzvf $(dist_tgz)
431 mkdir -p $(dist_dir)/build
432 cd $(dist_dir)/build; ../configure; make; make check; make distclean
433 rm -rf $(dist_dir)
434
435 junk += $(project_name)-*.tar.gz
436
437 .PHONY : dist distcheck
438
439 #-------------------------------------------------------------------------
440 # Default
441 #-------------------------------------------------------------------------
442
443 all : $(install_hdrs) $(install_libs) $(install_exes)
444 .PHONY : all
445
446 #-------------------------------------------------------------------------
447 # Makefile debugging
448 #-------------------------------------------------------------------------
449 # This handy rule will display the contents of any make variable by
450 # using the target debug-<varname>. So for example, make debug-junk will
451 # display the contents of the junk variable.
452
453 debug-% :
454 @echo $* = $($*)
455
456 #-------------------------------------------------------------------------
457 # Clean up junk
458 #-------------------------------------------------------------------------
459
460 clean :
461 rm -rf *~ \#* $(junk)
462
463 distclean :
464 rm -rf *~ \#* $(junk) $(dist_junk)
465
466 .PHONY : clean distclean