2 ifndef Lib.mak.included
5 # These variables should be provided by the includer Makefile:
6 # P should be the project name, mostly used to handle include directories
7 # T should be the path to the top-level directory.
8 # C should be the path to the current directory.
10 # Load top-level directory local configuration
11 sinclude $T/Config.mak
13 # Verbosity flag (empty show nice messages, 1 be verbose)
15 override V := $(if $(findstring s,$(MAKEFLAGS)),1,$V)
17 # Flavor (variant), should be one of "dbg", "opt" or "cov"
20 # Use C++ linker by default
23 # Use precompiled headers if non-empty
30 # Use absolute paths to avoid problems with automatic dependencies when
31 # building from subdirectories
34 # Name of the current directory, relative to $T
35 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
37 # Base directory where to put variants
40 # Generated files top directory
43 # Objects (and other garbage like precompiled headers and dependency files)
56 # Generated includes directory
63 # Find sources files and get the corresponding object names
64 # The first argument should be the sources extension ("c" or "cpp" typically)
65 # It expects the variable $T and $O to be defined as commented previously in
66 # this file. $C should be defined to the path to the current directory relative
68 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $T/$C -name '*.$1'))
70 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
71 # directory, so it can be displayed as if it were a relative directory. Take
72 # just one argument, the file name.
73 abbr = $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
74 $(subst $T,.,$(patsubst $T/%,%,$1)))
76 # Execute a command printing a nice message if $V is empty
77 # The first argument is mandatory and it's the command to execute. The second
78 # and third arguments are optional and are the target name and command name to
81 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
82 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
84 # Same as vexec but it silence the echo command (prepending a @).
85 exec = $(if $V,,@)$(call vexec,$1,$2,$3)
87 # Compile a source file to an object, generating pre-compiled headers and
88 # dependencies. The pre-compiled headers are generated only if the system
89 # includes change. This function is designed to be used as a command in a rule.
90 # It takes one argument only, the type of file to compile (typically "c" or
91 # "cpp"). What to compile and the output files are built using the automatic
92 # variables from a rule.
95 @if test -f $O/$*.d; then \
97 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
98 grep -h '^#include <' $(call abbr,$<) $$h | sort -u > "$$tmp"; \
99 if diff -q -w "$(call abbr,$O/$*.$1.h)" "$$tmp" > /dev/null 2>&1; \
103 mv "$$tmp" "$(call abbr,$O/$*.$1.h)"; \
104 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
108 touch "$(call abbr,$O/$*.$1.h)"; \
111 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
114 # Link object files to build an executable. The objects files are taken from
115 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
116 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
117 # function is designed to be used as a command in a rule. The ouput name is
118 # taken from the rule automatic variables. If an argument is provided, it's
119 # included in the link command line. The variable LINKER is used to link the
120 # executable; for example, if you want to link a C++ executable, you should use
122 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
123 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
124 $(foreach obj,$(filter %.o,$^),$(obj)))
130 # Warn about everything
131 override CPPFLAGS += -Wall
133 # Use the includes directories to search for includes
134 override CPPFLAGS += -I$I -I$J
136 # Be standard compilant
137 override CFLAGS += -std=c99 -pedantic
138 override CXXFLAGS += -std=c++98 -pedantic
140 # Use the generated library directory to for libraries
141 override LDFLAGS += -L$L -Wall
143 # Make sure the generated libraries can be found
144 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
151 override CPPFLAGS += -ggdb -DDEBUG
155 override CPPFLAGS += -O2 -DNDEBUG
159 override CPPFLAGS += -ggdb -pg --coverage
160 override LDFLAGS += -pg --coverage
164 # Automatic rebuilding when flags or commands changes
165 ######################################################
167 # Re-compile C files if one of this variables changes
168 COMPILE.c.FLAGS := $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH)
170 # Re-compile C++ files if one of this variables changes
171 COMPILE.cpp.FLAGS := $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH)
173 # Re-link binaries and libraries if one of this variables changes
174 LINK.o.FLAGS := $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
180 $O/%.o: $T/%.c $G/compile-c-flags
183 $O/%.o: $T/%.cpp $G/compile-cpp-flags
186 $B/%: $G/link-o-flags
189 $L/%.so: override CFLAGS += -fPIC
190 $L/%.so: override CXXFLAGS += -fPIC
191 $L/%.so: $G/link-o-flags
196 $(call exec,$(RM) -r $D,$D)
199 # Automatic dependency handling
200 ################################
202 sinclude $(shell test -d $O && find $O -name '*.d')
205 # Create build directory structure
206 ###################################
208 # Create a file with flags used to trigger rebuilding when they change. The
209 # first argument is the name of the file where to store the flags, the second
210 # are the flags and the third argument is a text to be displayed if the flags
211 # have changed. This should be used as a rule action or something where
212 # a shell script is expected.
213 gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \
214 test -f $1 && echo "$3"; \
217 # Create $O, $B, $L, $I and $J directories and replicate the directory
218 # structure of the project into $O. Create one symlink "last" to the current
219 # build directory and another to use as include directory. It update the flags
220 # files to detect flag and/or compiler changes to force a rebuild.
222 # NOTE: the second mkdir can yield no arguments if the project don't have any
223 # subdirectories, that's why the current directory "." is included, so it
224 # won't show an error message in case of no subdirectories.
225 setup_build_dir__ := $(shell \
226 mkdir -p $O $B $L $I $J; \
227 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
228 $(shell find $T -type d -not -path '$D*'))); \
229 $(call gen_rebuild_flags,$G/compile-c-flags, \
230 $(COMPILE.c.FLAGS),C compiler or flags;); \
231 $(call gen_rebuild_flags,$G/compile-cpp-flags, \
232 $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \
233 $(call gen_rebuild_flags,$G/link-o-flags, \
234 $(LINK.o.FLAGS),linker or link flags;); \
235 test -L $I/$P || ln -s $T $I/$P; \
236 test -L $D/last || ln -s $F $D/last )
238 # Print any generated message (if verbose)
239 $(if $V,,$(if $(setup_build_dir__), \
240 $(info !! Something changed: $(setup_build_dir__) \
241 re-building affected files...)))
243 # Include the Build.mak for this directory
246 # Phony rule to make all the targets (sub-makefiles can append targets to build
247 # to the $(all) variable).