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 # S should be sub-directory where the current makefile is, relative to $T.
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
72 # Directory of the current makefile (this might not be the same as $(CURDIR)
73 # This variable is "lazy" because $S changes all the time, so it should be
74 # evaluated in the context where $C is used, not here.
81 # Compare two strings, if they are the same, returns the string, if not,
83 eq = $(if $(subst $1,,$2),,$1)
85 # Find sources files and get the corresponding object names
86 # The first argument should be the sources extension ("c" or "cpp" typically)
87 # It expects the variable $T and $O to be defined as commented previously in
89 find_objects = $(patsubst $T/%.$1,$O/%.o,$(shell find $C -name '*.$1'))
91 # Find sources files and get the corresponding object names
92 # The first argument should be the sources extension ("c" or "cpp" typically)
93 # It expects the variable $T and $O to be defined as commented previously in
95 find_headers = $(patsubst $C/%.$1,$2/%.$1,$(shell find $C -name '*.$1'))
97 # Abbreviate a file name. Cut the leading part of a file if it match to the $T
98 # directory, so it can be displayed as if it were a relative directory. Take
99 # just one argument, the file name.
100 abbr_helper = $(subst $T,.,$(patsubst $T/%,%,$1))
101 abbr = $(if $(call eq,$(call abbr_helper,$1),$1),$1, \
102 $(addprefix $(shell echo $R | sed 's|/\?\([^/]\+\)/\?|../|g'),\
103 $(call abbr_helper,$1)))
105 # Execute a command printing a nice message if $V is @.
106 # The first argument is mandatory and it's the command to execute. The second
107 # and third arguments are optional and are the target name and command name to
110 echo ' $(notdir $(if $3,$(strip $3),$(firstword $1))) \
111 $(call abbr,$(if $2,$(strip $2),$@))' ; )$1
113 # Same as vexec but it silence the echo command (prepending a @ if $V).
114 exec = $V$(call vexec,$1,$2,$3)
116 # Compile a source file to an object, generating pre-compiled headers and
117 # dependencies. The pre-compiled headers are generated only if the system
118 # includes change. This function is designed to be used as a command in a rule.
119 # It takes one argument only, the type of file to compile (typically "c" or
120 # "cpp"). What to compile and the output files are built using the automatic
121 # variables from a rule.
124 $Vif test -f $O/$*.d; then \
126 h=`awk -F: '!$$0 {f = 1} $$0 && f {print $$1}' $O/$*.d`; \
127 grep -h '^#include <' $< $$h | sort -u > "$$tmp"; \
128 if diff -q -w "$O/$*.$1.h" "$$tmp" > /dev/null 2>&1; \
132 mv "$$tmp" "$O/$*.$1.h"; \
133 $(call vexec,$(COMPILE.$1) -o "$O/$*.$1.h.gch" "$O/$*.$1.h",\
137 touch "$O/$*.$1.h"; \
140 $(call exec,$(COMPILE.$1) -o $@ -MMD -MP $(if $(GCH),-include $O/$*.$1.h) $<)
143 # Link object files to build an executable. The objects files are taken from
144 # the prerequisite files ($O/%.o). If in the prerequisite files are shared
145 # objects ($L/lib%.so), they are included as libraries to link to (-l%). This
146 # function is designed to be used as a command in a rule. The ouput name is
147 # taken from the rule automatic variables. If an argument is provided, it's
148 # included in the link command line. The variable LINKER is used to link the
149 # executable; for example, if you want to link a C++ executable, you should use
151 link = $(call exec,$(LINKER) $(LDFLAGS) $(TARGET_ARCH) -o $@ $1 \
152 $(patsubst $L/lib%.so,-l%,$(filter %.so,$^)) \
153 $(foreach obj,$(filter %.o,$^),$(obj)))
155 # Install a file. All arguments are optional. The first argument is the file
156 # mode (defaults to 0644). The second argument are extra flags to the install
157 # command (defaults to -D). The third argument is the source file to install
158 # (defaults to $<) and the last one is the destination (defaults to $@).
159 install_file = $(call exec,install -m $(if $1,$1,0644) $(if $2,$2,-D) \
160 $(if $3,$3,$<) $(if $4,$4,$@))
162 # Create a symbolic link to the project under the $(INCLUDE_DIR). The first
163 # argument is the name of symlink to create. The link is only created if it
164 # doesn't already exist.
165 symlink_include_dir = $(shell \
166 test -L $(INCLUDE_DIR)/$1 \
167 || ln -s $C $(INCLUDE_DIR)/$1 )
169 # Create a file with flags used to trigger rebuilding when they change. The
170 # first argument is the name of the file where to store the flags, the second
171 # are the flags and the third argument is a text to be displayed if the flags
172 # have changed (optional). This should be used as a rule action or something
173 # where a shell script is expected.
174 gen_rebuild_flags = $(shell if test x"$2" != x"`cat $1 2>/dev/null`"; then \
175 $(if $3,test -f $1 && echo "$3";) \
182 # Warn about everything
183 override CPPFLAGS += -Wall
185 # Use the includes directories to search for includes
186 override CPPFLAGS += -I$(INCLUDE_DIR)
188 # Let the program know where it will be installed
189 override CPPFLAGS += -DPREFIX=$(prefix)
191 # Be standard compilant
192 override CFLAGS += -std=c99 -pedantic
193 override CXXFLAGS += -std=c++98 -pedantic
195 # Use the generated library directory to for libraries
196 override LDFLAGS += -L$L -Wall
198 # Make sure the generated libraries can be found
199 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
206 override CPPFLAGS += -ggdb -DDEBUG
210 override CPPFLAGS += -O2 -DNDEBUG
214 override CPPFLAGS += -ggdb -pg --coverage
215 override LDFLAGS += -pg --coverage
219 # Automatic dependency handling
220 ################################
222 # These files are created during compilation.
223 sinclude $(shell test -d $O && find $O -name '*.d')
230 $O/%.o: $T/%.c $G/compile-c-flags
233 # Compile C++ objects
234 $O/%.o: $T/%.cpp $G/compile-cpp-flags
237 # Link binary programs
238 $B/%: $G/link-o-flags
241 # Link shared libraries
242 $L/%.so: override CFLAGS += -fPIC
243 $L/%.so: override CXXFLAGS += -fPIC
244 $L/%.so: $G/link-o-flags
247 # Install binary programs
249 $(call install_file,0755)
251 # Install system binary programs
253 $(call install_file,0755)
261 $(call exec,$(RM) -r $D,$D)
263 # Phony rule to uninstall all built targets (like "install", uses $(install)).
266 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
268 # These rules use the "Secondary Expansion" GNU Make feature, to allow
269 # sub-makes to add values to the special variables $(all), after this makefile
273 # Phony rule to make all the targets (sub-makefiles can append targets to build
274 # to the $(all) variable).
278 # Phony rule to install all built targets (sub-makefiles can append targets to
279 # build to the $(install) variable).
284 # Create build directory structure
285 ###################################
287 # Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory
288 # structure of the project into $O. Create one symlink "last" to the current
291 # NOTE: the second mkdir can yield no arguments if the project don't have any
292 # subdirectories, that's why the current directory "." is included, so it
293 # won't show an error message in case of no subdirectories.
294 setup_build_dir__ := $(shell \
295 mkdir -p $O $B $L $(INCLUDE_DIR); \
296 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
297 $(shell find $T -type d -not -path '$D*'))); \
298 test -L $D/last || ln -s $F $D/last )
301 # Automatic rebuilding when flags or commands changes
302 ######################################################
304 # Re-compile C files if one of this variables changes
305 COMPILE.c.FLAGS += $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH) ~ $(prefix)
307 # Re-compile C++ files if one of this variables changes
308 COMPILE.cpp.FLAGS += $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH) \
311 # Re-link binaries and libraries if one of this variables changes
312 LINK.o.FLAGS += $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
314 # Create files containing the current flags to trigger a rebuild if they change
315 setup_flag_files__ := $(call gen_rebuild_flags,$G/compile-c-flags, \
316 $(COMPILE.c.FLAGS),C compiler or flags; )
317 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
318 $G/compile-cpp-flags, $(COMPILE.cpp.FLAGS),C++ compiler or flags; )
319 setup_flag_files__ := $(setup_flag_files__)$(call gen_rebuild_flags, \
320 $G/link-o-flags, $(LINK.o.FLAGS),linker or link flags; )
322 # Print any generated message (if verbose)
323 $(if $V,$(if $(setup_flag_files__), \
324 $(info !! Something changed: $(setup_flag_files__)re-building \