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 # Compare two strings, if they are the same, returns the string, if not,
63 eq = $(if $(subst $1,,$2),,$1)
65 # Find sources files and get the corresponding object names
66 # The first argument should be the sources extension ("c" or "cpp" typically)
67 # It expects the variable $T and $O to be defined as commented previously in
68 # this file. $C should be defined to the path to the current directory relative
70 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $T/$C -name '*.$1'))
72 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
73 # directory, so it can be displayed as if it were a relative directory. Take
74 # just one argument, the file name.
75 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
76 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1, \
77 $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
78 $(call abbr_helper,$1)))
80 # Execute a command printing a nice message if $V is @.
81 # The first argument is mandatory and it's the command to execute. The second
82 # and third arguments are optional and are the target name and command name to
85 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
86 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
88 # Same as vexec but it silence the echo command (prepending a @ if $V).
89 exec = $V$(call vexec,$1,$2,$3)
91 # Compile a source file to an object, generating pre-compiled headers and
92 # dependencies. The pre-compiled headers are generated only if the system
93 # includes change. This function is designed to be used as a command in a rule.
94 # It takes one argument only, the type of file to compile (typically "c" or
95 # "cpp"). What to compile and the output files are built using the automatic
96 # variables from a rule.
99 $Vif test -f $O/$*.d; then \
101 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
102 grep -h '^#include <' $(call abbr,$<) $$h | sort -u > "$$tmp"; \
103 if diff -q -w "$(call abbr,$O/$*.$1.h)" "$$tmp" > /dev/null 2>&1; \
107 mv "$$tmp" "$(call abbr,$O/$*.$1.h)"; \
108 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
112 touch "$(call abbr,$O/$*.$1.h)"; \
115 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
118 # Link object files to build an executable. The objects files are taken from
119 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
120 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
121 # function is designed to be used as a command in a rule. The ouput name is
122 # taken from the rule automatic variables. If an argument is provided, it's
123 # included in the link command line. The variable LINKER is used to link the
124 # executable; for example, if you want to link a C++ executable, you should use
126 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
127 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
128 $(foreach obj,$(filter %.o,$^),$(obj)))
130 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
131 # argument is the name of symlink to create. The link is only created if it
132 # doesn't already exist.
133 symlink_include_dir = $(shell \
134 test -L $(INCLUDE_DIR)/$1 \
135 || ln -s $T/$C $(INCLUDE_DIR)/$1 )
141 # Warn about everything
142 override CPPFLAGS += -Wall
144 # Use the includes directories to search for includes
145 override CPPFLAGS += -I$(INCLUDE_DIR)
147 # Be standard compilant
148 override CFLAGS += -std=c99 -pedantic
149 override CXXFLAGS += -std=c++98 -pedantic
151 # Use the generated library directory to for libraries
152 override LDFLAGS += -L$L -Wall
154 # Make sure the generated libraries can be found
155 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
162 override CPPFLAGS += -ggdb -DDEBUG
166 override CPPFLAGS += -O2 -DNDEBUG
170 override CPPFLAGS += -ggdb -pg --coverage
171 override LDFLAGS += -pg --coverage
175 # Automatic dependency handling
176 ################################
178 # These files are created during compilation.
179 sinclude $(shell test -d $O && find $O -name '*.d')
185 $O/%.o: $T/%.c $G/compile-c-flags
188 $O/%.o: $T/%.cpp $G/compile-cpp-flags
191 $B/%: $G/link-o-flags
194 $L/%.so: override CFLAGS += -fPIC
195 $L/%.so: override CXXFLAGS += -fPIC
196 $L/%.so: $G/link-o-flags
201 $(call exec,$(RM) -r $D,$D)
203 # These rules use the "Secondary Expansion" GNU Make feature, to allow
204 # sub-makes to add values to the special variables $(all), after this makefile
208 # Phony rule to make all the targets (sub-makefiles can append targets to build
209 # to the $(all) variable).
214 # Create build directory structure
215 ###################################
218 # Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory
219 # structure of the project into $O. Create one symlink "last" to the current
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 $(INCLUDE_DIR); \
227 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
228 $(shell find $T -type d -not -path '$D*'))); \
229 test -L $D/last || ln -s $F $D/last )
232 # Automatic rebuilding when flags or commands changes
233 ######################################################
235 # Re-compile C files if one of this variables changes
236 COMPILE.c.FLAGS += $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH)
238 # Re-compile C++ files if one of this variables changes
239 COMPILE.cpp.FLAGS += $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH)
241 # Re-link binaries and libraries if one of this variables changes
242 LINK.o.FLAGS += $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
244 # Create a file with flags used to trigger rebuilding when they change. The
245 # first argument is the name of the file where to store the flags, the second
246 # are the flags and the third argument is a text to be displayed if the flags
247 # have changed. This should be used as a rule action or something where
248 # a shell script is expected.
249 gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \
250 test -f $1 && echo "$3"; \
253 # Create files containing the current flags to trigger a rebuild if they change
254 setup_flag_files__ := $(shell \
255 $(call gen_rebuild_flags,$G/compile-c-flags, \
256 $(COMPILE.c.FLAGS),C compiler or flags;); \
257 $(call gen_rebuild_flags,$G/compile-cpp-flags, \
258 $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \
259 $(call gen_rebuild_flags,$G/link-o-flags, \
260 $(LINK.o.FLAGS),linker or link flags;) )
262 # Print any generated message (if verbose)
263 $(if $V,$(if $(setup_flag_files__), \
264 $(info !! Something changed: $(setup_flag_files__) \
265 re-building affected files...)))