1 ifndef Lib.mak.included
4 # These variables should be provided by the includer Makefile:
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 # Degault install flags
29 # Use precompiled headers if non-empty
36 # Base directory where to install files (can be overrided, 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
53 # Generated files top directory
56 # Objects (and other garbage like precompiled headers and dependency files)
66 # Installation directory
67 I := $(DESTDIR)$(prefix)
70 INCLUDE_DIR ?= $G/include
72 # Directory of the current makefile (this might not be the same as $(CURDIR)
73 # This variable is "lazy" because $S changes all the time, so it should be
74 # evaluated in the context where $C is used, not here.
81 # Compare two strings, if they are the same, returns the string, if not,
83 eq = $(if $(subst $1,,$2),,$1)
85 # Find sources files and get the corresponding object names
86 # The first argument should be the sources extension ("c" or "cpp" typically)
87 # It expects the variable $T and $O to be defined as commented previously in
89 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $C -name '*.$1'))
91 # Find sources files and get the corresponding object names
92 # The first argument should be the sources extension ("c" or "cpp" typically)
93 # It expects the variable $T and $O to be defined as commented previously in
95 find_headers = $(patsubst $C/%.$1,$2/%.$1,$(shell find $C -name '*.$1'))
97 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
98 # directory, so it can be displayed as if it were a relative directory. Take
99 # just one argument, the file name.
100 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
101 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1, \
102 $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
103 $(call abbr_helper,$1)))
105 # Execute a command printing a nice message if $V is @.
106 # The first argument is mandatory and it's the command to execute. The second
107 # and third arguments are optional and are the target name and command name to
110 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
111 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
113 # Same as vexec but it silence the echo command (prepending a @ if $V).
114 exec = $V$(call vexec,$1,$2,$3)
116 # Compile a source file to an object, generating pre-compiled headers and
117 # dependencies. The pre-compiled headers are generated only if the system
118 # includes change. This function is designed to be used as a command in a rule.
119 # It takes one argument only, the type of file to compile (typically "c" or
120 # "cpp"). What to compile and the output files are built using the automatic
121 # variables from a rule.
124 $Vif test -f $O/$*.d; then \
126 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
127 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
128 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
132 mv "$$tmp" "$O/$*.$1.h"; \
133 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
137 touch "$O/$*.$1.h"; \
140 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
143 # Link object files to build an executable. The objects files are taken from
144 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
145 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
146 # function is designed to be used as a command in a rule. The ouput name is
147 # taken from the rule automatic variables. If an argument is provided, it's
148 # included in the link command line. The variable LINKER is used to link the
149 # executable; for example, if you want to link a C++ executable, you should use
151 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
152 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
153 $(foreach obj,$(filter %.o,$^),$(obj)))
155 # Install a file. All arguments are optional. The first argument is the file
156 # mode (defaults to 0644). The second argument are extra flags to the install
157 # command (defaults to -D). The third argument is the source file to install
158 # (defaults to $<) and the last one is the destination (defaults to $@).
159 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
160 $(if $3,$3,$<) $(if $4,$4,$@))
162 # Concatenate variables together. The first argument is a list of variables
163 # names to concatenate. The second argument is an optional prefix for the
164 # variables and the third is the string to use as separator (" ~" if omitted).
168 # $(call varcat,A B,X_, --)
169 # Will produce something like "a -- b --"
170 varcat = $(foreach v,$1,$($2$v)$(if $3,$3, ~))
172 # Replace variables with specified values in a template file. The first
173 # argument is a list of make variables names which will be replaced in the
174 # target file. The strings @VARNAME@ in the template file will be replaced
175 # with the value of the make $(VARNAME) variable and the result will be stored
176 # in the target file. The second (optional) argument is a prefix to add to the
177 # make variables names, so if the prefix is PREFIX_ and @VARNAME@ is found in
178 # the template file, it will be replaced by the value of the make variable
179 # $(PREFIX_VARNAME). The third and fourth arguments are the source file and
180 # the destination file (both optional, $< and $@ are used if omitted). The
181 # fifth (optional) argument are options to pass to the substitute sed command
182 # (for example, use "g" if you want to do multiple substitutions per line).
183 replace = $(call exec,sed '$(foreach v,$1,s|@$v@|$($2$v)|$5;)' $(if $3,$3,$<) \
186 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
187 # argument is the name of symlink to create. The link is only created if it
188 # doesn't already exist.
189 symlink_include_dir = $(shell \
190 test -L $(INCLUDE_DIR)/$1 \
191 || ln -s $C $(INCLUDE_DIR)/$1 )
193 # Create a file with flags used to trigger rebuilding when they change. The
194 # first argument is the name of the file where to store the flags, the second
195 # are the flags and the third argument is a text to be displayed if the flags
196 # have changed (optional). This should be used as a rule action or something
197 # where a shell script is expected.
198 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
199 $(if $3,test -f $1 && echo "$3";) \
206 # Warn about everything
207 override CPPFLAGS += -Wall
209 # Use the includes directories to search for includes
210 override CPPFLAGS += -I$(INCLUDE_DIR)
212 # Let the program know where it will be installed
213 override CPPFLAGS += -DPREFIX=$(prefix)
215 # Be standard compilant
216 override CFLAGS += -std=c99 -pedantic
217 override CXXFLAGS += -std=c++98 -pedantic
219 # Use the generated library directory to for libraries
220 override LDFLAGS += -L$L -Wall
222 # Make sure the generated libraries can be found
223 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
230 override CPPFLAGS += -ggdb -DDEBUG
234 override CPPFLAGS += -O2 -DNDEBUG
238 override CPPFLAGS += -ggdb -pg --coverage
239 override LDFLAGS += -pg --coverage
243 # Automatic dependency handling
244 ################################
246 # These files are created during compilation.
247 sinclude $(shell test -d $O && find $O -name '*.d')
254 $O/%.o: $T/%.c $G/compile-c-flags
257 # Compile C++ objects
258 $O/%.o: $T/%.cpp $G/compile-cpp-flags
261 # Link binary programs
262 $B/%: $G/link-o-flags
265 # Link shared libraries
266 $L/%.so: override CFLAGS += -fPIC
267 $L/%.so: override CXXFLAGS += -fPIC
268 $L/%.so: $G/link-o-flags
271 # Create pkg-config files using a template
273 $(call replace,$(PC_VARS),$*-PC-)
275 # Install binary programs
277 $(call install_file,0755)
279 # Install system binary programs
281 $(call install_file,0755)
283 # Install pkg-config specification files
293 $(call exec,$(RM) -r $D,$D)
295 # Phony rule to uninstall all built targets (like "install", uses $(install)).
298 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
300 # These rules use the "Secondary Expansion" GNU Make feature, to allow
301 # sub-makes to add values to the special variables $(all), after this makefile
305 # Phony rule to make all the targets (sub-makefiles can append targets to build
306 # to the $(all) variable).
310 # Phony rule to install all built targets (sub-makefiles can append targets to
311 # build to the $(install) variable).
316 # Create build directory structure
317 ###################################
319 # Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory
320 # structure of the project into $O. Create one symlink "last" to the current
323 # NOTE: the second mkdir can yield no arguments if the project don't have any
324 # subdirectories, that's why the current directory "." is included, so it
325 # won't show an error message in case of no subdirectories.
326 setup_build_dir__ := $(shell \
327 mkdir -p $O $B $L $(INCLUDE_DIR); \
328 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
329 $(shell find $T -type d -not -path '$D*'))); \
330 test -L $D/last || ln -s $F $D/last )
333 # Automatic rebuilding when flags or commands changes
334 ######################################################
336 # Re-compile C files if one of this variables changes
337 COMPILE.c.FLAGS := $(call varcat,CC CPPFLAGS CFLAGS TARGET_ARCH prefix)
339 # Re-compile C++ files if one of this variables changes
340 COMPILE.cpp.FLAGS := $(call varcat,CXX CPPFLAGS CXXFLAGS TARGET_ARCH prefix)
342 # Re-link binaries and libraries if one of this variables changes
343 LINK.o.FLAGS := $(call varcat,LD LDFLAGS TARGET_ARCH)
345 # Create files containing the current flags to trigger a rebuild if they change
346 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
347 $(COMPILE.c.FLAGS),C compiler or flags; )
348 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
349 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
350 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
351 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
353 # Print any generated message (if verbose)
354 $(if $V,$(if $(setup_flag_files__), \
355 $(info !! Something changed: $(setup_flag_files__)re-building \