1 ifndef Lib.mak.included
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))
47 # Base directory where to install files (can be overridden, should be absolute)
50 # Path to a complete alternative environment, usually a jail, or an installed
51 # system mounted elsewhere than /.
54 # Use absolute paths to avoid problems with automatic dependencies when
55 # building from subdirectories
58 # Name of the current directory, relative to $T
59 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
61 # Base directory where to put variants (Variants Directory)
64 # Generated files top directory
67 # Objects (and other garbage like pre-compiled headers and dependency files)
77 # Documentation directory
80 # Installation directory
81 I := $(DESTDIR)$(prefix)
84 INCLUDE_DIR ?= $G/include
86 # Directory of the current makefile (this might not be the same as $(CURDIR)
87 # This variable is "lazy" because $S changes all the time, so it should be
88 # evaluated in the context where $C is used, not here.
95 # Compare two strings, if they are the same, returns the string, if not,
97 eq = $(if $(subst $1,,$2),,$1)
99 # Find sources files and get the corresponding object names. The first
100 # argument should be the sources extension ("c" or "cpp" typically). The
101 # second argument is where to search for the sources ($C if omitted). The
102 # resulting files will always have the suffix "o" and the directory rewritten
103 # to match the directory structure (from $T) but in the $O directory. For
104 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
105 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
106 # $(call find_objects,c)
107 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
108 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
109 find $(if $2,$2,$C) -name '*.$1'))
111 # Find files and get the their file names relative to another directory. The
112 # first argument should be the files suffix (".h" or ".cpp" for example). The
113 # second argument is a directory rewrite, the matched files will be rewriten to
114 # be in the directory specified in this argument (it defaults to the third
115 # argument if omitted). The third argument is where to search for the files
117 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
118 $(shell find $(if $3,$3,$C) -name '*$1'))
120 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
121 # directory, so it can be displayed as if it were a relative directory. Take
122 # just one argument, the file name.
123 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
124 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
125 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
126 $(call abbr_helper,$1)))
128 # Execute a command printing a nice message if $V is @.
129 # The first argument is mandatory and it's the command to execute. The second
130 # and third arguments are optional and are the target name and command name to
133 echo ' $(call abbr,$(if $3,$(strip $3),$(firstword $1))) \
134 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
136 # Same as vexec but it silence the echo command (prepending a @ if $V).
137 exec = $V$(call vexec,$1,$2,$3)
139 # Compile a source file to an object, generating pre-compiled headers and
140 # dependencies. The pre-compiled headers are generated only if the system
141 # includes change. This function is designed to be used as a command in a rule.
142 # It takes one argument only, the type of file to compile (typically "c" or
143 # "cpp"). What to compile and the output files are built using the automatic
144 # variables from a rule.
147 $Vif test -f $O/$*.d; then \
149 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
150 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
151 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
155 mv "$$tmp" "$O/$*.$1.h"; \
156 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
160 touch "$O/$*.$1.h"; \
163 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
166 # Link object files to build an executable. The objects files are taken from
167 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
168 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
169 # function is designed to be used as a command in a rule. The output name is
170 # taken from the rule automatic variables. If an argument is provided, it's
171 # included in the link command line. The variable LINKER is used to link the
172 # executable; for example, if you want to link a C++ executable, you should use
174 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
175 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
176 $(foreach obj,$(filter %.o,$^),$(obj)))
178 # Install a file. All arguments are optional. The first argument is the file
179 # mode (defaults to 0644). The second argument are extra flags to the install
180 # command (defaults to -D). The third argument is the source file to install
181 # (defaults to $<) and the last one is the destination (defaults to $@).
182 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
183 $(if $3,$3,$<) $(if $4,$4,$@))
185 # Concatenate variables together. The first argument is a list of variables
186 # names to concatenate. The second argument is an optional prefix for the
187 # variables and the third is the string to use as separator (" ~" if omitted).
191 # $(call varcat,A B,X_, --)
192 # Will produce something like "a -- b --"
193 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
195 # Replace variables with specified values in a template file. The first
196 # argument is a list of make variables names which will be replaced in the
197 # target file. The strings @VARNAME@ in the template file will be replaced
198 # with the value of the make $(VARNAME) variable and the result will be stored
199 # in the target file. The second (optional) argument is a prefix to add to the
200 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
201 # the template file, it will be replaced by the value of the make variable
202 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
203 # the destination file (both optional, $< and $@ are used if omitted). The
204 # fifth (optional) argument are options to pass to the substitute sed command
205 # (for example, use "g" if you want to do multiple substitutions per line).
206 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
209 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
210 # argument is the name of symbolic link to create. The link is only created if
211 # it doesn't already exist.
212 symlink_include_dir = $(shell \
213 test -L $(INCLUDE_DIR)/$1 \
214 || ln -s $C $(INCLUDE_DIR)/$1 )
216 # Create a file with flags used to trigger rebuilding when they change. The
217 # first argument is the name of the file where to store the flags, the second
218 # are the flags and the third argument is a text to be displayed if the flags
219 # have changed (optional). This should be used as a rule action or something
220 # where a shell script is expected.
221 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
222 $(if $3,test -f $1 && echo "$3";) \
225 # Include sub-directory's Build.mak. The only argument is a list of
226 # subdirectories for which Build.mak should be included. The $S directory is
227 # set properly before including each sub-directory's Build.mak and restored
229 define build_subdir_code
230 _parent__$d__dir_ := $$S
231 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
232 include $$T/$$S/Build.mak
233 S := $$(_parent__$d__dir_)
235 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
237 # Run a command through valgrind if $(VALGRIND) is non-empty. The first and
238 # only argument is the command to. If $(VALGRIND) is empty, the command is
239 # executed as passed to this function. If valgrind is used, the
240 # $(VALGRIND_CMD) is prepended to the command to run. See VALGRIND_CMD
241 # definition for extra options that can be passed as make variables.
242 valgrind = $(call exec,$(if $(VALGRIND),$(VALGRIND_CMD)) $1,\
243 $(if $(VALGRIND),[$(firstword $(VALGRIND_CMD))], ),\
250 # Warn about everything
251 override CPPFLAGS += -Wall
253 # Use the includes directories to search for includes
254 override CPPFLAGS += -I$(INCLUDE_DIR)
256 # Let the program know where it will be installed
257 override CPPFLAGS += -DPREFIX=$(prefix)
259 # Be standard compliant
260 override CFLAGS += -std=c99 -pedantic
261 override CXXFLAGS += -std=c++98 -pedantic
263 # Use the generated library directory to for libraries
264 override LDFLAGS += -L$L -Wall
266 # Make sure the generated libraries can be found
267 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
274 override CPPFLAGS += -ggdb -DDEBUG
278 override CPPFLAGS += -O2 -DNDEBUG
282 override CPPFLAGS += -ggdb -pg --coverage
283 override LDFLAGS += -pg --coverage
287 # Automatic dependency handling
288 ################################
290 # These files are created during compilation.
291 sinclude $(shell test -d $O && find $O -name '*.d')
298 $O/%.o: $T/%.c $G/compile-c-flags
301 # Compile C++ objects
302 $O/%.o: $T/%.cpp $G/compile-cpp-flags
305 # Link binary programs
306 $B/%: $G/link-o-flags
309 # Link shared libraries
310 $L/%.so: override CFLAGS += -fPIC
311 $L/%.so: override CXXFLAGS += -fPIC
312 $L/%.so: $G/link-o-flags
315 # Create pkg-config files using a template
317 $(call replace,$(PC_VARS),$*-PC-)
319 # Run doxygen to build the documentation. It expects the first prerequisite to
320 # be the Doxyfile to use and the next prerequisites the input files. This rule
321 # is a little restrictive, but you can always make your own if it doesn't fit
325 $(call exec,(cat $<; \
326 echo "FULL_PATH_NAMES=YES"; \
327 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
328 $(wordlist 2,$(words $^),$^))"; \
329 echo "OUTPUT_DIRECTORY=$(@D)"; \
330 echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
331 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
332 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
333 echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
336 # Install binary programs
338 $(call install_file,0755)
340 # Install system binary programs
342 $(call install_file,0755)
344 # Install pkg-config specification files
354 $(call exec,$(RM) -r $(VD),$(VD))
356 # Phony rule to uninstall all built targets (like "install", uses $(install)).
359 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
361 # These rules use the "Secondary Expansion" GNU Make feature, to allow
362 # sub-makes to add values to the special variables $(all), after this makefile
366 # Phony rule to make all the targets (sub-makefiles can append targets to build
367 # to the $(all) variable).
371 # Phony rule to install all built targets (sub-makefiles can append targets to
372 # build to the $(install) variable).
376 # Phony rule to build all documentation targets (sub-makefiles can append
377 # documentation to build to the $(doc) variable).
381 # Phony rule to build and run all test (sub-makefiles can append targets to
382 # build and run tests to the $(test) variable).
387 # Create build directory structure
388 ###################################
390 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
391 # directory structure of the project into $O. Create one symbolic link "last"
392 # to the current build directory.
394 # NOTE: the second mkdir can yield no arguments if the project don't have any
395 # subdirectories, that's why the current directory "." is included, so it
396 # won't show an error message in case of no subdirectories.
397 setup_build_dir__ := $(shell \
398 mkdir -p $O $B $L $D $(INCLUDE_DIR); \
399 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
400 $(shell find $T -type d -not -path '$(VD)*'))); \
401 test -L $(VD)/last || ln -s $F $(VD)/last )
404 # Automatic rebuilding when flags or commands changes
405 ######################################################
407 # Re-compile C files if one of this variables changes
408 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
410 # Re-compile C++ files if one of this variables changes
411 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
413 # Re-link binaries and libraries if one of this variables changes
414 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
416 # Create files containing the current flags to trigger a rebuild if they change
417 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
418 $(COMPILE.c.FLAGS),C compiler or flags; )
419 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
420 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
421 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
422 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
424 # Print any generated message (if verbose)
425 $(if $V,$(if $(setup_flag_files__), \
426 $(info !! Something changed: $(setup_flag_files__)re-building \