1 # boilermake: A reusable, but flexible, boilerplate Makefile.
3 # Copyright 2008, 2009, 2010 Dan Moulding, Alan T. DeKok
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Caution: Don't edit this Makefile! Create your own main.mk and other
19 # submakefiles, which will be included by this Makefile.
20 # Only edit this if you need to modify boilermake's behavior (fix
21 # bugs, add features, etc).
23 # Note: Parameterized "functions" in this makefile that are marked with
24 # "USE WITH EVAL" are only useful in conjuction with eval. This is
25 # because those functions result in a block of Makefile syntax that must
26 # be evaluated after expansion. Since they must be used with eval, most
27 # instances of "$" within them need to be escaped with a second "$" to
28 # accomodate the double expansion that occurs when eval is invoked.
30 # ADD_CLEAN_RULE - Parameterized "function" that adds a new rule and phony
31 # target for cleaning the specified target (removing its build-generated
37 clean: clean_$(notdir ${1})
38 .PHONY: clean_$(notdir ${1})
40 @$(strip rm -f ${${1}_BUILD}/${1} ${${1}_NOLIBTOOL} ${${1}_BUILD}/${${1}_RELINK} $${${1}_OBJS} $${${1}_DEPS} $${${1}_OBJS:%.${OBJ_EXT}=%.[do]}) $(if ${TARGET_DIR},$${TARGET_DIR}/$(notdir ${1}))
45 # FILTER_DEPENDS: function to turn a *.d file into a *.mk file.
46 # We start off with the dependencies as created by the compiler,
47 # CPP, or makedepend. We then ensure that there is an empty dependency
48 # for each header file. The blank target ensures that the build
49 # can proceed even if the header file has been deleted.
53 # remove dependencies on global include files
54 # remove empty dependencies
55 # remove CPP hacks like "foo: <built-in>"
57 # 1) Filter the .d file to remove unnecessary cruft
60 # Replace ".o" with "${OBJ_EXT}"
61 # delete empty continuation lines
63 # replace "build/" with "${BUILD_DIR}/" when it's at the start of a line
64 # delete references to ${BUILD_DIR}/make/include, the "config.mk"
65 # file adds these dependencies automatically.
66 # replace "build/" with "${BUILD_DIR}/" when it's in the middle of a line
68 # remove sequential duplicate lines
70 # 2) Create empty dependencies from the files
73 # remove existing targets
74 # remove continuations (to make the targets stand by themselves)
76 # add in empty dependency for each file.
77 # remove sequential duplicate lines
80 @mkdir -p $$(dir $${BUILD_DIR}/make/src/$$*)
81 @mkdir -p $$(dir $${BUILD_DIR}/objs/$$*)
84 -e 's,^ *[^:]* *: *$$$$,,' \
86 -e 's/\.o: /.$$$${OBJ_EXT}: /' \
88 -e 's,^$${BUILD_DIR},$$$${BUILD_DIR},' \
89 -e 's, $${BUILD_DIR}/make/include/[^ :]*,,' \
90 -e 's, $${BUILD_DIR}, $$$${BUILD_DIR},' \
91 < $${BUILD_DIR}/objs/$$*.d | sed -e '$$$$!N; /^\(.*\)\n\1$$$$/!P; D' \
92 > $${BUILD_DIR}/make/src/$$*.mk
95 -e 's,^ *[^:]* *: *$$$$,,' \
97 -e 's, $${BUILD_DIR}/make/include/[^ :]*,,' \
101 < $${BUILD_DIR}/objs/$$*.d | sed -e '$$$$!N; /^\(.*\)\n\1$$$$/!P; D' \
102 >> $${BUILD_DIR}/make/src/$$*.mk
103 @rm -f $${BUILD_DIR}/objs/$$*.d
106 # ADD_OBJECT_RULE - Parameterized "function" that adds a pattern rule, using
107 # the commands from the second argument, for building object files from
108 # source files with the filename extension specified in the first argument.
110 # This function assumes that the C/C++ sources files have filenames
111 # *relative* to the source root. If they have absolute pathnames, it
112 # creates the wrong filenames...
116 ifeq "${CPP_MAKEDEPEND}" "yes"
117 define ADD_OBJECT_RULE
118 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1}
120 $${CPP} $${CPPFLAGS} $${SRC_INCDIRS} $${SRC_DEFS} $$< | sed \
121 -n 's,^\# *[0-9][0-9]* *"\([^"]*\)".*,$$@: \1,p' > $${BUILD_DIR}/objs/$$*.d
126 define ADD_OBJECT_RULE
127 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1}
133 # ADD_TARGET_DIR - Parameterized "function" that makes a link from
134 # TARGET_DIR to the executable or library in the BUILD_DIR directory.
138 ifneq "${TARGET_DIR}" ""
139 define ADD_TARGET_DIR
140 all: $${TARGET_DIR}/$$(notdir ${1})
142 $${TARGET_DIR}/$$(notdir ${1}): ${1}
143 [ -f $${TARGET_DIR}/$$(notdir ${1}) ] || ln -s ${1} $${TARGET_DIR}/$$(notdir ${1})
148 # ADD_TARGET_TO_ALL - Parameterized "function" that adds the target,
149 # and makes "all" depend on it.
153 define ADD_TARGET_TO_ALL
158 # ADD_TARGET_RULE.* - Parameterized "functions" that adds a new target to the
159 # Makefile. There should be one ADD_TARGET_RULE definition for each
160 # type of target that is used in the build.
162 # New rules can be added by copying one of the existing ones, and
163 # replacing the line after the "mkdir"
166 # ADD_TARGET_RULE.exe - Build an executable target.
170 define ADD_TARGET_RULE.exe
171 # So "make ${1}" works
173 ${1}: $${${1}_BUILD}/${1}
175 # Create executable ${1}
176 $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PRBIN} $${${1}_PRLIBS}
177 @$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
178 @echo LINK $${${1}_BUILD}/${1}
179 @$${${1}_LINKER} -o $${${1}_BUILD}/${1} $${RPATH_FLAGS} $${LDFLAGS} \
180 $${${1}_LDFLAGS} $${${1}_OBJS} $${${1}_PRLIBS} \
181 $${LDLIBS} $${${1}_LDLIBS}
186 # ADD_TARGET_RULE.a - Build a static library target.
190 define ADD_TARGET_RULE.a
191 # So "make ${1}" works
193 ${1}: $${${1}_BUILD}/${1}
195 # Create static library ${1}
196 $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PREREQS}
197 @$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
198 @echo LINK $${${1}_BUILD}/${1}
199 @$${AR} $${ARFLAGS} $${${1}_BUILD}/${1} $${${1}_OBJS}
204 # ADD_TARGET_RULE.so - Build a ".so" target.
208 define ADD_TARGET_RULE.so
209 $(error Please add rules to build a ".so" file.)
212 # ADD_TARGET_RULE.dll - Build a ".dll" target.
216 define ADD_TARGET_RULE.dll
217 $(error Please add rules to build a ".dll" file.)
220 # ADD_TARGET_RULE.dylib - Build a ".dylib" target.
224 define ADD_TARGET_RULE.dylib
225 $(error Please add rules to build a ".dylib" file.)
228 # CANONICAL_PATH - Given one or more paths, converts the paths to the canonical
229 # form. The canonical form is the path, relative to the project's top-level
230 # directory (the directory from which "make" is run), and without
231 # any "./" or "../" sequences. For paths that are not located below the
232 # top-level directory, the canonical form is the absolute path (i.e. from
233 # the root of the filesystem) also without "./" or "../" sequences.
234 define CANONICAL_PATH
235 $(patsubst ${CURDIR}/%,%,$(abspath ${1}))
238 # COMPILE_C_CMDS - Commands for compiling C source code.
239 define COMPILE_C_CMDS
242 @$(strip ${COMPILE.c} -o $@ -c -MD ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} \
243 ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS} $<)
246 # COMPILE_CXX_CMDS - Commands for compiling C++ source code.
247 define COMPILE_CXX_CMDS
249 @$(strip ${COMPILE.cxx} -o $@ -c -MD ${CXXFLAGS} ${SRC_CXXFLAGS} ${INCDIRS} \
250 ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS} $<)
253 # INCLUDE_SUBMAKEFILE - Parameterized "function" that includes a new
254 # "submakefile" fragment into the overall Makefile. It also recursively
255 # includes all submakefiles of the specified submakefile fragment.
259 define INCLUDE_SUBMAKEFILE
260 # Initialize all variables that can be defined by a makefile fragment, then
261 # include the specified makefile fragment.
283 # A directory stack is maintained so that the correct paths are used as we
284 # recursively include all submakefiles. Get the makefile's directory and
285 # push it onto the stack.
286 DIR := $(call CANONICAL_PATH,$(dir ${1}))
287 DIR_STACK := $$(call PUSH,$${DIR_STACK},$${DIR})
291 # Initialize internal local variables.
294 # Determine which target this makefile's variables apply to. A stack is
295 # used to keep track of which target is the "current" target as we
296 # recursively include other submakefiles.
297 ifneq "$$(strip $${TARGET})" ""
298 # This makefile defined a new target. Target variables defined by this
299 # makefile apply to this new target. Initialize the target's variables.
301 # libs go into ${BUILD_DIR}/lib
302 # everything else goes into ${BUILD_DIR}/bin
303 # TGT := $$(strip $$(if $$(suffix $${TARGET}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)/$${TARGET})
306 # A "hook" to rewrite "libfoo.a" -> "libfoo.la" when using libtool
307 $$(eval $$(call ADD_LIBTOOL_SUFFIX))
310 $${TGT}_LDFLAGS := $${TGT_LDFLAGS}
311 $${TGT}_LDLIBS := $${TGT_LDLIBS}
312 $${TGT}_LINKER := $${TGT_LINKER}
313 $${TGT}_POSTMAKE := $${TGT_POSTMAKE}
314 $${TGT}_POSTCLEAN := $${TGT_POSTCLEAN}
315 $${TGT}_POSTINSTALL := $${TGT_POSTINSTALL}
316 $${TGT}_PREREQS := $${TGT_PREREQS}
317 $${TGT}_PRBIN := $$(addprefix $${BUILD_DIR}/bin/,$$(filter-out %.a %.so %.la,$${TGT_PREREQS}))
318 $${TGT}_PRLIBS := $$(addprefix $${BUILD_DIR}/lib/,$$(filter %.a %.so %.la,$${TGT_PREREQS}))
322 $${TGT}_MAN := $${MAN}
323 $${TGT}_SUFFIX := $$(if $$(suffix $${TGT}),$$(suffix $${TGT}),.exe)
324 $${TGT}_BUILD := $$(if $$(suffix $${TGT}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)
325 $${TGT}_MAKEFILES += ${1}
326 $${TGT}_CHECK_HEADERS := $${TGT_CHECK_HEADERS}
327 $${TGT}_CHECK_LIBS := $${TGT_CHECK_LIBS}
329 # The values defined by this makefile apply to the the "current" target
330 # as determined by which target is at the top of the stack.
331 TGT := $$(strip $$(call PEEK,$${TGT_STACK}))
332 $${TGT}_LDFLAGS += $${TGT_LDFLAGS}
333 $${TGT}_LDLIBS += $${TGT_LDLIBS}
334 $${TGT}_POSTCLEAN += $${TGT_POSTCLEAN}
335 $${TGT}_POSTMAKE += $${TGT_POSTMAKE}
336 $${TGT}_PREREQS += $${TGT_PREREQS}
339 # Push the current target onto the target stack.
340 TGT_STACK := $$(call PUSH,$${TGT_STACK},$${TGT})
342 ifneq "$$(strip $${SOURCES})" ""
343 # This makefile builds one or more objects from source. Validate the
344 # specified sources against the supported source file types.
345 BAD_SRCS := $$(strip $$(filter-out $${ALL_SRC_EXTS},$${SOURCES}))
346 ifneq "$${BAD_SRCS}" ""
347 $$(error Unsupported source file(s) found in ${1} [$${BAD_SRCS}])
350 # Qualify and canonicalize paths.
351 SOURCES := $$(call QUALIFY_PATH,$${DIR},$${SOURCES})
352 SOURCES := $$(call CANONICAL_PATH,$${SOURCES})
353 SRC_INCDIRS := $$(call QUALIFY_PATH,$${DIR},$${SRC_INCDIRS})
354 SRC_INCDIRS := $$(call CANONICAL_PATH,$${SRC_INCDIRS})
356 # Save the list of source files for this target.
357 $${TGT}_SOURCES += $${SOURCES}
359 # Convert the source file names to their corresponding object file
361 OBJS := $$(addprefix $${BUILD_DIR}/objs/,\
362 $$(addsuffix .${OBJ_EXT},$$(basename $${SOURCES})))
364 # Add the objects to the current target's list of objects, and create
365 # target-specific variables for the objects based on any source
366 # variables that were defined.
367 $${TGT}_OBJS += $${OBJS}
368 $${TGT}_DEPS += $$(addprefix $${BUILD_DIR}/make/src/,\
369 $$(addsuffix .mk,$$(basename $${SOURCES})))
371 # A "hook" to define variables needed by the "legacy" makefiles.
372 $$(eval $$(call ADD_LEGACY_VARIABLES,$$(dir ${1}),$${TGT}))
374 $${OBJS}: SRC_CFLAGS := $${SRC_CFLAGS}
375 $${OBJS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
376 $${OBJS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
377 $${OBJS}: SRC_INCDIRS := $$(addprefix -I,$${SRC_INCDIRS})
380 ifneq "$$(strip $${SUBMAKEFILES})" ""
381 # This makefile has submakefiles. Recursively include them.
382 $$(foreach MK,$${SUBMAKEFILES},\
383 $$(eval $$(call INCLUDE_SUBMAKEFILE,\
384 $$(call CANONICAL_PATH,\
385 $$(call QUALIFY_PATH,$${DIR},$${MK})))))
388 # Reset the "current" target to it's previous value.
389 TGT_STACK := $$(call POP,$${TGT_STACK})
390 # If we're about to change targets, create the rules for the target
391 ifneq "$${TGT}" "$$(call PEEK,$${TGT_STACK})"
392 # add rules to build the target, and have "all" depend on it.
393 $$(eval $$(call ADD_TARGET_TO_ALL,$${TGT}))
395 # A "hook" to add rules for ${TARGET_DIR}/foo, if TARGET_DIR
396 # is defined. Otherwise, we leave the source directory untouched.
397 $$(eval $$(call ADD_TARGET_DIR,$${TGT}))
399 # A "hook" to build the libtool target.
400 $$(eval $$(call ADD_LIBTOOL_TARGET))
402 # Choose the correct linker.
403 ifeq "$$(strip $$(filter $${CXX_SRC_EXTS},$${$${TGT}_SOURCES}))" ""
404 ifeq "$${$${TGT}_LINKER}" ""
405 $${TGT}_LINKER := ${LL}$${LINK.c}
408 ifeq "$${$${TGT}_LINKER}" ""
409 $${TGT}_LINKER := ${LL}$${LINK.cxx}
413 # add rules to build the target
414 $$(eval $$(call ADD_TARGET_RULE$${$${TGT}_SUFFIX},$${TGT}))
416 # generate the clean rule for this target.
417 $$(eval $$(call ADD_CLEAN_RULE,$${TGT}))
419 # Hook to add an installation target
420 $$(eval $$(call ADD_INSTALL_TARGET,$${TGT}))
422 # Hook to add a configuration target
423 $$(eval $$(call ADD_TARGET_CONFIG,$${TGT}))
425 # "hook" for legacy Makefiles
426 $$(eval $$(call ADD_LEGACY_RULE,$${TGT}))
429 TGT := $$(call PEEK,$${TGT_STACK})
431 # Reset the "current" directory to it's previous value.
432 DIR_STACK := $$(call POP,$${DIR_STACK})
433 DIR := $$(call PEEK,$${DIR_STACK})
436 # MIN - Parameterized "function" that results in the minimum lexical value of
437 # the two values given.
439 $(firstword $(sort ${1} ${2}))
442 # PEEK - Parameterized "function" that results in the value at the top of the
443 # specified colon-delimited stack.
445 $(lastword $(subst :, ,${1}))
448 # POP - Parameterized "function" that pops the top value off of the specified
449 # colon-delimited stack, and results in the new value of the stack. Note that
450 # the popped value cannot be obtained using this function; use peek for that.
452 ${1:%:$(lastword $(subst :, ,${1}))=%}
455 # PUSH - Parameterized "function" that pushes a value onto the specified colon-
456 # delimited stack, and results in the new value of the stack.
461 # QUALIFY_PATH - Given a "root" directory and one or more paths, qualifies the
462 # paths using the "root" directory (i.e. appends the root directory name to
463 # the paths) except for paths that are absolute.
465 $(addprefix ${1}/,$(filter-out /%,${2})) $(filter /%,${2})
468 ###############################################################################
470 # Start of Makefile Evaluation
472 ###############################################################################
474 # Older versions of GNU Make lack capabilities needed by boilermake.
475 # With older versions, "make" may simply output "nothing to do", likely leading
476 # to confusion. To avoid this, check the version of GNU make up-front and
477 # inform the user if their version of make doesn't meet the minimum required.
478 MIN_MAKE_VERSION := 3.81
479 MIN_MAKE_VER_MSG := boilermake requires GNU Make ${MIN_MAKE_VERSION} or greater
480 ifeq "${MAKE_VERSION}" ""
481 $(info GNU Make not detected)
482 $(error ${MIN_MAKE_VER_MSG})
484 ifneq "${MIN_MAKE_VERSION}" "$(call MIN,${MIN_MAKE_VERSION},${MAKE_VERSION})"
485 $(info This is GNU Make version ${MAKE_VERSION})
486 $(error ${MIN_MAKE_VER_MSG})
489 # Define the source file extensions that we know how to handle.
492 CXX_SRC_EXTS := %.C %.cc %.cp %.cpp %.CPP %.cxx %.c++
493 ALL_SRC_EXTS := ${C_SRC_EXTS} ${CXX_SRC_EXTS}
495 # Initialize global variables.
502 ifeq "${top_builddir}" ""
506 # Ensure that valid values are set for BUILD_DIR
507 ifeq "$(strip ${BUILD_DIR})" ""
508 ifeq "${top_builddir}" "${PWD}"
511 BUILD_DIR := ${top_builddir}/build
514 BUILD_DIR := $(call CANONICAL_PATH,${BUILD_DIR})
517 # Define compilers and linkers
525 # Define the "all" target (which simply builds all user-defined targets) as the
530 # Add "clean" rules to remove all build-generated files.
534 top_makedir := $(dir $(lastword ${MAKEFILE_LIST}))
536 -include ${top_makedir}/install.mk
537 -include ${top_makedir}/libtool.mk
539 # Include the main user-supplied submakefile. This also recursively includes
540 # all other user-supplied submakefiles.
541 $(eval $(call INCLUDE_SUBMAKEFILE,${top_builddir}/main.mk))
543 # Perform post-processing on global variables as needed.
544 DEFS := $(addprefix -D,${DEFS})
545 INCDIRS := $(addprefix -I,$(call CANONICAL_PATH,${INCDIRS}))
547 # Add pattern rule(s) for creating compiled object code from C source.
548 $(foreach EXT,${C_SRC_EXTS},\
549 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_C_CMDS})))
551 # Add pattern rule(s) for creating compiled object code from C++ source.
552 $(foreach EXT,${CXX_SRC_EXTS},\
553 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_CXX_CMDS})))
555 # Don't include the target dependencies if we're doing a "make clean"
556 # Future: have a list of targets that don't require dependency generation,
557 # and see if MAKECMDGOALS is one of them.
558 ifneq "$(MAKECMDGOALS)" "clean"
559 $(foreach TGT,${ALL_TGTS},\
560 $(eval -include ${${TGT}_DEPS}))