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
36 # Base directory where to install files (can be overridden, should be absolute)
39 # Path to a complete alternative environment, usually a jail, or an installed
40 # system mounted elsewhere than /.
43 # Use absolute paths to avoid problems with automatic dependencies when
44 # building from subdirectories
47 # Name of the current directory, relative to $T
48 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
50 # Base directory where to put variants (Variants Directory)
53 # Generated files top directory
56 # Objects (and other garbage like pre-compiled headers and dependency files)
66 # Documentation directory
69 # Installation directory
70 I := $(DESTDIR)$(prefix)
73 INCLUDE_DIR ?= $G/include
75 # Directory of the current makefile (this might not be the same as $(CURDIR)
76 # This variable is "lazy" because $S changes all the time, so it should be
77 # evaluated in the context where $C is used, not here.
84 # Compare two strings, if they are the same, returns the string, if not,
86 eq = $(if $(subst $1,,$2),,$1)
88 # Find sources files and get the corresponding object names. The first
89 # argument should be the sources extension ("c" or "cpp" typically). The
90 # second argument is where to search for the sources ($C if omitted). The
91 # resulting files will always have the suffix "o" and the directory rewritten
92 # to match the directory structure (from $T) but in the $O directory. For
93 # example, if $T is "/usr/src", $O is "/tmp/obj", $C is "/usr/src/curr" and it
94 # have 2 C sources: "/usr/src/curr/1.c" and "/usr/src/curr/dir/2.c", the call:
95 # $(call find_objects,c)
96 # Will yield "/tmp/obj/curr/1.o" and "/tmp/obj/curr/dir/2.o".
97 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell \
98 find $(if $2,$2,$C) -name '*.$1'))
100 # Find files and get the their file names relative to another directory. The
101 # first argument should be the files suffix (".h" or ".cpp" for example). The
102 # second argument is a directory rewrite, the matched files will be rewriten to
103 # be in the directory specified in this argument (it defaults to the third
104 # argument if omitted). The third argument is where to search for the files
106 find_files = $(patsubst $(if $3,$3,$C)/%$1,$(if $2,$2,$(if $3,$3,$C))/%$1, \
107 $(shell find $(if $3,$3,$C) -name '*$1'))
109 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
110 # directory, so it can be displayed as if it were a relative directory. Take
111 # just one argument, the file name.
112 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
113 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1,$(addprefix \
114 $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
115 $(call abbr_helper,$1)))
117 # Execute a command printing a nice message if $V is @.
118 # The first argument is mandatory and it's the command to execute. The second
119 # and third arguments are optional and are the target name and command name to
122 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
123 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
125 # Same as vexec but it silence the echo command (prepending a @ if $V).
126 exec = $V$(call vexec,$1,$2,$3)
128 # Compile a source file to an object, generating pre-compiled headers and
129 # dependencies. The pre-compiled headers are generated only if the system
130 # includes change. This function is designed to be used as a command in a rule.
131 # It takes one argument only, the type of file to compile (typically "c" or
132 # "cpp"). What to compile and the output files are built using the automatic
133 # variables from a rule.
136 $Vif test -f $O/$*.d; then \
138 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
139 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
140 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
144 mv "$$tmp" "$O/$*.$1.h"; \
145 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
149 touch "$O/$*.$1.h"; \
152 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
155 # Link object files to build an executable. The objects files are taken from
156 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
157 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
158 # function is designed to be used as a command in a rule. The output name is
159 # taken from the rule automatic variables. If an argument is provided, it's
160 # included in the link command line. The variable LINKER is used to link the
161 # executable; for example, if you want to link a C++ executable, you should use
163 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
164 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
165 $(foreach obj,$(filter %.o,$^),$(obj)))
167 # Install a file. All arguments are optional. The first argument is the file
168 # mode (defaults to 0644). The second argument are extra flags to the install
169 # command (defaults to -D). The third argument is the source file to install
170 # (defaults to $<) and the last one is the destination (defaults to $@).
171 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
172 $(if $3,$3,$<) $(if $4,$4,$@))
174 # Concatenate variables together. The first argument is a list of variables
175 # names to concatenate. The second argument is an optional prefix for the
176 # variables and the third is the string to use as separator (" ~" if omitted).
180 # $(call varcat,A B,X_, --)
181 # Will produce something like "a -- b --"
182 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
184 # Replace variables with specified values in a template file. The first
185 # argument is a list of make variables names which will be replaced in the
186 # target file. The strings @VARNAME@ in the template file will be replaced
187 # with the value of the make $(VARNAME) variable and the result will be stored
188 # in the target file. The second (optional) argument is a prefix to add to the
189 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
190 # the template file, it will be replaced by the value of the make variable
191 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
192 # the destination file (both optional, $< and $@ are used if omitted). The
193 # fifth (optional) argument are options to pass to the substitute sed command
194 # (for example, use "g" if you want to do multiple substitutions per line).
195 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
198 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
199 # argument is the name of symbolic link to create. The link is only created if
200 # it doesn't already exist.
201 symlink_include_dir = $(shell \
202 test -L $(INCLUDE_DIR)/$1 \
203 || ln -s $C $(INCLUDE_DIR)/$1 )
205 # Create a file with flags used to trigger rebuilding when they change. The
206 # first argument is the name of the file where to store the flags, the second
207 # are the flags and the third argument is a text to be displayed if the flags
208 # have changed (optional). This should be used as a rule action or something
209 # where a shell script is expected.
210 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
211 $(if $3,test -f $1 && echo "$3";) \
214 # Include sub-directory's Build.mak. The only argument is a list of
215 # subdirectories for which Build.mak should be included. The $S directory is
216 # set properly before including each sub-directory's Build.mak and restored
218 define build_subdir_code
219 _parent__$d__dir_ := $$S
220 S := $$(if $$(_parent__$d__dir_),$$(_parent__$d__dir_)/$d,$d)
221 include $$T/$$S/Build.mak
222 S := $$(_parent__$d__dir_)
224 include_subdirs = $(foreach d,$1,$(eval $(build_subdir_code)))
230 # Warn about everything
231 override CPPFLAGS += -Wall
233 # Use the includes directories to search for includes
234 override CPPFLAGS += -I$(INCLUDE_DIR)
236 # Let the program know where it will be installed
237 override CPPFLAGS += -DPREFIX=$(prefix)
239 # Be standard compliant
240 override CFLAGS += -std=c99 -pedantic
241 override CXXFLAGS += -std=c++98 -pedantic
243 # Use the generated library directory to for libraries
244 override LDFLAGS += -L$L -Wall
246 # Make sure the generated libraries can be found
247 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
254 override CPPFLAGS += -ggdb -DDEBUG
258 override CPPFLAGS += -O2 -DNDEBUG
262 override CPPFLAGS += -ggdb -pg --coverage
263 override LDFLAGS += -pg --coverage
267 # Automatic dependency handling
268 ################################
270 # These files are created during compilation.
271 sinclude $(shell test -d $O && find $O -name '*.d')
278 $O/%.o: $T/%.c $G/compile-c-flags
281 # Compile C++ objects
282 $O/%.o: $T/%.cpp $G/compile-cpp-flags
285 # Link binary programs
286 $B/%: $G/link-o-flags
289 # Link shared libraries
290 $L/%.so: override CFLAGS += -fPIC
291 $L/%.so: override CXXFLAGS += -fPIC
292 $L/%.so: $G/link-o-flags
295 # Create pkg-config files using a template
297 $(call replace,$(PC_VARS),$*-PC-)
299 # Run doxygen to build the documentation. It expects the first prerequisite to
300 # be the Doxyfile to use and the next prerequisites the input files. This rule
301 # is a little restrictive, but you can always make your own if it doesn't fit
305 $(call exec,(cat $<; \
306 echo "FULL_PATH_NAMES=YES"; \
307 echo "INPUT=$(patsubst $(<D)/%,$(INCLUDE_DIR)/$*/%, \
308 $(wordlist 2,$(words $^),$^))"; \
309 echo "OUTPUT_DIRECTORY=$(@D)"; \
310 echo "INCLUDE_PATH=$(INCLUDE_DIR)"; \
311 echo "STRIP_FROM_PATH=$(INCLUDE_DIR)"; \
312 echo "STRIP_FROM_INC_PATH=$(INCLUDE_DIR)"; \
313 echo "QUIET=$(if $V,YES,NO)") | doxygen -,$(@D),doxygen)
316 # Install binary programs
318 $(call install_file,0755)
320 # Install system binary programs
322 $(call install_file,0755)
324 # Install pkg-config specification files
334 $(call exec,$(RM) -r $(VD),$(VD))
336 # Phony rule to uninstall all built targets (like "install", uses $(install)).
339 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
341 # These rules use the "Secondary Expansion" GNU Make feature, to allow
342 # sub-makes to add values to the special variables $(all), after this makefile
346 # Phony rule to make all the targets (sub-makefiles can append targets to build
347 # to the $(all) variable).
351 # Phony rule to install all built targets (sub-makefiles can append targets to
352 # build to the $(install) variable).
356 # Phony rule to build all documentation targets (sub-makefiles can append
357 # documentation to build to the $(doc) variable).
362 # Create build directory structure
363 ###################################
365 # Create $O, $B, $L, $D and $(INCLUDE_DIR) directories and replicate the
366 # directory structure of the project into $O. Create one symbolic link "last"
367 # to the current build directory.
369 # NOTE: the second mkdir can yield no arguments if the project don't have any
370 # subdirectories, that's why the current directory "." is included, so it
371 # won't show an error message in case of no subdirectories.
372 setup_build_dir__ := $(shell \
373 mkdir -p $O $B $L $D $(INCLUDE_DIR); \
374 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
375 $(shell find $T -type d -not -path '$(VD)*'))); \
376 test -L $(VD)/last || ln -s $F $(VD)/last )
379 # Automatic rebuilding when flags or commands changes
380 ######################################################
382 # Re-compile C files if one of this variables changes
383 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
385 # Re-compile C++ files if one of this variables changes
386 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
388 # Re-link binaries and libraries if one of this variables changes
389 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
391 # Create files containing the current flags to trigger a rebuild if they change
392 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
393 $(COMPILE.c.FLAGS),C compiler or flags; )
394 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
395 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
396 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
397 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
399 # Print any generated message (if verbose)
400 $(if $V,$(if $(setup_flag_files__), \
401 $(info !! Something changed: $(setup_flag_files__)re-building \