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
69 # Name of the build directory (to use when excluding some paths)
70 BUILD_DIR_NAME ?= build
72 # Directories to exclude from the build directory tree replication
73 BUILD_DIR_EXCLUDE ?= $(BUILD_DIR_NAME) .git .hg .bzr _darcs .svn CVS
79 # Base directory where to install files (can be overridden, should be absolute)
82 # Path to a complete alternative environment, usually a jail, or an installed
83 # system mounted elsewhere than /.
86 # Use absolute paths to avoid problems with automatic dependencies when
87 # building from subdirectories
90 # Name of the current directory, relative to $T
91 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
93 # Base directory where to put variants (Variants Directory)
94 VD ?= $T/$(BUILD_DIR_NAME)
96 # Generated files top directory
99 # Objects (and other garbage like pre-compiled headers and dependency files)
106 # Libraries directory
109 # Documentation directory
112 # Installation directory
113 I := $(DESTDIR)$(prefix)
116 INCLUDE_DIR ?= $G/include
118 # Directory of the current makefile (this might not be the same as $(CURDIR)
119 # This variable is "lazy" because $S changes all the time, so it should be
120 # evaluated in the context where $C is used, not here.
127 # Compare two strings, if they are the same, returns the string, if not,
129 eq = $(if $(subst $1,,$2),,$1)
131 # Find sources files and get the corresponding object names. The first
132 # argument should be the sources extension ("c" or "cpp" typically). The
133 # second argument is where to search for the sources ($C if omitted). The
134 # resulting files will always have the suffix "o" and the directory rewritten
135 # to match the directory structure (from $T) but in the $O directory. For
136 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
137 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
138 # $(call find_objects,c)
139 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
140 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
141 find $(if $2,$2,$C) -name '*.$1'))
143 # Find files and get the their file names relative to another directory. The
144 # first argument should be the files suffix (".h" or ".cpp" for example). The
145 # second argument is a directory rewrite, the matched files will be rewriten to
146 # be in the directory specified in this argument (it defaults to the third
147 # argument if omitted). The third argument is where to search for the files
149 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
150 $(shell find $(if $3,$3,$C) -name '*$1'))
152 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
153 # directory, so it can be displayed as if it were a relative directory. Take
154 # just one argument, the file name.
155 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
156 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
157 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
158 $(call abbr_helper,$1)))
160 # Execute a command printing a nice message if $V is @.
161 # The first argument is mandatory and it's the command to execute. The second
162 # and third arguments are optional and are the target name and command name to
164 vexec_pc = $(if $1,\033[$1m%s\033[00m,%s)
165 vexec_p = $(if $(COLOR), \
166 ' $(call vexec_pc,$(COLOR_CMD)) $(call vexec_pc,$(COLOR_ARG))\n', \
168 vexec = $(if $V,printf $(vexec_p) \
169 '$(call abbr,$(if $3,$(strip $3),$(firstword $1)))' \
170 '$(call abbr,$(if $2,$(strip $2),$@))' ; )$1
172 # Same as vexec but it silence the echo command (prepending a @ if $V).
173 exec = $V$(call vexec,$1,$2,$3)
175 # Compile a source file to an object, generating pre-compiled headers and
176 # dependencies. The pre-compiled headers are generated only if the system
177 # includes change. This function is designed to be used as a command in a rule.
178 # The first argument is the type of file to compile (typically "c" or "cpp").
179 # What to compile and the output files are built using the automatic variables
180 # from a rule. The second argument is the base output directory (typically
181 # $O). You can add non-propagated object-specific flags defining a variable
182 # with the name of the target followed with ".EXTRA_FLAGS".
185 $Vif test -f $2/$*.d; then \
187 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $2/$*.d`; \
188 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
189 if diff -q -w "$2/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
193 mv "$$tmp" "$2/$*.$1.h"; \
194 $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
195 -o "$2/$*.$1.h.gch" "$2/$*.$1.h",$2/$*.$1.h.gch); \
198 touch "$2/$*.$1.h"; \
201 $(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
202 $(if $(GCH),-include $2/$*.$1.h) $<)
205 # Link object files to build an executable. The objects files are taken from
206 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
207 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
208 # function is designed to be used as a command in a rule. The output name is
209 # taken from the rule automatic variables. If an argument is provided, it's
210 # included in the link command line. The variable LINKER is used to link the
211 # executable; for example, if you want to link a C++ executable, you should use
212 # LINKER := $(CXX). You can add non-propagated target-specific flags defining
213 # a variable with the name of the target followed with ".EXTRA_FLAGS". You can
214 # specify a non-propagated object-specific linker defining a variable with the
215 # name of the target followed with ".LINKER".
216 link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
217 $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
218 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
219 $(foreach obj,$(filter %.o,$^),$(obj)))
221 # Install a file. All arguments are optional. The first argument is the file
222 # mode (defaults to 0644). The second argument are extra flags to the install
223 # command (defaults to -D). The third argument is the source file to install
224 # (defaults to $<) and the last one is the destination (defaults to $@).
225 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
226 $(if $3,$3,$<) $(if $4,$4,$@))
228 # Concatenate variables together. The first argument is a list of variables
229 # names to concatenate. The second argument is an optional prefix for the
230 # variables and the third is the string to use as separator (" ~" if omitted).
234 # $(call varcat,A B,X_, --)
235 # Will produce something like "a -- b --"
236 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
238 # Replace variables with specified values in a template file. The first
239 # argument is a list of make variables names which will be replaced in the
240 # target file. The strings @VARNAME@ in the template file will be replaced
241 # with the value of the make $(VARNAME) variable and the result will be stored
242 # in the target file. The second (optional) argument is a prefix to add to the
243 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
244 # the template file, it will be replaced by the value of the make variable
245 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
246 # the destination file (both optional, $< and $@ are used if omitted). The
247 # fifth (optional) argument are options to pass to the substitute sed command
248 # (for example, use "g" if you want to do multiple substitutions per line).
249 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
252 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
253 # argument is the name of symbolic link to create. The link is only created if
254 # it doesn't already exist.
255 symlink_include_dir = $(shell \
256 test -L $(INCLUDE_DIR)/$1 \
257 || ln -s $C $(INCLUDE_DIR)/$1 )
259 # Create a file with flags used to trigger rebuilding when they change. The
260 # first argument is the name of the file where to store the flags, the second
261 # are the flags and the third argument is a text to be displayed if the flags
262 # have changed (optional). This should be used as a rule action or something
263 # where a shell script is expected.
264 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
265 $(if $3,test -f $1 && echo "$3";) \
268 # Include sub-directory's Build.mak. The only argument is a list of
269 # subdirectories for which Build.mak should be included. The $S directory is
270 # set properly before including each sub-directory's Build.mak and restored
272 define build_subdir_code
273 _parent__$d__dir_ := $$S
274 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
275 include $$T/$$S/Build.mak
276 S := $$(_parent__$d__dir_)
278 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
280 # Run a command through valgrind if $(VALGRIND) is non-empty. The first
281 # argument is the command to run. If $(VALGRIND) is empty, the command is
282 # executed as passed to this function. If valgrind is used, the
283 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
284 # definition for extra options that can be passed as make variables. The
285 # second argument is the name of the command to print when $V is non-empty (if
286 # omitted, the first word of the first argument is used).
287 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
288 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
289 $(if $2,$2,$(firstword $1)))
295 # Warn about everything
296 override CPPFLAGS += -Wall
298 # Use the includes directories to search for includes
299 override CPPFLAGS += -I$(INCLUDE_DIR)
301 # Let the program know where it will be installed
302 override CPPFLAGS += -DPREFIX=$(prefix)
304 # Be standard compliant
305 override CFLAGS += -std=c99 -pedantic
306 override CXXFLAGS += -std=c++98 -pedantic
308 # Use the generated library directory to for libraries
309 override LDFLAGS += -L$L -Wall
311 # Make sure the generated libraries can be found
312 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
319 override CPPFLAGS += -ggdb -DDEBUG
323 override CPPFLAGS += -O2 -DNDEBUG
327 override CPPFLAGS += -ggdb -pg --coverage
328 override LDFLAGS += -pg --coverage
332 # Automatic dependency handling
333 ################################
335 # These files are created during compilation.
336 sinclude $(shell test -d $O && find $O -name '*.d')
343 $O/%.o: $T/%.c $G/compile-c-flags
346 # Compile C++ objects
347 $O/%.o: $T/%.cpp $G/compile-cpp-flags
348 $(call compile,cpp,$O)
350 # Link binary programs
351 $B/%: $G/link-o-flags
354 # Link shared libraries
355 $L/%.so: override CFLAGS += -fPIC
356 $L/%.so: override CXXFLAGS += -fPIC
357 $L/%.so: $G/link-o-flags
360 # Create pkg-config files using a template
362 $(call replace,$(PC_VARS),$*-PC-)
364 # Run doxygen to build the documentation. It expects the first prerequisite to
365 # be the Doxyfile to use and the next prerequisites the input files. This rule
366 # is a little restrictive, but you can always make your own if it doesn't fit
370 $(call exec,(cat $<; \
371 echo "FULL_PATH_NAMES=YES"; \
372 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
373 $(wordlist 2,$(words $^),$^))"; \
374 echo "OUTPUT_DIRECTORY=$(@D)"; \
375 echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
376 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
377 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
378 echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
381 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
382 # to be set to the directory where the Sphinx's conf.py and reST source files
383 # are. This rule is a little restrictive, but you can always make your own if
384 # it doesn't fit your needs ;)
385 $D/%/sphinx-stamp: $G/sphinx-flags
386 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
387 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
388 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
389 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
392 # Install binary programs
394 $(call install_file,0755)
396 # Install system binary programs
398 $(call install_file,0755)
400 # Install pkg-config specification files
410 $(call exec,$(RM) -r $(VD),$(VD))
412 # Phony rule to uninstall all built targets (like "install", uses $(install)).
415 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
417 # These rules use the "Secondary Expansion" GNU Make feature, to allow
418 # sub-makes to add values to the special variables $(all), after this makefile
422 # Phony rule to make all the targets (sub-makefiles can append targets to build
423 # to the $(all) variable).
427 # Phony rule to install all built targets (sub-makefiles can append targets to
428 # build to the $(install) variable).
432 # Phony rule to build all documentation targets (sub-makefiles can append
433 # documentation to build to the $(doc) variable).
437 # Phony rule to build and run all test (sub-makefiles can append targets to
438 # build and run tests to the $(test) variable).
443 # Create build directory structure
444 ###################################
446 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
447 # directory structure of the project into $O. Create one symbolic link "last"
448 # to the current build directory.
450 # NOTE: the second mkdir can yield no arguments if the project don't have any
451 # subdirectories, that's why the current directory "." is included, so it
452 # won't show an error message in case of no subdirectories.
453 setup_build_dir__ := $(shell \
454 mkdir -p $O $B $L $D $(INCLUDE_DIR) $(addprefix $O,$(patsubst $T%,%,\
455 $(shell find $T -type d $(foreach d,$(BUILD_DIR_EXCLUDE), \
456 -not -path '*/$d' -not -path '*/$d/*')))); \
457 test -L $(VD)/last || ln -s $F $(VD)/last )
460 # Automatic rebuilding when flags or commands changes
461 ######################################################
463 # Re-compile C files if one of this variables changes
464 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
466 # Re-compile C++ files if one of this variables changes
467 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
469 # Re-link binaries and libraries if one of this variables changes
470 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
472 # Re-build sphinx documentation if one of these variables changes
473 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
475 # Create files containing the current flags to trigger a rebuild if they change
476 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
477 $(COMPILE.c.FLAGS),C compiler or flags; )
478 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
479 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
480 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
481 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
482 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
483 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx command or flags; )
485 # Print any generated message (if verbose)
486 $(if $V,$(if $(setup_flag_files__), \
487 $(info !! Something changed: $(setup_flag_files__)re-building \