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 )
173 # Warn about everything
174 override CPPFLAGS += -Wall
176 # Use the includes directories to search for includes
177 override CPPFLAGS += -I$(INCLUDE_DIR)
179 # Let the program know where it will be installed
180 override CPPFLAGS += -DPREFIX=$(prefix)
182 # Be standard compilant
183 override CFLAGS += -std=c99 -pedantic
184 override CXXFLAGS += -std=c++98 -pedantic
186 # Use the generated library directory to for libraries
187 override LDFLAGS += -L$L -Wall
189 # Make sure the generated libraries can be found
190 export LD_LIBRARY_PATH := $L:$(LD_LIBRARY_PATH)
197 override CPPFLAGS += -ggdb -DDEBUG
201 override CPPFLAGS += -O2 -DNDEBUG
205 override CPPFLAGS += -ggdb -pg --coverage
206 override LDFLAGS += -pg --coverage
210 # Automatic dependency handling
211 ################################
213 # These files are created during compilation.
214 sinclude $(shell test -d $O && find $O -name '*.d')
220 $O/%.o: $T/%.c $G/compile-c-flags
223 $O/%.o: $T/%.cpp $G/compile-cpp-flags
226 $B/%: $G/link-o-flags
229 $L/%.so: override CFLAGS += -fPIC
230 $L/%.so: override CXXFLAGS += -fPIC
231 $L/%.so: $G/link-o-flags
235 $(call install_file,0755)
238 $(call install_file,0755)
245 $(call exec,$(RM) -r $D,$D)
247 # Phony rule to uninstall all built targets (like "install", uses $(install)).
250 $V$(foreach i,$(install),$(call vexec,$(RM) $i,$i);)
252 # These rules use the "Secondary Expansion" GNU Make feature, to allow
253 # sub-makes to add values to the special variables $(all), after this makefile
257 # Phony rule to make all the targets (sub-makefiles can append targets to build
258 # to the $(all) variable).
262 # Phony rule to install all built targets (sub-makefiles can append targets to
263 # build to the $(install) variable).
268 # Create build directory structure
269 ###################################
271 # Create $O, $B, $L and $(INCLUDE_DIR) directories and replicate the directory
272 # structure of the project into $O. Create one symlink "last" to the current
275 # NOTE: the second mkdir can yield no arguments if the project don't have any
276 # subdirectories, that's why the current directory "." is included, so it
277 # won't show an error message in case of no subdirectories.
278 setup_build_dir__ := $(shell \
279 mkdir -p $O $B $L $(INCLUDE_DIR); \
280 mkdir -p . $(addprefix $O,$(patsubst $T%,%,\
281 $(shell find $T -type d -not -path '$D*'))); \
282 test -L $D/last || ln -s $F $D/last )
285 # Automatic rebuilding when flags or commands changes
286 ######################################################
288 # Re-compile C files if one of this variables changes
289 COMPILE.c.FLAGS += $(CC) ~ $(CPPFLAGS) ~ $(CFLAGS) ~ $(TARGET_ARCH) ~ $(prefix)
291 # Re-compile C++ files if one of this variables changes
292 COMPILE.cpp.FLAGS += $(CXX) ~ $(CPPFLAGS) ~ $(CXXFLAGS) ~ $(TARGET_ARCH) \
295 # Re-link binaries and libraries if one of this variables changes
296 LINK.o.FLAGS += $(LD) ~ $(LDFLAGS) ~ $(TARGET_ARCH)
298 # Create a file with flags used to trigger rebuilding when they change. The
299 # first argument is the name of the file where to store the flags, the second
300 # are the flags and the third argument is a text to be displayed if the flags
301 # have changed. This should be used as a rule action or something where
302 # a shell script is expected.
303 gen_rebuild_flags = if test x"$2" != x"`cat $1 2>/dev/null`"; then \
304 test -f $1 && echo "$3"; \
307 # Create files containing the current flags to trigger a rebuild if they change
308 setup_flag_files__ := $(shell \
309 $(call gen_rebuild_flags,$G/compile-c-flags, \
310 $(COMPILE.c.FLAGS),C compiler or flags;); \
311 $(call gen_rebuild_flags,$G/compile-cpp-flags, \
312 $(COMPILE.cpp.FLAGS),C++ compiler or flags;); \
313 $(call gen_rebuild_flags,$G/link-o-flags, \
314 $(LINK.o.FLAGS),linker or link flags;) )
316 # Print any generated message (if verbose)
317 $(if $V,$(if $(setup_flag_files__), \
318 $(info !! Something changed: $(setup_flag_files__) \
319 re-building affected files...)))