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 # ANSI color used for the warnings if $(COLOR) is non-empty
34 # See COLOR_CMD comment for details.
37 # Flavor (variant), should be one of "dbg", "opt" or "cov"
40 # Use C++ linker by default
43 # Default mode used to install files
46 # Default install flags
49 # Use pre-compiled headers if non-empty
52 # If non-empty, use valgrind to run commands via the "valgrind" function
55 # Options to pass to valgrind; if the variable $(VALGRIND_SUPP) is non-empty
56 # it will be used as a suppressions file.
57 VALGRIND_CMD ?= valgrind --tool=memcheck --leak-check=yes --db-attach=no \
58 --num-callers=24 --leak-resolution=high --track-fds=yes \
60 $(if $V,--log-file=$<.valgrind.log) \
61 $(if $(VALGRIND_SUPP),--suppressions=$(VALGRIND_SUPP))
63 # Command to generate Sphinx based documentation
64 SPHINX ?= sphinx-build
66 # Format to build using Sphinx (html, pickle, htmlhelp, latex, changes or
67 # linkcheck; see sphinx-build docs for details)
70 # Paper size for Sphinx LaTeX output (a4, letter, etc.)
71 SPHINX_PAPERSIZE ?= a4
73 # Name of the build directory (to use when excluding some paths)
74 BUILD_DIR_NAME ?= build
76 # Directories to exclude from the build directory tree replication
77 BUILD_DIR_EXCLUDE ?= $(BUILD_DIR_NAME) .git .hg .bzr _darcs .svn CVS
83 # Base directory where to install files (can be overridden, should be absolute)
86 # Path to a complete alternative environment, usually a jail, or an installed
87 # system mounted elsewhere than /.
90 # Use absolute paths to avoid problems with automatic dependencies when
91 # building from subdirectories
94 # Name of the current directory, relative to $T
95 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
97 # Base directory where to put variants (Variants Directory)
98 VD ?= $T/$(BUILD_DIR_NAME)
100 # Generated files top directory
103 # Objects (and other garbage like pre-compiled headers and dependency files)
110 # Libraries directory
113 # Documentation directory
116 # Installation directory
117 I := $(DESTDIR)$(prefix)
120 INCLUDE_DIR ?= $G/include
122 # Directory of the current makefile (this might not be the same as $(CURDIR)
123 # This variable is "lazy" because $S changes all the time, so it should be
124 # evaluated in the context where $C is used, not here.
131 # Compare two strings, if they are the same, returns the string, if not,
133 eq = $(if $(subst $1,,$2),,$1)
135 # Find sources files and get the corresponding object names. The first
136 # argument should be the sources extension ("c" or "cpp" typically). The
137 # second argument is where to search for the sources ($C if omitted). The
138 # resulting files will always have the suffix "o" and the directory rewritten
139 # to match the directory structure (from $T) but in the $O directory. For
140 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
141 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
142 # $(call find_objects,c)
143 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
144 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
145 find $(if $2,$2,$C) -name '*.$1'))
147 # Find files and get the their file names relative to another directory. The
148 # first argument should be the files suffix (".h" or ".cpp" for example). The
149 # second argument is a directory rewrite, the matched files will be rewriten to
150 # be in the directory specified in this argument (it defaults to the third
151 # argument if omitted). The third argument is where to search for the files
153 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
154 $(shell find $(if $3,$3,$C) -name '*$1'))
156 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
157 # directory, so it can be displayed as if it were a relative directory. Take
158 # just one argument, the file name.
159 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
160 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
161 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
162 $(call abbr_helper,$1)))
164 # Execute a command printing a nice message if $V is @.
165 # The first argument is mandatory and it's the command to execute. The second
166 # and third arguments are optional and are the target name and command name to
168 vexec_pc = $(if $1,\033[$1m%s\033[00m,%s)
169 vexec_p = $(if $(COLOR), \
170 ' $(call vexec_pc,$(COLOR_CMD)) $(call vexec_pc,$(COLOR_ARG))\n', \
172 vexec = $(if $V,printf $(vexec_p) \
173 '$(call abbr,$(if $3,$(strip $3),$(firstword $1)))' \
174 '$(call abbr,$(if $2,$(strip $2),$@))' ; )$1
176 # Same as vexec but it silence the echo command (prepending a @ if $V).
177 exec = $V$(call vexec,$1,$2,$3)
179 # Compile a source file to an object, generating pre-compiled headers (if
180 # $(GCH) is non-empty) and dependencies. The pre-compiled headers are generated
181 # only if the system includes change. This function is designed to be used as
182 # a command in a rule. The first argument is the type of file to compile
183 # (typically "c" or "cpp"). What to compile and the output files are built
184 # using the automatic variables from a rule. The second argument is the base
185 # output directory (typically $O). You can add non-propagated object-specific
186 # flags defining a variable with the name of the target followed with
189 # XXX: The pre-compiled headers generation is not very useful if you include
190 # local files using #include <...>, because the system headers detection
191 # is a little simplistic now, it just parse the source file and all its
192 # dependencies searching for lines starting with "#include <" and
193 # extracting the included files from them.
196 $Vif test -f $2/$*.d; then \
198 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $2/$*.d`; \
199 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
200 if diff -q -w "$2/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
204 mv "$$tmp" "$2/$*.$1.h"; \
205 $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
206 -o "$2/$*.$1.h.gch" "$2/$*.$1.h",$2/$*.$1.h.gch); \
209 touch "$2/$*.$1.h"; \
212 $(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
213 $(if $(GCH),-include $2/$*.$1.h) $<)
216 # Link object files to build an executable. The objects files are taken from
217 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
218 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
219 # function is designed to be used as a command in a rule. The output name is
220 # taken from the rule automatic variables. If an argument is provided, it's
221 # included in the link command line. The variable LINKER is used to link the
222 # executable; for example, if you want to link a C++ executable, you should use
223 # LINKER := $(CXX). You can add non-propagated target-specific flags defining
224 # a variable with the name of the target followed with ".EXTRA_FLAGS". You can
225 # specify a non-propagated object-specific linker defining a variable with the
226 # name of the target followed with ".LINKER".
227 link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
228 $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
229 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
230 $(foreach obj,$(filter %.o,$^),$(obj)))
232 # Install a file. All arguments are optional. The first argument is the file
233 # mode (defaults to 0644). The second argument are extra flags to the install
234 # command (defaults to -D). The third argument is the source file to install
235 # (defaults to $<) and the last one is the destination (defaults to $@).
236 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
237 $(if $3,$3,$<) $(if $4,$4,$@))
239 # Concatenate variables together. The first argument is a list of variables
240 # names to concatenate. The second argument is an optional prefix for the
241 # variables and the third is the string to use as separator (" ~" if omitted).
245 # $(call varcat,A B,X_, --)
246 # Will produce something like "a -- b --"
247 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
249 # Replace variables with specified values in a template file. The first
250 # argument is a list of make variables names which will be replaced in the
251 # target file. The strings @VARNAME@ in the template file will be replaced
252 # with the value of the make $(VARNAME) variable and the result will be stored
253 # in the target file. The second (optional) argument is a prefix to add to the
254 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
255 # the template file, it will be replaced by the value of the make variable
256 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
257 # the destination file (both optional, $< and $@ are used if omitted). The
258 # fifth (optional) argument are options to pass to the substitute sed command
259 # (for example, use "g" if you want to do multiple substitutions per line).
260 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
263 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
264 # argument is the name of symbolic link to create. The link is only created if
265 # it doesn't already exist.
266 symlink_include_dir = $(shell \
267 test -L $(INCLUDE_DIR)/$1 \
268 || ln -s $C $(INCLUDE_DIR)/$1 )
270 # Create a file with flags used to trigger rebuilding when they change. The
271 # first argument is the name of the file where to store the flags, the second
272 # are the flags and the third argument is a text to be displayed if the flags
273 # have changed (optional). This should be used as a rule action or something
274 # where a shell script is expected.
275 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
276 $(if $3,test -f $1 && echo "$(if $(COLOR),$(if $(COLOR_WARN),\
277 \033[$(COLOR_WARN)m$3\033[00m,$3),$3);";) \
280 # Include sub-directory's Build.mak. The only argument is a list of
281 # subdirectories for which Build.mak should be included. The $S directory is
282 # set properly before including each sub-directory's Build.mak and restored
284 define build_subdir_code
285 _parent__$d__dir_ := $$S
286 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
287 include $$T/$$S/Build.mak
288 S := $$(_parent__$d__dir_)
290 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
292 # Run a command through valgrind if $(VALGRIND) is non-empty. The first
293 # argument is the command to run. If $(VALGRIND) is empty, the command is
294 # executed as passed to this function. If valgrind is used, the
295 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
296 # definition for extra options that can be passed as make variables. The
297 # second argument is the name of the command to print when $V is non-empty (if
298 # omitted, the first word of the first argument is used).
299 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
300 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
301 $(if $2,$2,$(firstword $1)))
307 # Warn about everything
308 override CPPFLAGS += -Wall
310 # Use the includes directories to search for includes
311 override CPPFLAGS += -I$(INCLUDE_DIR)
313 # Let the program know where it will be installed
314 override CPPFLAGS += -DPREFIX=$(prefix)
316 # Be standard compliant
317 override CFLAGS += -std=c99 -pedantic
318 override CXXFLAGS += -std=c++98 -pedantic
320 # Use the generated library directory to for libraries
321 override LDFLAGS += -L$L -Wall
323 # Make sure the generated libraries can be found
324 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
331 override CPPFLAGS += -ggdb -DDEBUG
335 override CPPFLAGS += -O2 -DNDEBUG
339 override CPPFLAGS += -ggdb -pg --coverage
340 override LDFLAGS += -pg --coverage
344 # Automatic dependency handling
345 ################################
347 # These files are created during compilation.
348 sinclude $(shell test -d $O && find $O -name '*.d')
355 $O/%.o: $T/%.c $G/compile-c-flags
358 # Compile C++ objects
359 $O/%.o: $T/%.cpp $G/compile-cpp-flags
360 $(call compile,cpp,$O)
362 # Link binary programs
363 $B/%: $G/link-o-flags
366 # Link shared libraries
367 $L/%.so: override CFLAGS += -fPIC
368 $L/%.so: override CXXFLAGS += -fPIC
369 $L/%.so: $G/link-o-flags
372 # Create pkg-config files using a template
374 $(call replace,$(PC_VARS),$*-PC-)
376 # Run doxygen to build the documentation. It expects the first prerequisite to
377 # be the Doxyfile to use and the next prerequisites the input files. You
378 # can override Doxyfile configuration variables by defining a DOXYGEN_VARS
379 # Make variable for this rule. For example, defining:
380 # PROJECT_NAME := myproj
381 # DOXYGEN_VARS := PROJECT_NAME
382 # You can override Doxygen's PROJECT_NAME configuration option. Optionally, you
383 # can define DOXYGEN_VARS_PREFIX too, to avoid polluting your Makefile with
384 # Doxygen variables. For example:
385 # DOXY.PROJECT_NAME := myproj
386 # DOXYGEN_VARS_PREFIX := DOXY.
387 # DOXYGEN_VARS := PROJECT_NAME
388 # This rule might be still a little restrictive, but you can always make your
389 # own if it doesn't fit your needs ;)
392 $(call exec,(cat $<; \
393 echo "FULL_PATH_NAMES=YES"; \
394 $(if $(filter INPUT,$(DOXYGEN_VARS)),,\
395 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
396 $(wordlist 2,$(words $^),$^))";) \
397 echo "OUTPUT_DIRECTORY=$(@D)"; \
398 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
399 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
400 echo "QUIET=$(if $V,YES,NO)"; \
401 $(foreach v,$(DOXYGEN_VARS),\
402 echo '$v=$($(DOXYGEN_VARS_PREFIX)$v)';) \
403 ) | doxygen -,$(@D),doxygen)
406 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
407 # to be set to the directory where the Sphinx's conf.py and reST source files
408 # are. This rule is a little restrictive, but you can always make your own if
409 # it doesn't fit your needs ;)
410 $D/%/sphinx-stamp: $G/sphinx-flags
411 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
412 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
413 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
414 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
417 # Install binary programs
419 $(call install_file,0755)
421 # Install system binary programs
423 $(call install_file,0755)
425 # Install pkg-config specification files
435 $(call exec,$(RM) -r $(VD),$(VD))
437 # Phony rule to uninstall all built targets (like "install", uses $(install)).
440 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
442 # These rules use the "Secondary Expansion" GNU Make feature, to allow
443 # sub-makes to add values to the special variables $(all), after this makefile
447 # Phony rule to make all the targets (sub-makefiles can append targets to build
448 # to the $(all) variable).
452 # Phony rule to install all built targets (sub-makefiles can append targets to
453 # build to the $(install) variable).
457 # Phony rule to build all documentation targets (sub-makefiles can append
458 # documentation to build to the $(doc) variable).
462 # Phony rule to build and run all test (sub-makefiles can append targets to
463 # build and run tests to the $(test) variable).
468 # Create build directory structure
469 ###################################
471 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
472 # directory structure of the project into $O. Create one symbolic link "last"
473 # to the current build directory.
475 # NOTE: the second mkdir can yield no arguments if the project don't have any
476 # subdirectories, that's why the current directory "." is included, so it
477 # won't show an error message in case of no subdirectories.
478 setup_build_dir__ := $(shell \
479 mkdir -p $O $B $L $D $(INCLUDE_DIR) $(addprefix $O,$(patsubst $T%,%,\
480 $(shell find $T -type d $(foreach d,$(BUILD_DIR_EXCLUDE), \
481 -not -path '*/$d' -not -path '*/$d/*')))); \
482 test -L $(VD)/last || ln -s $F $(VD)/last )
485 # Automatic rebuilding when flags or commands changes
486 ######################################################
488 # Re-compile C files if one of this variables changes
489 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
491 # Re-compile C++ files if one of this variables changes
492 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
494 # Re-link binaries and libraries if one of this variables changes
495 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
497 # Re-build sphinx documentation if one of these variables changes
498 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
500 # Create files containing the current flags to trigger a rebuild if they change
501 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
502 $(COMPILE.c.FLAGS),C compiler)
503 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
504 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler)
505 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
506 $G/link-o-flags, $(LINK.o.FLAGS),linker)
507 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
508 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx)
510 # Print any generated message (if verbose)
511 $(if $V,$(if $(setup_flag_files__), \
512 $(info !! Flags or commands changed:$(setup_flag_files__) re-building \