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 # If $V is non-empty, colored output is used if $(COLOR) is non-empty too
20 # ANSI color used for the command if $(COLOR) is non-empty
21 # The color is composed with 2 numbers separated by ;
22 # The first is the style. 00 is normal, 01 is bold, 04 is underline, 05 blinks,
24 # The second is the color: 30 dark gray/black, 31 red, 32 green, 33 yellow, 34
25 # blue, 35 magenta, 36 cyan and 37 white.
26 # If empty, no special color is used.
29 # ANSI color used for the argument if $(COLOR) is non-empty
30 # See COLOR_CMD comment for details.
33 # Flavor (variant), should be one of "dbg", "opt" or "cov"
36 # Use C++ linker by default
39 # Default mode used to install files
42 # Default install flags
45 # Use pre-compiled headers if non-empty
48 # If non-empty, use valgrind to run commands via the "valgrind" function
51 # Options to pass to valgrind; if the variable $(VALGRIND_SUPP) is non-empty
52 # it will be used as a suppressions file.
53 VALGRIND_CMD ?= valgrind --tool=memcheck --leak-check=yes --db-attach=no \
54 --num-callers=24 --leak-resolution=high --track-fds=yes \
56 $(if $V,--log-file=$<.valgrind.log) \
57 $(if $(VALGRIND_SUPP),--suppressions=$(VALGRIND_SUPP))
59 # Command to generate Sphinx based documentation
60 SPHINX ?= sphinx-build
62 # Format to build using Sphinx (html, pickle, htmlhelp, latex, changes or
63 # linkcheck; see sphinx-build docs for details)
66 # Paper size for Sphinx LaTeX output (a4, letter, etc.)
67 SPHINX_PAPERSIZE ?= a4
73 # Base directory where to install files (can be overridden, should be absolute)
76 # Path to a complete alternative environment, usually a jail, or an installed
77 # system mounted elsewhere than /.
80 # Use absolute paths to avoid problems with automatic dependencies when
81 # building from subdirectories
84 # Name of the current directory, relative to $T
85 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
87 # Base directory where to put variants (Variants Directory)
90 # Generated files top directory
93 # Objects (and other garbage like pre-compiled headers and dependency files)
100 # Libraries directory
103 # Documentation directory
106 # Installation directory
107 I := $(DESTDIR)$(prefix)
110 INCLUDE_DIR ?= $G/include
112 # Directory of the current makefile (this might not be the same as $(CURDIR)
113 # This variable is "lazy" because $S changes all the time, so it should be
114 # evaluated in the context where $C is used, not here.
121 # Compare two strings, if they are the same, returns the string, if not,
123 eq = $(if $(subst $1,,$2),,$1)
125 # Find sources files and get the corresponding object names. The first
126 # argument should be the sources extension ("c" or "cpp" typically). The
127 # second argument is where to search for the sources ($C if omitted). The
128 # resulting files will always have the suffix "o" and the directory rewritten
129 # to match the directory structure (from $T) but in the $O directory. For
130 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
131 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
132 # $(call find_objects,c)
133 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
134 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
135 find $(if $2,$2,$C) -name '*.$1'))
137 # Find files and get the their file names relative to another directory. The
138 # first argument should be the files suffix (".h" or ".cpp" for example). The
139 # second argument is a directory rewrite, the matched files will be rewriten to
140 # be in the directory specified in this argument (it defaults to the third
141 # argument if omitted). The third argument is where to search for the files
143 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
144 $(shell find $(if $3,$3,$C) -name '*$1'))
146 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
147 # directory, so it can be displayed as if it were a relative directory. Take
148 # just one argument, the file name.
149 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
150 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
151 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
152 $(call abbr_helper,$1)))
154 # Execute a command printing a nice message if $V is @.
155 # The first argument is mandatory and it's the command to execute. The second
156 # and third arguments are optional and are the target name and command name to
158 vexec_pc = $(if $1,\033[$1m%s\033[00m,%s)
159 vexec_p = $(if $(COLOR), \
160 ' $(call vexec_pc,$(COLOR_CMD)) $(call vexec_pc,$(COLOR_ARG))\n', \
162 vexec = $(if $V,printf $(vexec_p) \
163 '$(call abbr,$(if $3,$(strip $3),$(firstword $1)))' \
164 '$(call abbr,$(if $2,$(strip $2),$@))' ; )$1
166 # Same as vexec but it silence the echo command (prepending a @ if $V).
167 exec = $V$(call vexec,$1,$2,$3)
169 # Compile a source file to an object, generating pre-compiled headers and
170 # dependencies. The pre-compiled headers are generated only if the system
171 # includes change. This function is designed to be used as a command in a rule.
172 # The first argument is the type of file to compile (typically "c" or "cpp").
173 # What to compile and the output files are built using the automatic variables
174 # from a rule. The second argument is the base output directory (typically
175 # $O). You can add non-propagated object-specific flags defining a variable
176 # with the name of the target followed with ".EXTRA_FLAGS".
179 $Vif test -f $2/$*.d; then \
181 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $2/$*.d`; \
182 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
183 if diff -q -w "$2/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
187 mv "$$tmp" "$2/$*.$1.h"; \
188 $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
189 -o "$2/$*.$1.h.gch" "$2/$*.$1.h",$2/$*.$1.h.gch); \
192 touch "$2/$*.$1.h"; \
195 $(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
196 $(if $(GCH),-include $2/$*.$1.h) $<)
199 # Link object files to build an executable. The objects files are taken from
200 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
201 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
202 # function is designed to be used as a command in a rule. The output name is
203 # taken from the rule automatic variables. If an argument is provided, it's
204 # included in the link command line. The variable LINKER is used to link the
205 # executable; for example, if you want to link a C++ executable, you should use
206 # LINKER := $(CXX). You can add non-propagated target-specific flags defining
207 # a variable with the name of the target followed with ".EXTRA_FLAGS". You can
208 # specify a non-propagated object-specific linker defining a variable with the
209 # name of the target followed with ".LINKER".
210 link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
211 $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
212 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
213 $(foreach obj,$(filter %.o,$^),$(obj)))
215 # Install a file. All arguments are optional. The first argument is the file
216 # mode (defaults to 0644). The second argument are extra flags to the install
217 # command (defaults to -D). The third argument is the source file to install
218 # (defaults to $<) and the last one is the destination (defaults to $@).
219 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
220 $(if $3,$3,$<) $(if $4,$4,$@))
222 # Concatenate variables together. The first argument is a list of variables
223 # names to concatenate. The second argument is an optional prefix for the
224 # variables and the third is the string to use as separator (" ~" if omitted).
228 # $(call varcat,A B,X_, --)
229 # Will produce something like "a -- b --"
230 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
232 # Replace variables with specified values in a template file. The first
233 # argument is a list of make variables names which will be replaced in the
234 # target file. The strings @VARNAME@ in the template file will be replaced
235 # with the value of the make $(VARNAME) variable and the result will be stored
236 # in the target file. The second (optional) argument is a prefix to add to the
237 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
238 # the template file, it will be replaced by the value of the make variable
239 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
240 # the destination file (both optional, $< and $@ are used if omitted). The
241 # fifth (optional) argument are options to pass to the substitute sed command
242 # (for example, use "g" if you want to do multiple substitutions per line).
243 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
246 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
247 # argument is the name of symbolic link to create. The link is only created if
248 # it doesn't already exist.
249 symlink_include_dir = $(shell \
250 test -L $(INCLUDE_DIR)/$1 \
251 || ln -s $C $(INCLUDE_DIR)/$1 )
253 # Create a file with flags used to trigger rebuilding when they change. The
254 # first argument is the name of the file where to store the flags, the second
255 # are the flags and the third argument is a text to be displayed if the flags
256 # have changed (optional). This should be used as a rule action or something
257 # where a shell script is expected.
258 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
259 $(if $3,test -f $1 && echo "$3";) \
262 # Include sub-directory's Build.mak. The only argument is a list of
263 # subdirectories for which Build.mak should be included. The $S directory is
264 # set properly before including each sub-directory's Build.mak and restored
266 define build_subdir_code
267 _parent__$d__dir_ := $$S
268 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
269 include $$T/$$S/Build.mak
270 S := $$(_parent__$d__dir_)
272 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
274 # Run a command through valgrind if $(VALGRIND) is non-empty. The first
275 # argument is the command to run. If $(VALGRIND) is empty, the command is
276 # executed as passed to this function. If valgrind is used, the
277 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
278 # definition for extra options that can be passed as make variables. The
279 # second argument is the name of the command to print when $V is non-empty (if
280 # omitted, the first word of the first argument is used).
281 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
282 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
283 $(if $2,$2,$(firstword $1)))
289 # Warn about everything
290 override CPPFLAGS += -Wall
292 # Use the includes directories to search for includes
293 override CPPFLAGS += -I$(INCLUDE_DIR)
295 # Let the program know where it will be installed
296 override CPPFLAGS += -DPREFIX=$(prefix)
298 # Be standard compliant
299 override CFLAGS += -std=c99 -pedantic
300 override CXXFLAGS += -std=c++98 -pedantic
302 # Use the generated library directory to for libraries
303 override LDFLAGS += -L$L -Wall
305 # Make sure the generated libraries can be found
306 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
313 override CPPFLAGS += -ggdb -DDEBUG
317 override CPPFLAGS += -O2 -DNDEBUG
321 override CPPFLAGS += -ggdb -pg --coverage
322 override LDFLAGS += -pg --coverage
326 # Automatic dependency handling
327 ################################
329 # These files are created during compilation.
330 sinclude $(shell test -d $O && find $O -name '*.d')
337 $O/%.o: $T/%.c $G/compile-c-flags
340 # Compile C++ objects
341 $O/%.o: $T/%.cpp $G/compile-cpp-flags
342 $(call compile,cpp,$O)
344 # Link binary programs
345 $B/%: $G/link-o-flags
348 # Link shared libraries
349 $L/%.so: override CFLAGS += -fPIC
350 $L/%.so: override CXXFLAGS += -fPIC
351 $L/%.so: $G/link-o-flags
354 # Create pkg-config files using a template
356 $(call replace,$(PC_VARS),$*-PC-)
358 # Run doxygen to build the documentation. It expects the first prerequisite to
359 # be the Doxyfile to use and the next prerequisites the input files. This rule
360 # is a little restrictive, but you can always make your own if it doesn't fit
364 $(call exec,(cat $<; \
365 echo "FULL_PATH_NAMES=YES"; \
366 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
367 $(wordlist 2,$(words $^),$^))"; \
368 echo "OUTPUT_DIRECTORY=$(@D)"; \
369 echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
370 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
371 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
372 echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
375 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
376 # to be set to the directory where the Sphinx's conf.py and reST source files
377 # are. This rule is a little restrictive, but you can always make your own if
378 # it doesn't fit your needs ;)
379 $D/%/sphinx-stamp: $G/sphinx-flags
380 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
381 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
382 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
383 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
386 # Install binary programs
388 $(call install_file,0755)
390 # Install system binary programs
392 $(call install_file,0755)
394 # Install pkg-config specification files
404 $(call exec,$(RM) -r $(VD),$(VD))
406 # Phony rule to uninstall all built targets (like "install", uses $(install)).
409 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
411 # These rules use the "Secondary Expansion" GNU Make feature, to allow
412 # sub-makes to add values to the special variables $(all), after this makefile
416 # Phony rule to make all the targets (sub-makefiles can append targets to build
417 # to the $(all) variable).
421 # Phony rule to install all built targets (sub-makefiles can append targets to
422 # build to the $(install) variable).
426 # Phony rule to build all documentation targets (sub-makefiles can append
427 # documentation to build to the $(doc) variable).
431 # Phony rule to build and run all test (sub-makefiles can append targets to
432 # build and run tests to the $(test) variable).
437 # Create build directory structure
438 ###################################
440 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
441 # directory structure of the project into $O. Create one symbolic link "last"
442 # to the current build directory.
444 # NOTE: the second mkdir can yield no arguments if the project don't have any
445 # subdirectories, that's why the current directory "." is included, so it
446 # won't show an error message in case of no subdirectories.
447 setup_build_dir__ := $(shell \
448 mkdir -p $O $B $L $D $(INCLUDE_DIR); \
449 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
450 $(shell find $T -type d -not -path '$(VD)*'))); \
451 test -L $(VD)/last || ln -s $F $(VD)/last )
454 # Automatic rebuilding when flags or commands changes
455 ######################################################
457 # Re-compile C files if one of this variables changes
458 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
460 # Re-compile C++ files if one of this variables changes
461 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
463 # Re-link binaries and libraries if one of this variables changes
464 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
466 # Re-build sphinx documentation if one of these variables changes
467 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
469 # Create files containing the current flags to trigger a rebuild if they change
470 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
471 $(COMPILE.c.FLAGS),C compiler or flags; )
472 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
473 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
474 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
475 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
476 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
477 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx command or flags; )
479 # Print any generated message (if verbose)
480 $(if $V,$(if $(setup_flag_files__), \
481 $(info !! Something changed: $(setup_flag_files__)re-building \