1 # Copyright 2008 - 2011 Integratech S.A.
2 # Copyright 2011 - 2014 Leandro Lucarella
3 # Distributed under the Boost Software License, Version 1.0
4 # See the LICENSE file for details, or http://www.boost.org/LICENSE_1_0.txt
6 ifndef Makeit.mak.included
7 Makeit.mak.included := 1
9 # These variables should be provided by the Makefile that include us:
10 # P should be the project name, mostly used to handle include directories
11 # T should be the path to the top-level directory.
12 # S should be sub-directory where the current makefile is, relative to $T.
14 # Verbosity flag (empty show nice messages, non-empty use make messages)
15 # When used internal, $V expand to @ is nice messages should be printed, this
16 # way it's easy to add $V in front of commands that should be silenced when
17 # displaying the nice messages.
18 override V := $(if $V,,@)
20 override V := $(if $(findstring s,$(MAKEFLAGS)),,$V)
22 # If $V is non-empty, colored output is used if $(COLOR) is non-empty too
25 # ANSI color used for the command if $(COLOR) is non-empty
26 # The color is composed with 2 numbers separated by ;
27 # The first is the style. 00 is normal, 01 is bold, 04 is underline, 05 blinks,
29 # The second is the color: 30 dark gray/black, 31 red, 32 green, 33 yellow, 34
30 # blue, 35 magenta, 36 cyan and 37 white.
31 # If empty, no special color is used.
34 # ANSI color used for the argument if $(COLOR) is non-empty
35 # See COLOR_CMD comment for details.
38 # ANSI color used for the warnings if $(COLOR) is non-empty
39 # See COLOR_CMD comment for details.
42 # ANSI color used for commands output if $(COLOR) is non-empty
43 # See COLOR_CMD comment for details.
46 # Flavor (variant), can be defined by the user in Config.mak
49 # Use C++ linker by default
52 # Default mode used to install files
55 # Default install flags
58 # Use pre-compiled headers if non-empty
61 # If non-empty, use valgrind to run commands via the "valgrind" function
64 # Options to pass to valgrind; if the variable $(VALGRIND_SUPP) is non-empty
65 # it will be used as a suppressions file.
66 VALGRIND_CMD ?= valgrind --tool=memcheck --leak-check=yes --db-attach=no \
67 --num-callers=24 --leak-resolution=high --track-fds=yes \
69 $(if $V,--log-file=$<.valgrind.log) \
70 $(if $(VALGRIND_SUPP),--suppressions=$(VALGRIND_SUPP))
72 # Command to generate Sphinx based documentation
73 SPHINX ?= sphinx-build
75 # Format to build using Sphinx (html, pickle, htmlhelp, latex, changes or
76 # linkcheck; see sphinx-build docs for details)
79 # Paper size for Sphinx LaTeX output (a4, letter, etc.)
80 SPHINX_PAPERSIZE ?= a4
82 # Name of the build directory (to use when excluding some paths)
83 BUILD_DIR_NAME ?= build
85 # Directories to exclude from the build directory tree replication
86 BUILD_DIR_EXCLUDE ?= $(BUILD_DIR_NAME) .git .hg .bzr _darcs .svn CVS
92 # Base directory where to install files (can be overridden, should be absolute)
95 # Path to a complete alternative environment, usually a jail, or an installed
96 # system mounted elsewhere than /.
99 # Use absolute paths to avoid problems with automatic dependencies when
100 # building from subdirectories
103 # Name of the current directory, relative to $T
104 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
106 # Base directory where to put variants (Variants Directory)
107 VD ?= $T/$(BUILD_DIR_NAME)
109 # Generated files top directory
112 # Objects (and other garbage like pre-compiled headers and dependency files)
119 # Libraries directory
122 # Documentation directory
125 # Installation directory
126 I := $(DESTDIR)$(prefix)
129 INCLUDE_DIR ?= $G/include
131 # Directory of the current makefile (this might not be the same as $(CURDIR)
132 # This variable is "lazy" because $S changes all the time, so it should be
133 # evaluated in the context where $C is used, not here.
140 # Compare two strings, if they are the same, returns the string, if not,
142 eq = $(if $(subst $1,,$2),,$1)
144 # Find sources files and get the corresponding object names. The first
145 # argument should be the sources extension ("c" or "cpp" typically). The
146 # second argument is where to search for the sources ($C if omitted). The
147 # resulting files will always have the suffix "o" and the directory rewritten
148 # to match the directory structure (from $T) but in the $O directory. For
149 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
150 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
151 # $(call find_objects,c)
152 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
153 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
154 find $(if $2,$2,$C) -name '*.$1'))
156 # Find files and get the their file names relative to another directory. The
157 # first argument should be the files suffix (".h" or ".cpp" for example). The
158 # second argument is a directory rewrite, the matched files will be rewriten to
159 # be in the directory specified in this argument (it defaults to the third
160 # argument if omitted). The third argument is where to search for the files
162 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
163 $(shell find $(if $3,$3,$C) -name '*$1'))
165 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
166 # directory, so it can be displayed as if it were a relative directory. Take
167 # just one argument, the file name.
168 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
169 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
170 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
171 $(call abbr_helper,$1)))
173 # Execute a command printing a nice message if $V is @.
174 # The first argument is mandatory and it's the command to execute. The second
175 # and third arguments are optional and are the target name and command name to
177 vexec_pc = $(if $1,\033[$1m%s\033[00m,%s)
178 vexec_p = $(if $(COLOR), \
179 ' $(call vexec_pc,$(COLOR_CMD)) $(call vexec_pc,$(COLOR_ARG))\n$(if \
180 $(COLOR_OUT),\033[$(COLOR_OUT)m)', \
182 vexec = $(if $V,printf $(vexec_p) \
183 '$(call abbr,$(if $3,$(strip $3),$(firstword $1)))' \
184 '$(call abbr,$(if $2,$(strip $2),$@))' ; )$1 \
185 $(if $V,$(if $(COLOR),$(if $(COLOR_OUT), \
186 ; r=$$? ; printf '\033[00m' ; exit $$r)))
188 # Same as vexec but it silence the echo command (prepending a @ if $V).
189 exec = $V$(call vexec,$1,$2,$3)
191 # Compile a source file to an object, generating pre-compiled headers (if
192 # $(GCH) is non-empty) and dependencies. The pre-compiled headers are generated
193 # only if the system includes change. This function is designed to be used as
194 # a command in a rule. The first argument is the type of file to compile
195 # (typically "c" or "cpp"). What to compile and the output files are built
196 # using the automatic variables from a rule. The second argument is the base
197 # output directory (typically $O). You can add non-propagated object-specific
198 # flags defining a variable with the name of the target followed with
201 # XXX: The pre-compiled headers generation is not very useful if you include
202 # local files using #include <...>, because the system headers detection
203 # is a little simplistic now, it just parse the source file and all its
204 # dependencies searching for lines starting with "#include <" and
205 # extracting the included files from them.
208 $Vif test -f $2/$*.d; then \
210 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $2/$*.d`; \
211 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
212 if diff -q -w "$2/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
216 mv "$$tmp" "$2/$*.$1.h"; \
217 $(call vexec,$(COMPILE.$1) $($@.EXTRA_FLAGS) \
218 -o "$2/$*.$1.h.gch" "$2/$*.$1.h",$2/$*.$1.h.gch); \
221 touch "$2/$*.$1.h"; \
224 $(call exec,$(COMPILE.$1) $($@.EXTRA_FLAGS) -o $@ -MMD -MP \
225 $(if $(GCH),-include $2/$*.$1.h) $<)
228 # Link object files to build an executable. The objects files are taken from
229 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
230 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
231 # function is designed to be used as a command in a rule. The output name is
232 # taken from the rule automatic variables. If an argument is provided, it's
233 # included in the link command line. The variable LINKER is used to link the
234 # executable; for example, if you want to link a C++ executable, you should use
235 # LINKER := $(CXX). You can add non-propagated target-specific flags defining
236 # a variable with the name of the target followed with ".EXTRA_FLAGS". You can
237 # specify a non-propagated object-specific linker defining a variable with the
238 # name of the target followed with ".LINKER".
239 link = $(call exec,$(if $($@.LINKER),$($@.LINKER),$(LINKER)) \
240 $(LDFLAGS) $(TARGET_ARCH) $($@.EXTRA_FLAGS) -o $@ $1 \
241 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
242 $(foreach obj,$(filter %.o,$^),$(obj)))
244 # Install a file. All arguments are optional. The first argument is the file
245 # mode (defaults to 0644). The second argument are extra flags to the install
246 # command (defaults to -D). The third argument is the source file to install
247 # (defaults to $<) and the last one is the destination (defaults to $@).
248 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
249 $(if $3,$3,$<) $(if $4,$4,$@))
251 # Concatenate variables together. The first argument is a list of variables
252 # names to concatenate. The second argument is an optional prefix for the
253 # variables and the third is the string to use as separator (" ~" if omitted).
257 # $(call varcat,A B,X_, --)
258 # Will produce something like "a -- b --"
259 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
261 # Replace variables with specified values in a template file. The first
262 # argument is a list of make variables names which will be replaced in the
263 # target file. The strings @VARNAME@ in the template file will be replaced
264 # with the value of the make $(VARNAME) variable and the result will be stored
265 # in the target file. The second (optional) argument is a prefix to add to the
266 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
267 # the template file, it will be replaced by the value of the make variable
268 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
269 # the destination file (both optional, $< and $@ are used if omitted). The
270 # fifth (optional) argument are options to pass to the substitute sed command
271 # (for example, use "g" if you want to do multiple substitutions per line).
272 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
275 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
276 # argument is the name of symbolic link to create. The link is only created if
277 # it doesn't already exist.
278 symlink_include_dir = $(shell \
279 test -L $(INCLUDE_DIR)/$1 \
280 || ln -s $C $(INCLUDE_DIR)/$1 )
282 # Create a file with flags used to trigger rebuilding when they change. The
283 # first argument is the name of the file where to store the flags, the second
284 # are the flags and the third argument is a text to be displayed if the flags
285 # have changed (optional). This should be used as a rule action or something
286 # where a shell script is expected.
287 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
288 $(if $3,test -f $1 && echo "$(if $(COLOR),$(if $(COLOR_WARN),\
289 \033[$(COLOR_WARN)m$3\033[00m,$3),$3);";) \
292 # Include sub-directory's Build.mak. The only argument is a list of
293 # subdirectories for which Build.mak should be included. The $S directory is
294 # set properly before including each sub-directory's Build.mak and restored
296 define build_subdir_code
297 _parent__$d__dir_ := $$S
298 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
299 include $$T/$$S/Build.mak
300 S := $$(_parent__$d__dir_)
302 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
304 # Run a command through valgrind if $(VALGRIND) is non-empty. The first
305 # argument is the command to run. If $(VALGRIND) is empty, the command is
306 # executed as passed to this function. If valgrind is used, the
307 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
308 # definition for extra options that can be passed as make variables. The
309 # second argument is the name of the command to print when $V is non-empty (if
310 # omitted, the first word of the first argument is used).
311 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
312 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
313 $(if $2,$2,$(firstword $1)))
319 # Use the includes directories to search for includes
320 override CPPFLAGS += -I$(INCLUDE_DIR)
322 # Let the program know where it will be installed
323 override CPPFLAGS += -DPREFIX=$(prefix)
325 # Use the generated library directory to for libraries
326 override LDFLAGS += -L$L
328 # Make sure the generated libraries can be found
329 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
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. You
366 # can override Doxyfile configuration variables by defining a DOXYGEN_VARS
367 # Make variable for this rule. For example, defining:
368 # PROJECT_NAME := myproj
369 # DOXYGEN_VARS := PROJECT_NAME
370 # You can override Doxygen's PROJECT_NAME configuration option. Optionally, you
371 # can define DOXYGEN_VARS_PREFIX too, to avoid polluting your Makefile with
372 # Doxygen variables. For example:
373 # DOXY.PROJECT_NAME := myproj
374 # DOXYGEN_VARS_PREFIX := DOXY.
375 # DOXYGEN_VARS := PROJECT_NAME
376 # This rule might be still a little restrictive, but you can always make your
377 # own if it doesn't fit your needs ;)
380 $(call exec,(cat $<; \
381 echo "FULL_PATH_NAMES=YES"; \
382 $(if $(filter INPUT,$(DOXYGEN_VARS)),,\
383 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
384 $(wordlist 2,$(words $^),$^))";) \
385 echo "OUTPUT_DIRECTORY=$(@D)"; \
386 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
387 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
388 echo "QUIET=$(if $V,YES,NO)"; \
389 $(foreach v,$(DOXYGEN_VARS),\
390 echo '$v=$($(DOXYGEN_VARS_PREFIX)$v)';) \
391 ) | doxygen -,$(@D),doxygen)
394 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
395 # to be set to the directory where the Sphinx's conf.py and reST source files
396 # are. This rule is a little restrictive, but you can always make your own if
397 # it doesn't fit your needs ;)
398 $D/%/sphinx-stamp: $G/sphinx-flags
399 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
400 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
401 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
402 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
405 # Install binary programs
407 $(call install_file,0755)
409 # Install system binary programs
411 $(call install_file,0755)
413 # Install pkg-config specification files
423 $(call exec,$(RM) -r $(VD),$(VD))
425 # Phony rule to uninstall all built targets (like "install", uses $(install)).
428 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
430 # These rules use the "Secondary Expansion" GNU Make feature, to allow
431 # sub-makes to add values to the special variables $(all), after this makefile
435 # Phony rule to make all the targets (sub-makefiles can append targets to build
436 # to the $(all) variable).
440 # Phony rule to install all built targets (sub-makefiles can append targets to
441 # build to the $(install) variable).
445 # Phony rule to build all documentation targets (sub-makefiles can append
446 # documentation to build to the $(doc) variable).
450 # Phony rule to build and run all test (sub-makefiles can append targets to
451 # build and run tests to the $(test) variable).
456 # Create build directory structure
457 ###################################
459 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
460 # directory structure of the project into $O. Create one symbolic link "last"
461 # to the current build directory.
462 setup_build_dir__ := $(shell \
463 mkdir -p $O $B $L $D $(INCLUDE_DIR) $(addprefix $O,$(patsubst $T%,%,\
464 $(shell find $T -type d $(foreach d,$(BUILD_DIR_EXCLUDE), \
465 -not -path '$T/$d' -not -path '$T/$d/*' \
466 -not -path '$T/*/$d' -not -path '$T/*/$d/*')))); \
467 rm -f $(VD)/last && ln -s $F $(VD)/last )
470 # Automatic rebuilding when flags or commands changes
471 ######################################################
473 # Re-compile C files if one of this variables changes
474 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
476 # Re-compile C++ files if one of this variables changes
477 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
479 # Re-link binaries and libraries if one of this variables changes
480 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
482 # Re-build sphinx documentation if one of these variables changes
483 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
485 # Create files containing the current flags to trigger a rebuild if they change
486 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
487 $(COMPILE.c.FLAGS),C compiler)
488 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
489 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler)
490 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
491 $G/link-o-flags, $(LINK.o.FLAGS),linker)
492 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
493 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx)
495 # Print any generated message (if verbose)
496 $(if $V,$(if $(setup_flag_files__), \
497 $(info !! Flags or commands changed:$(setup_flag_files__) re-building \