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 # 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
76 # Compare two strings, if they are the same, returns the string, if not,
78 eq = $(if $(subst $1,,$2),,$1)
80 # Find sources files and get the corresponding object names
81 # The first argument should be the sources extension ("c" or "cpp" typically)
82 # It expects the variable $T and $O to be defined as commented previously in
83 # this file. $C should be defined to the path to the current directory relative
85 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $T/$C -name '*.$1'))
87 # Find sources files and get the corresponding object names
88 # The first argument should be the sources extension ("c" or "cpp" typically)
89 # It expects the variable $T and $O to be defined as commented previously in
90 # this file. $C should be defined to the path to the current directory relative
92 find_headers = $(patsubst $T/$C/%.$1,$2/%.$1,$(shell find $T/$C -name '*.$1'))
94 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
95 # directory, so it can be displayed as if it were a relative directory. Take
96 # just one argument, the file name.
97 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
98 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1, \
99 $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
100 $(call abbr_helper,$1)))
102 # Execute a command printing a nice message if $V is @.
103 # The first argument is mandatory and it's the command to execute. The second
104 # and third arguments are optional and are the target name and command name to
107 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
108 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
110 # Same as vexec but it silence the echo command (prepending a @ if $V).
111 exec = $V$(call vexec,$1,$2,$3)
113 # Compile a source file to an object, generating pre-compiled headers and
114 # dependencies. The pre-compiled headers are generated only if the system
115 # includes change. This function is designed to be used as a command in a rule.
116 # It takes one argument only, the type of file to compile (typically "c" or
117 # "cpp"). What to compile and the output files are built using the automatic
118 # variables from a rule.
121 $Vif test -f $O/$*.d; then \
123 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
124 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
125 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
129 mv "$$tmp" "$O/$*.$1.h"; \
130 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
134 touch "$O/$*.$1.h"; \
137 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
140 # Link object files to build an executable. The objects files are taken from
141 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
142 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
143 # function is designed to be used as a command in a rule. The ouput name is
144 # taken from the rule automatic variables. If an argument is provided, it's
145 # included in the link command line. The variable LINKER is used to link the
146 # executable; for example, if you want to link a C++ executable, you should use
148 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
149 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
150 $(foreach obj,$(filter %.o,$^),$(obj)))
152 # Install a file. All arguments are optional. The first argument is the file
153 # mode (defaults to 0644). The second argument are extra flags to the install
154 # command (defaults to -D). The third argument is the source file to install
155 # (defaults to $<) and the last one is the destination (defaults to $@).
156 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
157 $(if $3,$3,$<) $(if $4,$4,$@))
159 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
160 # argument is the name of symlink to create. The link is only created if it
161 # doesn't already exist.
162 symlink_include_dir = $(shell \
163 test -L $(INCLUDE_DIR)/$1 \
164 || ln -s $T/$C $(INCLUDE_DIR)/$1 )
170 # Warn about everything
171 override CPPFLAGS += -Wall
173 # Use the includes directories to search for includes
174 override CPPFLAGS += -I$(INCLUDE_DIR)
176 # Let the program know where it will be installed
177 override CPPFLAGS += -DPREFIX=$(prefix)
179 # Be standard compilant
180 override CFLAGS += -std=c99 -pedantic
181 override CXXFLAGS += -std=c++98 -pedantic
183 # Use the generated library directory to for libraries
184 override LDFLAGS += -L$L -Wall
186 # Make sure the generated libraries can be found
187 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
194 override CPPFLAGS += -ggdb -DDEBUG
198 override CPPFLAGS += -O2 -DNDEBUG
202 override CPPFLAGS += -ggdb -pg --coverage
203 override LDFLAGS += -pg --coverage
207 # Automatic dependency handling
208 ################################
210 # These files are created during compilation.
211 sinclude $(shell test -d $O && find $O -name '*.d')
217 $O/%.o: $T/%.c $G/compile-c-flags
220 $O/%.o: $T/%.cpp $G/compile-cpp-flags
223 $B/%: $G/link-o-flags
226 $L/%.so: override CFLAGS += -fPIC
227 $L/%.so: override CXXFLAGS += -fPIC
228 $L/%.so: $G/link-o-flags
232 $(call install_file,0755)
235 $(call install_file,0755)
242 $(call exec,$(RM) -r $D,$D)
244 # Phony rule to uninstall all built targets (like "install", uses $(install)).
247 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
249 # These rules use the "Secondary Expansion" GNU Make feature, to allow
250 # sub-makes to add values to the special variables $(all), after this makefile
254 # Phony rule to make all the targets (sub-makefiles can append targets to build
255 # to the $(all) variable).
259 # Phony rule to install all built targets (sub-makefiles can append targets to
260 # build to the $(install) variable).
265 # Create build directory structure
266 ###################################
268 # Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory
269 # structure of the project into $O. Create one symlink "last" to the current
272 # NOTE: the second mkdir can yield no arguments if the project don't have any
273 # subdirectories, that's why the current directory "." is included, so it
274 # won't show an error message in case of no subdirectories.
275 setup_build_dir__ := $(shell \
276 mkdir -p $O $B $L $(INCLUDE_DIR); \
277 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
278 $(shell find $T -type d -not -path '$D*'))); \
279 test -L $D/last || ln -s $F $D/last )
282 # Automatic rebuilding when flags or commands changes
283 ######################################################
285 # Re-compile C files if one of this variables changes
286 COMPILE.c.FLAGS += $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH) ~ $(prefix)
288 # Re-compile C++ files if one of this variables changes
289 COMPILE.cpp.FLAGS += $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH) \
292 # Re-link binaries and libraries if one of this variables changes
293 LINK.o.FLAGS += $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
295 # Create a file with flags used to trigger rebuilding when they change. The
296 # first argument is the name of the file where to store the flags, the second
297 # are the flags and the third argument is a text to be displayed if the flags
298 # have changed. This should be used as a rule action or something where
299 # a shell script is expected.
300 gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \
301 test -f $1 && echo "$3"; \
304 # Create files containing the current flags to trigger a rebuild if they change
305 setup_flag_files__ := $(shell \
306 $(call gen_rebuild_flags,$G/compile-c-flags, \
307 $(COMPILE.c.FLAGS),C compiler or flags;); \
308 $(call gen_rebuild_flags,$G/compile-cpp-flags, \
309 $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \
310 $(call gen_rebuild_flags,$G/link-o-flags, \
311 $(LINK.o.FLAGS),linker or link flags;) )
313 # Print any generated message (if verbose)
314 $(if $V,$(if $(setup_flag_files__), \
315 $(info !! Something changed: $(setup_flag_files__) \
316 re-building affected files...)))