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 (if
176 # $(GCH) is non-empty) and dependencies. The pre-compiled headers are generated
177 # only if the system includes change. This function is designed to be used as
178 # a command in a rule. The first argument is the type of file to compile
179 # (typically "c" or "cpp"). What to compile and the output files are built
180 # using the automatic variables from a rule. The second argument is the base
181 # output directory (typically $O). You can add non-propagated object-specific
182 # flags defining a variable with the name of the target followed with
185 # XXX: The pre-compiled headers generation is not very useful if you include
186 # local files using #include <...>, because the system headers detection
187 # is a little simplistic now, it just parse the source file and all its
188 # dependencies searching for lines starting with "#include <" and
189 # extracting the included files from them.
192 $Vif test -f $2/$*.d; then \
194 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $2/$*.d`; \
195 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
196 if diff -q -w "$2/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
200 mv "$$tmp" "$2/$*.$1.h"; \
201 $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
202 -o "$2/$*.$1.h.gch" "$2/$*.$1.h",$2/$*.$1.h.gch); \
205 touch "$2/$*.$1.h"; \
208 $(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
209 $(if $(GCH),-include $2/$*.$1.h) $<)
212 # Link object files to build an executable. The objects files are taken from
213 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
214 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
215 # function is designed to be used as a command in a rule. The output name is
216 # taken from the rule automatic variables. If an argument is provided, it's
217 # included in the link command line. The variable LINKER is used to link the
218 # executable; for example, if you want to link a C++ executable, you should use
219 # LINKER := $(CXX). You can add non-propagated target-specific flags defining
220 # a variable with the name of the target followed with ".EXTRA_FLAGS". You can
221 # specify a non-propagated object-specific linker defining a variable with the
222 # name of the target followed with ".LINKER".
223 link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
224 $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
225 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
226 $(foreach obj,$(filter %.o,$^),$(obj)))
228 # Install a file. All arguments are optional. The first argument is the file
229 # mode (defaults to 0644). The second argument are extra flags to the install
230 # command (defaults to -D). The third argument is the source file to install
231 # (defaults to $<) and the last one is the destination (defaults to $@).
232 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
233 $(if $3,$3,$<) $(if $4,$4,$@))
235 # Concatenate variables together. The first argument is a list of variables
236 # names to concatenate. The second argument is an optional prefix for the
237 # variables and the third is the string to use as separator (" ~" if omitted).
241 # $(call varcat,A B,X_, --)
242 # Will produce something like "a -- b --"
243 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
245 # Replace variables with specified values in a template file. The first
246 # argument is a list of make variables names which will be replaced in the
247 # target file. The strings @VARNAME@ in the template file will be replaced
248 # with the value of the make $(VARNAME) variable and the result will be stored
249 # in the target file. The second (optional) argument is a prefix to add to the
250 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
251 # the template file, it will be replaced by the value of the make variable
252 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
253 # the destination file (both optional, $< and $@ are used if omitted). The
254 # fifth (optional) argument are options to pass to the substitute sed command
255 # (for example, use "g" if you want to do multiple substitutions per line).
256 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
259 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
260 # argument is the name of symbolic link to create. The link is only created if
261 # it doesn't already exist.
262 symlink_include_dir = $(shell \
263 test -L $(INCLUDE_DIR)/$1 \
264 || ln -s $C $(INCLUDE_DIR)/$1 )
266 # Create a file with flags used to trigger rebuilding when they change. The
267 # first argument is the name of the file where to store the flags, the second
268 # are the flags and the third argument is a text to be displayed if the flags
269 # have changed (optional). This should be used as a rule action or something
270 # where a shell script is expected.
271 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
272 $(if $3,test -f $1 && echo "$3";) \
275 # Include sub-directory's Build.mak. The only argument is a list of
276 # subdirectories for which Build.mak should be included. The $S directory is
277 # set properly before including each sub-directory's Build.mak and restored
279 define build_subdir_code
280 _parent__$d__dir_ := $$S
281 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
282 include $$T/$$S/Build.mak
283 S := $$(_parent__$d__dir_)
285 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
287 # Run a command through valgrind if $(VALGRIND) is non-empty. The first
288 # argument is the command to run. If $(VALGRIND) is empty, the command is
289 # executed as passed to this function. If valgrind is used, the
290 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
291 # definition for extra options that can be passed as make variables. The
292 # second argument is the name of the command to print when $V is non-empty (if
293 # omitted, the first word of the first argument is used).
294 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
295 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
296 $(if $2,$2,$(firstword $1)))
302 # Warn about everything
303 override CPPFLAGS += -Wall
305 # Use the includes directories to search for includes
306 override CPPFLAGS += -I$(INCLUDE_DIR)
308 # Let the program know where it will be installed
309 override CPPFLAGS += -DPREFIX=$(prefix)
311 # Be standard compliant
312 override CFLAGS += -std=c99 -pedantic
313 override CXXFLAGS += -std=c++98 -pedantic
315 # Use the generated library directory to for libraries
316 override LDFLAGS += -L$L -Wall
318 # Make sure the generated libraries can be found
319 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
326 override CPPFLAGS += -ggdb -DDEBUG
330 override CPPFLAGS += -O2 -DNDEBUG
334 override CPPFLAGS += -ggdb -pg --coverage
335 override LDFLAGS += -pg --coverage
339 # Automatic dependency handling
340 ################################
342 # These files are created during compilation.
343 sinclude $(shell test -d $O && find $O -name '*.d')
350 $O/%.o: $T/%.c $G/compile-c-flags
353 # Compile C++ objects
354 $O/%.o: $T/%.cpp $G/compile-cpp-flags
355 $(call compile,cpp,$O)
357 # Link binary programs
358 $B/%: $G/link-o-flags
361 # Link shared libraries
362 $L/%.so: override CFLAGS += -fPIC
363 $L/%.so: override CXXFLAGS += -fPIC
364 $L/%.so: $G/link-o-flags
367 # Create pkg-config files using a template
369 $(call replace,$(PC_VARS),$*-PC-)
371 # Run doxygen to build the documentation. It expects the first prerequisite to
372 # be the Doxyfile to use and the next prerequisites the input files. You
373 # can override Doxyfile configuration variables by defining a DOXYGEN_VARS
374 # Make variable for this rule. For example, defining:
375 # PROJECT_NAME := myproj
376 # DOXYGEN_VARS := PROJECT_NAME
377 # You can override Doxygen's PROJECT_NAME configuration option. Optionally, you
378 # can define DOXYGEN_VARS_PREFIX too, to avoid polluting your Makefile with
379 # Doxygen variables. For example:
380 # DOXY.PROJECT_NAME := myproj
381 # DOXYGEN_VARS_PREFIX := DOXY.
382 # DOXYGEN_VARS := PROJECT_NAME
383 # This rule might be still a little restrictive, but you can always make your
384 # own if it doesn't fit your needs ;)
387 $(call exec,(cat $<; \
388 echo "FULL_PATH_NAMES=YES"; \
389 $(if $(filter INPUT,$(DOXYGEN_VARS)),,\
390 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
391 $(wordlist 2,$(words $^),$^))";) \
392 echo "OUTPUT_DIRECTORY=$(@D)"; \
393 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
394 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
395 echo "QUIET=$(if $V,YES,NO)"; \
396 $(foreach v,$(DOXYGEN_VARS),\
397 echo '$v=$($(DOXYGEN_VARS_PREFIX)$v)';) \
398 ) | doxygen -,$(@D),doxygen)
401 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
402 # to be set to the directory where the Sphinx's conf.py and reST source files
403 # are. This rule is a little restrictive, but you can always make your own if
404 # it doesn't fit your needs ;)
405 $D/%/sphinx-stamp: $G/sphinx-flags
406 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
407 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
408 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
409 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
412 # Install binary programs
414 $(call install_file,0755)
416 # Install system binary programs
418 $(call install_file,0755)
420 # Install pkg-config specification files
430 $(call exec,$(RM) -r $(VD),$(VD))
432 # Phony rule to uninstall all built targets (like "install", uses $(install)).
435 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
437 # These rules use the "Secondary Expansion" GNU Make feature, to allow
438 # sub-makes to add values to the special variables $(all), after this makefile
442 # Phony rule to make all the targets (sub-makefiles can append targets to build
443 # to the $(all) variable).
447 # Phony rule to install all built targets (sub-makefiles can append targets to
448 # build to the $(install) variable).
452 # Phony rule to build all documentation targets (sub-makefiles can append
453 # documentation to build to the $(doc) variable).
457 # Phony rule to build and run all test (sub-makefiles can append targets to
458 # build and run tests to the $(test) variable).
463 # Create build directory structure
464 ###################################
466 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
467 # directory structure of the project into $O. Create one symbolic link "last"
468 # to the current build directory.
470 # NOTE: the second mkdir can yield no arguments if the project don't have any
471 # subdirectories, that's why the current directory "." is included, so it
472 # won't show an error message in case of no subdirectories.
473 setup_build_dir__ := $(shell \
474 mkdir -p $O $B $L $D $(INCLUDE_DIR) $(addprefix $O,$(patsubst $T%,%,\
475 $(shell find $T -type d $(foreach d,$(BUILD_DIR_EXCLUDE), \
476 -not -path '*/$d' -not -path '*/$d/*')))); \
477 test -L $(VD)/last || ln -s $F $(VD)/last )
480 # Automatic rebuilding when flags or commands changes
481 ######################################################
483 # Re-compile C files if one of this variables changes
484 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
486 # Re-compile C++ files if one of this variables changes
487 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
489 # Re-link binaries and libraries if one of this variables changes
490 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
492 # Re-build sphinx documentation if one of these variables changes
493 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
495 # Create files containing the current flags to trigger a rebuild if they change
496 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
497 $(COMPILE.c.FLAGS),C compiler or flags; )
498 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
499 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
500 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
501 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
502 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
503 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx command or flags; )
505 # Print any generated message (if verbose)
506 $(if $V,$(if $(setup_flag_files__), \
507 $(info !! Something changed: $(setup_flag_files__)re-building \