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 # Flavor (variant), should be one of "dbg", "opt" or "cov"
20 # Use C++ linker by default
23 # Default mode used to install files
26 # Default install flags
29 # Use pre-compiled headers if non-empty
32 # If non-empty, use valgrind to run commands via the "valgrind" function
35 # Options to pass to valgrind; if the variable $(VALGRIND_SUPP) is non-empty
36 # it will be used as a suppressions file.
37 VALGRIND_CMD ?= valgrind --tool=memcheck --leak-check=yes --db-attach=no \
38 --num-callers=24 --leak-resolution=high --track-fds=yes \
40 $(if $V,--log-file=$<.valgrind.log) \
41 $(if $(VALGRIND_SUPP),--suppressions=$(VALGRIND_SUPP))
43 # Command to generate Sphinx based documentation
44 SPHINX ?= sphinx-build
46 # Format to build using Sphinx (html, pickle, htmlhelp, latex, changes or
47 # linkcheck; see sphinx-build docs for details)
50 # Paper size for Sphinx LaTeX output (a4, letter, etc.)
51 SPHINX_PAPERSIZE ?= a4
57 # Base directory where to install files (can be overridden, should be absolute)
60 # Path to a complete alternative environment, usually a jail, or an installed
61 # system mounted elsewhere than /.
64 # Use absolute paths to avoid problems with automatic dependencies when
65 # building from subdirectories
68 # Name of the current directory, relative to $T
69 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
71 # Base directory where to put variants (Variants Directory)
74 # Generated files top directory
77 # Objects (and other garbage like pre-compiled headers and dependency files)
87 # Documentation directory
90 # Installation directory
91 I := $(DESTDIR)$(prefix)
94 INCLUDE_DIR ?= $G/include
96 # Directory of the current makefile (this might not be the same as $(CURDIR)
97 # This variable is "lazy" because $S changes all the time, so it should be
98 # evaluated in the context where $C is used, not here.
105 # Compare two strings, if they are the same, returns the string, if not,
107 eq = $(if $(subst $1,,$2),,$1)
109 # Find sources files and get the corresponding object names. The first
110 # argument should be the sources extension ("c" or "cpp" typically). The
111 # second argument is where to search for the sources ($C if omitted). The
112 # resulting files will always have the suffix "o" and the directory rewritten
113 # to match the directory structure (from $T) but in the $O directory. For
114 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
115 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
116 # $(call find_objects,c)
117 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
118 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
119 find $(if $2,$2,$C) -name '*.$1'))
121 # Find files and get the their file names relative to another directory. The
122 # first argument should be the files suffix (".h" or ".cpp" for example). The
123 # second argument is a directory rewrite, the matched files will be rewriten to
124 # be in the directory specified in this argument (it defaults to the third
125 # argument if omitted). The third argument is where to search for the files
127 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
128 $(shell find $(if $3,$3,$C) -name '*$1'))
130 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
131 # directory, so it can be displayed as if it were a relative directory. Take
132 # just one argument, the file name.
133 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
134 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
135 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
136 $(call abbr_helper,$1)))
138 # Execute a command printing a nice message if $V is @.
139 # The first argument is mandatory and it's the command to execute. The second
140 # and third arguments are optional and are the target name and command name to
143 echo ' $(call abbr,$(if $3,$(strip $3),$(firstword $1))) \
144 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
146 # Same as vexec but it silence the echo command (prepending a @ if $V).
147 exec = $V$(call vexec,$1,$2,$3)
149 # Compile a source file to an object, generating pre-compiled headers and
150 # dependencies. The pre-compiled headers are generated only if the system
151 # includes change. This function is designed to be used as a command in a rule.
152 # It takes one argument only, the type of file to compile (typically "c" or
153 # "cpp"). What to compile and the output files are built using the automatic
154 # variables from a rule.
157 $Vif test -f $O/$*.d; then \
159 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
160 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
161 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
165 mv "$$tmp" "$O/$*.$1.h"; \
166 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
170 touch "$O/$*.$1.h"; \
173 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
176 # Link object files to build an executable. The objects files are taken from
177 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
178 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
179 # function is designed to be used as a command in a rule. The output name is
180 # taken from the rule automatic variables. If an argument is provided, it's
181 # included in the link command line. The variable LINKER is used to link the
182 # executable; for example, if you want to link a C++ executable, you should use
184 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
185 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
186 $(foreach obj,$(filter %.o,$^),$(obj)))
188 # Install a file. All arguments are optional. The first argument is the file
189 # mode (defaults to 0644). The second argument are extra flags to the install
190 # command (defaults to -D). The third argument is the source file to install
191 # (defaults to $<) and the last one is the destination (defaults to $@).
192 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
193 $(if $3,$3,$<) $(if $4,$4,$@))
195 # Concatenate variables together. The first argument is a list of variables
196 # names to concatenate. The second argument is an optional prefix for the
197 # variables and the third is the string to use as separator (" ~" if omitted).
201 # $(call varcat,A B,X_, --)
202 # Will produce something like "a -- b --"
203 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
205 # Replace variables with specified values in a template file. The first
206 # argument is a list of make variables names which will be replaced in the
207 # target file. The strings @VARNAME@ in the template file will be replaced
208 # with the value of the make $(VARNAME) variable and the result will be stored
209 # in the target file. The second (optional) argument is a prefix to add to the
210 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
211 # the template file, it will be replaced by the value of the make variable
212 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
213 # the destination file (both optional, $< and $@ are used if omitted). The
214 # fifth (optional) argument are options to pass to the substitute sed command
215 # (for example, use "g" if you want to do multiple substitutions per line).
216 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
219 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
220 # argument is the name of symbolic link to create. The link is only created if
221 # it doesn't already exist.
222 symlink_include_dir = $(shell \
223 test -L $(INCLUDE_DIR)/$1 \
224 || ln -s $C $(INCLUDE_DIR)/$1 )
226 # Create a file with flags used to trigger rebuilding when they change. The
227 # first argument is the name of the file where to store the flags, the second
228 # are the flags and the third argument is a text to be displayed if the flags
229 # have changed (optional). This should be used as a rule action or something
230 # where a shell script is expected.
231 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
232 $(if $3,test -f $1 && echo "$3";) \
235 # Include sub-directory's Build.mak. The only argument is a list of
236 # subdirectories for which Build.mak should be included. The $S directory is
237 # set properly before including each sub-directory's Build.mak and restored
239 define build_subdir_code
240 _parent__$d__dir_ := $$S
241 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
242 include $$T/$$S/Build.mak
243 S := $$(_parent__$d__dir_)
245 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
247 # Run a command through valgrind if $(VALGRIND) is non-empty. The first and
248 # only argument is the command to. If $(VALGRIND) is empty, the command is
249 # executed as passed to this function. If valgrind is used, the
250 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
251 # definition for extra options that can be passed as make variables.
252 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
253 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
260 # Warn about everything
261 override CPPFLAGS += -Wall
263 # Use the includes directories to search for includes
264 override CPPFLAGS += -I$(INCLUDE_DIR)
266 # Let the program know where it will be installed
267 override CPPFLAGS += -DPREFIX=$(prefix)
269 # Be standard compliant
270 override CFLAGS += -std=c99 -pedantic
271 override CXXFLAGS += -std=c++98 -pedantic
273 # Use the generated library directory to for libraries
274 override LDFLAGS += -L$L -Wall
276 # Make sure the generated libraries can be found
277 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
284 override CPPFLAGS += -ggdb -DDEBUG
288 override CPPFLAGS += -O2 -DNDEBUG
292 override CPPFLAGS += -ggdb -pg --coverage
293 override LDFLAGS += -pg --coverage
297 # Automatic dependency handling
298 ################################
300 # These files are created during compilation.
301 sinclude $(shell test -d $O && find $O -name '*.d')
308 $O/%.o: $T/%.c $G/compile-c-flags
311 # Compile C++ objects
312 $O/%.o: $T/%.cpp $G/compile-cpp-flags
315 # Link binary programs
316 $B/%: $G/link-o-flags
319 # Link shared libraries
320 $L/%.so: override CFLAGS += -fPIC
321 $L/%.so: override CXXFLAGS += -fPIC
322 $L/%.so: $G/link-o-flags
325 # Create pkg-config files using a template
327 $(call replace,$(PC_VARS),$*-PC-)
329 # Run doxygen to build the documentation. It expects the first prerequisite to
330 # be the Doxyfile to use and the next prerequisites the input files. This rule
331 # is a little restrictive, but you can always make your own if it doesn't fit
335 $(call exec,(cat $<; \
336 echo "FULL_PATH_NAMES=YES"; \
337 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
338 $(wordlist 2,$(words $^),$^))"; \
339 echo "OUTPUT_DIRECTORY=$(@D)"; \
340 echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
341 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
342 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
343 echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
346 # Run Sphinx to build the documentation. It expects the variable SPHINX_DIR
347 # to be set to the directory where the Sphinx's conf.py and reST source files
348 # are. This rule is a little restrictive, but you can always make your own if
349 # it doesn't fit your needs ;)
350 $D/%/sphinx-stamp: $G/sphinx-flags
351 $V mkdir -p $(@D)/$(SPHINX_FORMAT)
352 $(call exec,$(SPHINX) $(if $V,-q) -b $(SPHINX_FORMAT) \
353 -d $(@D)/doctrees -D latex_paper_size=$(SPHINX_PAPERSIZE) \
354 $(SPHINX_DIR) $(@D)/$(SPHINX_FORMAT),$(@D),$(SPHINX))
357 # Install binary programs
359 $(call install_file,0755)
361 # Install system binary programs
363 $(call install_file,0755)
365 # Install pkg-config specification files
375 $(call exec,$(RM) -r $(VD),$(VD))
377 # Phony rule to uninstall all built targets (like "install", uses $(install)).
380 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
382 # These rules use the "Secondary Expansion" GNU Make feature, to allow
383 # sub-makes to add values to the special variables $(all), after this makefile
387 # Phony rule to make all the targets (sub-makefiles can append targets to build
388 # to the $(all) variable).
392 # Phony rule to install all built targets (sub-makefiles can append targets to
393 # build to the $(install) variable).
397 # Phony rule to build all documentation targets (sub-makefiles can append
398 # documentation to build to the $(doc) variable).
402 # Phony rule to build and run all test (sub-makefiles can append targets to
403 # build and run tests to the $(test) variable).
408 # Create build directory structure
409 ###################################
411 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
412 # directory structure of the project into $O. Create one symbolic link "last"
413 # to the current build directory.
415 # NOTE: the second mkdir can yield no arguments if the project don't have any
416 # subdirectories, that's why the current directory "." is included, so it
417 # won't show an error message in case of no subdirectories.
418 setup_build_dir__ := $(shell \
419 mkdir -p $O $B $L $D $(INCLUDE_DIR); \
420 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
421 $(shell find $T -type d -not -path '$(VD)*'))); \
422 test -L $(VD)/last || ln -s $F $(VD)/last )
425 # Automatic rebuilding when flags or commands changes
426 ######################################################
428 # Re-compile C files if one of this variables changes
429 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
431 # Re-compile C++ files if one of this variables changes
432 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
434 # Re-link binaries and libraries if one of this variables changes
435 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
437 # Re-build sphinx documentation if one of these variables changes
438 SPHINX.FLAGS := $(call varcat,SPHINX SPHINX_FORMAT SPHINX_PAPERSIZE)
440 # Create files containing the current flags to trigger a rebuild if they change
441 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
442 $(COMPILE.c.FLAGS),C compiler or flags; )
443 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
444 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
445 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
446 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
447 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
448 $G/sphinx-flags, $(SPHINX.FLAGS),sphinx command or flags; )
450 # Print any generated message (if verbose)
451 $(if $V,$(if $(setup_flag_files__), \
452 $(info !! Something changed: $(setup_flag_files__)re-building \