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 # Verbosity flag (empty show nice messages, 1 be verbose)
12 override V := $(if $(findstring s,$(MAKEFLAGS)),1,$V)
14 # Flavor (variant), should be one of "dbg", "opt" or "cov"
17 # Use C++ linker by default
20 # Use precompiled headers if non-empty
27 # Use absolute paths to avoid problems with automatic dependencies when
28 # building from subdirectories
31 # Name of the current directory, relative to $T
32 R := $(subst $T,,$(patsubst $T/%,%,$(CURDIR)))
34 # Base directory where to put variants
37 # Generated files top directory
40 # Objects (and other garbage like precompiled headers and dependency files)
53 # Generated includes directory
60 # Find sources files and get the corresponding object names
61 # The first argument should be the sources extension ("c" or "cpp" typically)
62 # It expects the variable $T and $O to be defined as commented previously in
63 # this file. $C should be defined to the path to the current directory relative
65 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $T/$C -name '*.$1'))
67 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
68 # directory, so it can be displayed as if it were a relative directory. Take
69 # just one argument, the file name.
70 abbr = $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
71 $(subst $T,.,$(patsubst $T/%,%,$1)))
73 # Execute a command printing a nice message if $V is empty
74 # The first argument is mandatory and it's the command to execute. The second
75 # and third arguments are optional and are the target name and command name to
78 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
79 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
81 # Same as vexec but it silence the echo command (prepending a @).
82 exec = $(if $V,,@)$(call vexec,$1,$2,$3)
84 # Compile a source file to an object, generating pre-compiled headers and
85 # dependencies. The pre-compiled headers are generated only if the system
86 # includes change. This function is designed to be used as a command in a rule.
87 # It takes one argument only, the type of file to compile (typically "c" or
88 # "cpp"). What to compile and the output files are built using the automatic
89 # variables from a rule.
92 @if test -f $O/$*.d; then \
94 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
95 grep -h '^#include <' $(call abbr,$<) $$h | sort -u > "$$tmp"; \
96 if diff -q -w "$(call abbr,$O/$*.$1.h)" "$$tmp" > /dev/null 2>&1; \
100 mv "$$tmp" "$(call abbr,$O/$*.$1.h)"; \
101 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
105 touch "$(call abbr,$O/$*.$1.h)"; \
108 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
111 # Link object files to build an executable. The objects files are taken from
112 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
113 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
114 # function is designed to be used as a command in a rule. The ouput name is
115 # taken from the rule automatic variables. If an argument is provided, it's
116 # included in the link command line. The variable LINKER is used to link the
117 # executable; for example, if you want to link a C++ executable, you should use
119 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
120 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
121 $(foreach obj,$(filter %.o,$^),$(obj)))
127 # Warn about everything
128 override CPPFLAGS += -Wall
130 # Use the includes directories to search for includes
131 override CPPFLAGS += -I$I -I$J
133 # Be standard compilant
134 override CFLAGS += -std=c99 -pedantic
135 override CXXFLAGS += -std=c++98 -pedantic
137 # Use the generated library directory to for libraries
138 override LDFLAGS += -L$L -Wall
140 # Make sure the generated libraries can be found
141 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
148 override CPPFLAGS += -ggdb -DDEBUG
152 override CPPFLAGS += -O2 -DNDEBUG
156 override CPPFLAGS += -ggdb -pg --coverage
157 override LDFLAGS += -pg --coverage
161 # Automatic rebuilding when flags or commands changes
162 ######################################################
164 # Re-compile C files if one of this variables changes
165 COMPILE.c.FLAGS := $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH)
167 # Re-compile C++ files if one of this variables changes
168 COMPILE.cpp.FLAGS := $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH)
170 # Re-link binaries and libraries if one of this variables changes
171 LINK.o.FLAGS := $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
177 $O/%.o: $T/%.c $G/compile-c-flags
180 $O/%.o: $T/%.cpp $G/compile-cpp-flags
183 $B/%: $G/link-o-flags
186 $L/%.so: override CFLAGS += -fPIC
187 $L/%.so: override CXXFLAGS += -fPIC
188 $L/%.so: $G/link-o-flags
193 $(call exec,$(RM) -r $D,$D)
196 # Automatic dependency handling
197 ################################
199 sinclude $(shell test -d $O && find $O -name '*.d')
202 # Create build directory structure
203 ###################################
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. This should be used as a rule action or something where
209 # a shell script is expected.
210 gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \
211 test -f $1 && echo "$3"; \
214 # Create $O, $B, $L, $I and $J directories and replicate the directory
215 # structure of the project into $O. Create one symlink "last" to the current
216 # build directory and another to use as include directory. It update the flags
217 # files to detect flag and/or compiler changes to force a rebuild.
219 # NOTE: the second mkdir can yield no arguments if the project don't have any
220 # subdirectories, that's why the current directory "." is included, so it
221 # won't show an error message in case of no subdirectories.
222 setup_build_dir__ := $(shell \
223 mkdir -p $O $B $L $I $J; \
224 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
225 $(shell find $T -type d -not -path '$D*'))); \
226 $(call gen_rebuild_flags,$G/compile-c-flags, \
227 $(COMPILE.c.FLAGS),C compiler or flags;); \
228 $(call gen_rebuild_flags,$G/compile-cpp-flags, \
229 $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \
230 $(call gen_rebuild_flags,$G/link-o-flags, \
231 $(LINK.o.FLAGS),linker or link flags;); \
232 test -L $I/$P || ln -s $T $I/$P; \
233 test -L $D/last || ln -s $F $D/last )
235 # Print any generated message (if verbose)
236 $(if $V,,$(if $(setup_build_dir__), \
237 $(info !! Something changed: $(setup_build_dir__) \
238 re-building affected files...)))
240 # Include the Build.mak for this directory
243 # Phony rule to make all the targets (sub-makefiles can append targets to build
244 # to the $(all) variable).