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 # C should be the path to the current directory.
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 # 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)
55 INCLUDE_DIR ?= $G/include
61 # Find sources files and get the corresponding object names
62 # The first argument should be the sources extension ("c" or "cpp" typically)
63 # It expects the variable $T and $O to be defined as commented previously in
64 # this file. $C should be defined to the path to the current directory relative
66 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $T/$C -name '*.$1'))
68 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
69 # directory, so it can be displayed as if it were a relative directory. Take
70 # just one argument, the file name.
71 abbr = $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
72 $(subst $T,.,$(patsubst $T/%,%,$1)))
74 # Execute a command printing a nice message if $V is @.
75 # The first argument is mandatory and it's the command to execute. The second
76 # and third arguments are optional and are the target name and command name to
79 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
80 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
82 # Same as vexec but it silence the echo command (prepending a @ if $V).
83 exec = $V$(call vexec,$1,$2,$3)
85 # Compile a source file to an object, generating pre-compiled headers and
86 # dependencies. The pre-compiled headers are generated only if the system
87 # includes change. This function is designed to be used as a command in a rule.
88 # It takes one argument only, the type of file to compile (typically "c" or
89 # "cpp"). What to compile and the output files are built using the automatic
90 # variables from a rule.
93 $Vif test -f $O/$*.d; then \
95 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
96 grep -h '^#include <' $(call abbr,$<) $$h | sort -u > "$$tmp"; \
97 if diff -q -w "$(call abbr,$O/$*.$1.h)" "$$tmp" > /dev/null 2>&1; \
101 mv "$$tmp" "$(call abbr,$O/$*.$1.h)"; \
102 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
106 touch "$(call abbr,$O/$*.$1.h)"; \
109 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
112 # Link object files to build an executable. The objects files are taken from
113 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
114 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
115 # function is designed to be used as a command in a rule. The ouput name is
116 # taken from the rule automatic variables. If an argument is provided, it's
117 # included in the link command line. The variable LINKER is used to link the
118 # executable; for example, if you want to link a C++ executable, you should use
120 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
121 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
122 $(foreach obj,$(filter %.o,$^),$(obj)))
124 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
125 # argument is the name of symlink to create. The link is only created if it
126 # doesn't already exist.
127 symlink_include_dir = $(shell \
128 test -L $(INCLUDE_DIR)/$1 \
129 || ln -s $T/$C $(INCLUDE_DIR)/$1 )
135 # Warn about everything
136 override CPPFLAGS += -Wall
138 # Use the includes directories to search for includes
139 override CPPFLAGS += -I$(INCLUDE_DIR)
141 # Be standard compilant
142 override CFLAGS += -std=c99 -pedantic
143 override CXXFLAGS += -std=c++98 -pedantic
145 # Use the generated library directory to for libraries
146 override LDFLAGS += -L$L -Wall
148 # Make sure the generated libraries can be found
149 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
156 override CPPFLAGS += -ggdb -DDEBUG
160 override CPPFLAGS += -O2 -DNDEBUG
164 override CPPFLAGS += -ggdb -pg --coverage
165 override LDFLAGS += -pg --coverage
169 # Automatic dependency handling
170 ################################
172 # These files are created during compilation.
173 sinclude $(shell test -d $O && find $O -name '*.d')
179 $O/%.o: $T/%.c $G/compile-c-flags
182 $O/%.o: $T/%.cpp $G/compile-cpp-flags
185 $B/%: $G/link-o-flags
188 $L/%.so: override CFLAGS += -fPIC
189 $L/%.so: override CXXFLAGS += -fPIC
190 $L/%.so: $G/link-o-flags
195 $(call exec,$(RM) -r $D,$D)
197 # These rules use the "Secondary Expansion" GNU Make feature, to allow
198 # sub-makes to add values to the special variables $(all), after this makefile
202 # Phony rule to make all the targets (sub-makefiles can append targets to build
203 # to the $(all) variable).
208 # Create build directory structure
209 ###################################
212 # Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory
213 # structure of the project into $O. Create one symlink "last" to the current
216 # NOTE: the second mkdir can yield no arguments if the project don't have any
217 # subdirectories, that's why the current directory "." is included, so it
218 # won't show an error message in case of no subdirectories.
219 setup_build_dir__ := $(shell \
220 mkdir -p $O $B $L $(INCLUDE_DIR); \
221 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
222 $(shell find $T -type d -not -path '$D*'))); \
223 test -L $D/last || ln -s $F $D/last )
226 # Automatic rebuilding when flags or commands changes
227 ######################################################
229 # Re-compile C files if one of this variables changes
230 COMPILE.c.FLAGS += $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH)
232 # Re-compile C++ files if one of this variables changes
233 COMPILE.cpp.FLAGS += $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH)
235 # Re-link binaries and libraries if one of this variables changes
236 LINK.o.FLAGS += $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
238 # Create a file with flags used to trigger rebuilding when they change. The
239 # first argument is the name of the file where to store the flags, the second
240 # are the flags and the third argument is a text to be displayed if the flags
241 # have changed. This should be used as a rule action or something where
242 # a shell script is expected.
243 gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \
244 test -f $1 && echo "$3"; \
247 # Create files containing the current flags to trigger a rebuild if they change
248 setup_flag_files__ := $(shell \
249 $(call gen_rebuild_flags,$G/compile-c-flags, \
250 $(COMPILE.c.FLAGS),C compiler or flags;); \
251 $(call gen_rebuild_flags,$G/compile-cpp-flags, \
252 $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \
253 $(call gen_rebuild_flags,$G/link-o-flags, \
254 $(LINK.o.FLAGS),linker or link flags;) )
256 # Print any generated message (if verbose)
257 $(if $V,$(if $(setup_flag_files__), \
258 $(info !! Something changed: $(setup_flag_files__) \
259 re-building affected files...)))