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