1 ifndef Makeit.mak.included
2 Makeit.mak.included := 1
4 # These variables should be provided by the Makefile that include us:
5 # P should be the project name, mostly used to handle include directories
6 # T should be the path to the top-level directory.
7 # S should be sub-directory where the current makefile is, relative to $T.
9 # Verbosity flag (empty show nice messages, non-empty use make messages)
10 # When used internal, $V expand to @ is nice messages should be printed, this
11 # way it's easy to add $V in front of commands that should be silenced when
12 # displaying the nice messages.
13 override V := $(if $V,,@)
15 override V := $(if $(findstring s,$(MAKEFLAGS)),,$V)
17 # Flavor (variant), should be one of "dbg", "opt" or "cov"
20 # Use C++ linker by default
23 # Default mode used to install files
26 # Default install flags
29 # Use pre-compiled headers if non-empty
32 # If non-empty, use valgrind to run commands via the "valgrind" function
35 # Options to pass to valgrind; if the variable $(VALGRIND_SUPP) is non-empty
36 # it will be used as a suppressions file.
37 VALGRIND_CMD ?= valgrind --tool=memcheck --leak-check=yes --db-attach=no \
38 --num-callers=24 --leak-resolution=high --track-fds=yes \
40 $(if $V,--log-file=$<.valgrind.log) \
41 $(if $(VALGRIND_SUPP),--suppressions=$(VALGRIND_SUPP))
43 # Command to generate Sphinx based documentation
44 SPHINX ?= sphinx-build
46 # Format to build using Sphinx (html, pickle, htmlhelp, latex, changes or
47 # linkcheck; see sphinx-build docs for details)
50 # Paper size for Sphinx LaTeX output (a4, letter, etc.)
51 SPHINX_PAPERSIZE ?= a4
57 # Base directory where to install files (can be overridden, should be absolute)
60 # Path to a complete alternative environment, usually a jail, or an installed
61 # system mounted elsewhere than /.
64 # Use absolute paths to avoid problems with automatic dependencies when
65 # building from subdirectories
68 # Name of the current directory, relative to $T
69 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
71 # Base directory where to put variants (Variants Directory)
74 # Generated files top directory
77 # Objects (and other garbage like pre-compiled headers and dependency files)
87 # Documentation directory
90 # Installation directory
91 I := $(DESTDIR)$(prefix)
94 INCLUDE_DIR ?= $G/include
96 # Directory of the current makefile (this might not be the same as $(CURDIR)
97 # This variable is "lazy" because $S changes all the time, so it should be
98 # evaluated in the context where $C is used, not here.
105 # Compare two strings, if they are the same, returns the string, if not,
107 eq = $(if $(subst $1,,$2),,$1)
109 # Find sources files and get the corresponding object names. The first
110 # argument should be the sources extension ("c" or "cpp" typically). The
111 # second argument is where to search for the sources ($C if omitted). The
112 # resulting files will always have the suffix "o" and the directory rewritten
113 # to match the directory structure (from $T) but in the $O directory. For
114 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
115 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
116 # $(call find_objects,c)
117 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
118 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
119 find $(if $2,$2,$C) -name '*.$1'))
121 # Find files and get the their file names relative to another directory. The
122 # first argument should be the files suffix (".h" or ".cpp" for example). The
123 # second argument is a directory rewrite, the matched files will be rewriten to
124 # be in the directory specified in this argument (it defaults to the third
125 # argument if omitted). The third argument is where to search for the files
127 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
128 $(shell find $(if $3,$3,$C) -name '*$1'))
130 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
131 # directory, so it can be displayed as if it were a relative directory. Take
132 # just one argument, the file name.
133 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
134 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
135 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
136 $(call abbr_helper,$1)))
138 # Execute a command printing a nice message if $V is @.
139 # The first argument is mandatory and it's the command to execute. The second
140 # and third arguments are optional and are the target name and command name to
143 echo ' $(call abbr,$(if $3,$(strip $3),$(firstword $1))) \
144 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
146 # Same as vexec but it silence the echo command (prepending a @ if $V).
147 exec = $V$(call vexec,$1,$2,$3)
149 # Compile a source file to an object, generating pre-compiled headers and
150 # dependencies. The pre-compiled headers are generated only if the system
151 # includes change. This function is designed to be used as a command in a rule.
152 # It takes one argument only, the type of file to compile (typically "c" or
153 # "cpp"). What to compile and the output files are built using the automatic
154 # variables from a rule. You can add non-propagated object-specific flags
155 # defining a variable with the name of the target followed with ".EXTRA_FLAGS".
158 $Vif test -f $O/$*.d; then \
160 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
161 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
162 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
166 mv "$$tmp" "$O/$*.$1.h"; \
167 $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
168 -o "$O/$*.$1.h.gch" "$O/$*.$1.h",$O/$*.$1.h.gch); \
171 touch "$O/$*.$1.h"; \
174 $(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
175 $(if $(GCH),-include $O/$*.$1.h) $<)
178 # Link object files to build an executable. The objects files are taken from
179 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
180 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
181 # function is designed to be used as a command in a rule. The output name is
182 # taken from the rule automatic variables. If an argument is provided, it's
183 # included in the link command line. The variable LINKER is used to link the
184 # executable; for example, if you want to link a C++ executable, you should use
185 # LINKER := $(CXX). You can add non-propagated target-specific flags defining
186 # a variable with the name of the target followed with ".EXTRA_FLAGS". You can
187 # specify a non-propagated object-specific linker defining a variable with the
188 # name of the target followed with ".LINKER".
189 link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
190 $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
191 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
192 $(foreach obj,$(filter %.o,$^),$(obj)))
194 # Install a file. All arguments are optional. The first argument is the file
195 # mode (defaults to 0644). The second argument are extra flags to the install
196 # command (defaults to -D). The third argument is the source file to install
197 # (defaults to $<) and the last one is the destination (defaults to $@).
198 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
199 $(if $3,$3,$<) $(if $4,$4,$@))
201 # Concatenate variables together. The first argument is a list of variables
202 # names to concatenate. The second argument is an optional prefix for the
203 # variables and the third is the string to use as separator (" ~" if omitted).
207 # $(call varcat,A B,X_, --)
208 # Will produce something like "a -- b --"
209 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
211 # Replace variables with specified values in a template file. The first
212 # argument is a list of make variables names which will be replaced in the
213 # target file. The strings @VARNAME@ in the template file will be replaced
214 # with the value of the make $(VARNAME) variable and the result will be stored
215 # in the target file. The second (optional) argument is a prefix to add to the
216 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
217 # the template file, it will be replaced by the value of the make variable
218 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
219 # the destination file (both optional, $< and $@ are used if omitted). The
220 # fifth (optional) argument are options to pass to the substitute sed command
221 # (for example, use "g" if you want to do multiple substitutions per line).
222 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
225 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
226 # argument is the name of symbolic link to create. The link is only created if
227 # it doesn't already exist.
228 symlink_include_dir = $(shell \
229 test -L $(INCLUDE_DIR)/$1 \
230 || ln -s $C $(INCLUDE_DIR)/$1 )
232 # Create a file with flags used to trigger rebuilding when they change. The
233 # first argument is the name of the file where to store the flags, the second
234 # are the flags and the third argument is a text to be displayed if the flags
235 # have changed (optional). This should be used as a rule action or something
236 # where a shell script is expected.
237 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
238 $(if $3,test -f $1 && echo "$3";) \
241 # Include sub-directory's Build.mak. The only argument is a list of
242 # subdirectories for which Build.mak should be included. The $S directory is
243 # set properly before including each sub-directory's Build.mak and restored
245 define build_subdir_code
246 _parent__$d__dir_ := $$S
247 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
248 include $$T/$$S/Build.mak
249 S := $$(_parent__$d__dir_)
251 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
253 # Run a command through valgrind if $(VALGRIND) is non-empty. The first
254 # argument is the command to run. If $(VALGRIND) is empty, the command is
255 # executed as passed to this function. If valgrind is used, the
256 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
257 # definition for extra options that can be passed as make variables. The
258 # second argument is the name of the command to print when $V is non-empty (if
259 # omitted, the first word of the first argument is used).
260 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
261 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
262 $(if $2,$2,$(firstword $1)))
268 # Warn about everything
269 override CPPFLAGS += -Wall
271 # Use the includes directories to search for includes
272 override CPPFLAGS += -I$(INCLUDE_DIR)
274 # Let the program know where it will be installed
275 override CPPFLAGS += -DPREFIX=$(prefix)
277 # Be standard compliant
278 override CFLAGS += -std=c99 -pedantic
279 override CXXFLAGS += -std=c++98 -pedantic
281 # Use the generated library directory to for libraries
282 override LDFLAGS += -L$L -Wall
284 # Make sure the generated libraries can be found
285 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
292 override CPPFLAGS += -ggdb -DDEBUG
296 override CPPFLAGS += -O2 -DNDEBUG
300 override CPPFLAGS += -ggdb -pg --coverage
301 override LDFLAGS += -pg --coverage
305 # Automatic dependency handling
306 ################################
308 # These files are created during compilation.
309 sinclude $(shell test -d $O && find $O -name '*.d')
316 $O/%.o: $T/%.c $G/compile-c-flags
319 # Compile C++ objects
320 $O/%.o: $T/%.cpp $G/compile-cpp-flags
323 # Link binary programs
324 $B/%: $G/link-o-flags
327 # Link shared libraries
328 $L/%.so: override CFLAGS += -fPIC
329 $L/%.so: override CXXFLAGS += -fPIC
330 $L/%.so: $G/link-o-flags
333 # Create pkg-config files using a template
335 $(call replace,$(PC_VARS),$*-PC-)
337 # Run doxygen to build the documentation. It expects the first prerequisite to
338 # be the Doxyfile to use and the next prerequisites the input files. This rule
339 # is a little restrictive, but you can always make your own if it doesn't fit
343 $(call exec,(cat $<; \
344 echo "FULL_PATH_NAMES=YES"; \
345 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
346 $(wordlist 2,$(words $^),$^))"; \
347 echo "OUTPUT_DIRECTORY=$(@D)"; \
348 echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
349 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
350 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
351 echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
354 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
355 # to be set to the directory where the Sphinx's conf.py and reST source files
356 # are. This rule is a little restrictive, but you can always make your own if
357 # it doesn't fit your needs ;)
358 $D/%/sphinx-stamp: $G/sphinx-flags
359 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
360 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
361 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
362 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
365 # Install binary programs
367 $(call install_file,0755)
369 # Install system binary programs
371 $(call install_file,0755)
373 # Install pkg-config specification files
383 $(call exec,$(RM) -r $(VD),$(VD))
385 # Phony rule to uninstall all built targets (like "install", uses $(install)).
388 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
390 # These rules use the "Secondary Expansion" GNU Make feature, to allow
391 # sub-makes to add values to the special variables $(all), after this makefile
395 # Phony rule to make all the targets (sub-makefiles can append targets to build
396 # to the $(all) variable).
400 # Phony rule to install all built targets (sub-makefiles can append targets to
401 # build to the $(install) variable).
405 # Phony rule to build all documentation targets (sub-makefiles can append
406 # documentation to build to the $(doc) variable).
410 # Phony rule to build and run all test (sub-makefiles can append targets to
411 # build and run tests to the $(test) variable).
416 # Create build directory structure
417 ###################################
419 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
420 # directory structure of the project into $O. Create one symbolic link "last"
421 # to the current build directory.
423 # NOTE: the second mkdir can yield no arguments if the project don't have any
424 # subdirectories, that's why the current directory "." is included, so it
425 # won't show an error message in case of no subdirectories.
426 setup_build_dir__ := $(shell \
427 mkdir -p $O $B $L $D $(INCLUDE_DIR); \
428 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
429 $(shell find $T -type d -not -path '$(VD)*'))); \
430 test -L $(VD)/last || ln -s $F $(VD)/last )
433 # Automatic rebuilding when flags or commands changes
434 ######################################################
436 # Re-compile C files if one of this variables changes
437 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
439 # Re-compile C++ files if one of this variables changes
440 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
442 # Re-link binaries and libraries if one of this variables changes
443 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
445 # Re-build sphinx documentation if one of these variables changes
446 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
448 # Create files containing the current flags to trigger a rebuild if they change
449 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
450 $(COMPILE.c.FLAGS),C compiler or flags; )
451 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
452 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
453 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
454 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
455 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
456 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx command or flags; )
458 # Print any generated message (if verbose)
459 $(if $V,$(if $(setup_flag_files__), \
460 $(info !! Something changed: $(setup_flag_files__)re-building \