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/$$*)
83 -e 's,^$${top_srcdir},$$$${top_srcdir},' \
84 -e 's, $${top_srcdir}, $$$${top_srcdir},' \
85 -e 's,^$${BUILD_DIR},$$$${BUILD_DIR},' \
86 -e 's, $${BUILD_DIR}/make/include/[^ :]*,,' \
87 -e 's, $${BUILD_DIR}, $$$${BUILD_DIR},' \
89 -e 's,^ *[^:]* *: *$$$$,,' \
91 -e 's/\.o: /.$$$${OBJ_EXT}: /' \
93 < $${BUILD_DIR}/objs/$$*.d | sed -e '$$$$!N; /^\(.*\)\n\1$$$$/!P; D' \
94 > $${BUILD_DIR}/make/src/$$*.mk
96 -e 's, $${BUILD_DIR}/make/include/[^ :]*,,' \
98 -e 's,^ *[^:]* *: *$$$$,,' \
103 < $${BUILD_DIR}/objs/$$*.d | sed -e '$$$$!N; /^\(.*\)\n\1$$$$/!P; D' \
104 >> $${BUILD_DIR}/make/src/$$*.mk
105 @rm -f $${BUILD_DIR}/objs/$$*.d
108 # ADD_OBJECT_RULE - Parameterized "function" that adds a pattern rule, using
109 # the commands from the second argument, for building object files from
110 # source files with the filename extension specified in the first argument.
112 # This function assumes that the C/C++ sources files have filenames
113 # *relative* to the source root. If they have absolute pathnames, it
114 # creates the wrong filenames...
118 ifeq "${CPP_MAKEDEPEND}" "yes"
119 define ADD_OBJECT_RULE
120 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1}
122 $${CPP} $${CPPFLAGS} $${SRC_INCDIRS} $${SRC_DEFS} $$< | sed \
123 -n 's,^\# *[0-9][0-9]* *"\([^"]*\)".*,$$@: \1,p' > $${BUILD_DIR}/objs/$$*.d
128 define ADD_OBJECT_RULE
129 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1}
135 # ADD_TARGET_DIR - Parameterized "function" that makes a link from
136 # TARGET_DIR to the executable or library in the BUILD_DIR directory.
140 ifneq "${TARGET_DIR}" ""
141 define ADD_TARGET_DIR
142 all: $${TARGET_DIR}/$$(notdir ${1})
144 $${TARGET_DIR}/$$(notdir ${1}): ${1}
145 [ -f $${TARGET_DIR}/$$(notdir ${1}) ] || ln -s ${1} $${TARGET_DIR}/$$(notdir ${1})
150 # ADD_TARGET_TO_ALL - Parameterized "function" that adds the target,
151 # and makes "all" depend on it.
155 define ADD_TARGET_TO_ALL
160 # ADD_TARGET_RULE.* - Parameterized "functions" that adds a new target to the
161 # Makefile. There should be one ADD_TARGET_RULE definition for each
162 # type of target that is used in the build.
164 # New rules can be added by copying one of the existing ones, and
165 # replacing the line after the "mkdir"
168 # ADD_TARGET_RULE.exe - Build an executable target.
172 define ADD_TARGET_RULE.exe
173 # So "make ${1}" works
175 ${1}: $${${1}_BUILD}/${1}
177 # Create executable ${1}
178 $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PRBIN} $${${1}_PRLIBS}
179 @$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
180 @$(ECHO) LINK $${${1}_BUILD}/${1}
181 @$${${1}_LINKER} -o $${${1}_BUILD}/${1} $${RPATH_FLAGS} $${LDFLAGS} \
182 $${${1}_LDFLAGS} $${${1}_OBJS} $${${1}_PRLIBS} \
183 $${LDLIBS} $${${1}_LDLIBS}
188 # ADD_TARGET_RULE.a - Build a static library target.
192 define ADD_TARGET_RULE.a
193 # So "make ${1}" works
195 ${1}: $${${1}_BUILD}/${1}
197 # Create static library ${1}
198 $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PREREQS}
199 @$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
200 @$(ECHO) LINK $${${1}_BUILD}/${1}
201 @$${AR} $${ARFLAGS} $${${1}_BUILD}/${1} $${${1}_OBJS}
206 # ADD_TARGET_RULE.so - Build a ".so" target.
210 define ADD_TARGET_RULE.so
211 $(error Please add rules to build a ".so" file.)
214 # ADD_TARGET_RULE.dll - Build a ".dll" target.
218 define ADD_TARGET_RULE.dll
219 $(error Please add rules to build a ".dll" file.)
222 # ADD_TARGET_RULE.dylib - Build a ".dylib" target.
226 define ADD_TARGET_RULE.dylib
227 $(error Please add rules to build a ".dylib" file.)
230 # CANONICAL_PATH - Given one or more paths, converts the paths to the canonical
231 # form. The canonical form is the path, relative to the project's top-level
232 # directory (the directory from which "make" is run), and without
233 # any "./" or "../" sequences. For paths that are not located below the
234 # top-level directory, the canonical form is the absolute path (i.e. from
235 # the root of the filesystem) also without "./" or "../" sequences.
236 define CANONICAL_PATH
237 $(patsubst ${CURDIR}/%,%,$(abspath ${1}))
240 # COMPILE_C_CMDS - Commands for compiling C source code.
241 define COMPILE_C_CMDS
244 @$(strip ${COMPILE.c} -o $@ -c -MD ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} \
245 ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS} $<)
248 # COMPILE_CXX_CMDS - Commands for compiling C++ source code.
249 define COMPILE_CXX_CMDS
251 @$(strip ${COMPILE.cxx} -o $@ -c -MD ${CXXFLAGS} ${SRC_CXXFLAGS} ${INCDIRS} \
252 ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS} $<)
255 # INCLUDE_SUBMAKEFILE - Parameterized "function" that includes a new
256 # "submakefile" fragment into the overall Makefile. It also recursively
257 # includes all submakefiles of the specified submakefile fragment.
261 define INCLUDE_SUBMAKEFILE
262 # Initialize all variables that can be defined by a makefile fragment, then
263 # include the specified makefile fragment.
285 # A directory stack is maintained so that the correct paths are used as we
286 # recursively include all submakefiles. Get the makefile's directory and
287 # push it onto the stack.
288 DIR := $(call CANONICAL_PATH,$(dir ${1}))
289 DIR_STACK := $$(call PUSH,$${DIR_STACK},$${DIR})
293 # Initialize internal local variables.
296 # Determine which target this makefile's variables apply to. A stack is
297 # used to keep track of which target is the "current" target as we
298 # recursively include other submakefiles.
299 ifneq "$$(strip $${TARGET})" ""
300 # This makefile defined a new target. Target variables defined by this
301 # makefile apply to this new target. Initialize the target's variables.
303 # libs go into ${BUILD_DIR}/lib
304 # everything else goes into ${BUILD_DIR}/bin
305 # TGT := $$(strip $$(if $$(suffix $${TARGET}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)/$${TARGET})
308 # A "hook" to rewrite "libfoo.a" -> "libfoo.la" when using libtool
309 $$(eval $$(call ADD_LIBTOOL_SUFFIX))
312 $${TGT}_LDFLAGS := $${TGT_LDFLAGS}
313 $${TGT}_LDLIBS := $${TGT_LDLIBS}
314 $${TGT}_LINKER := $${TGT_LINKER}
315 $${TGT}_POSTMAKE := $${TGT_POSTMAKE}
316 $${TGT}_POSTCLEAN := $${TGT_POSTCLEAN}
317 $${TGT}_POSTINSTALL := $${TGT_POSTINSTALL}
318 $${TGT}_PREREQS := $${TGT_PREREQS}
319 $${TGT}_PRBIN := $$(addprefix $${BUILD_DIR}/bin/,$$(filter-out %.a %.so %.la,$${TGT_PREREQS}))
320 $${TGT}_PRLIBS := $$(addprefix $${BUILD_DIR}/lib/,$$(filter %.a %.so %.la,$${TGT_PREREQS}))
324 $${TGT}_MAN := $${MAN}
325 $${TGT}_SUFFIX := $$(if $$(suffix $${TGT}),$$(suffix $${TGT}),.exe)
326 $${TGT}_BUILD := $$(if $$(suffix $${TGT}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)
327 $${TGT}_MAKEFILES += ${1}
328 $${TGT}_CHECK_HEADERS := $${TGT_CHECK_HEADERS}
329 $${TGT}_CHECK_LIBS := $${TGT_CHECK_LIBS}
331 # The values defined by this makefile apply to the the "current" target
332 # as determined by which target is at the top of the stack.
333 TGT := $$(strip $$(call PEEK,$${TGT_STACK}))
334 $${TGT}_LDFLAGS += $${TGT_LDFLAGS}
335 $${TGT}_LDLIBS += $${TGT_LDLIBS}
336 $${TGT}_POSTCLEAN += $${TGT_POSTCLEAN}
337 $${TGT}_POSTMAKE += $${TGT_POSTMAKE}
338 $${TGT}_PREREQS += $${TGT_PREREQS}
341 # Push the current target onto the target stack.
342 TGT_STACK := $$(call PUSH,$${TGT_STACK},$${TGT})
344 ifneq "$$(strip $${SOURCES})" ""
345 # This makefile builds one or more objects from source. Validate the
346 # specified sources against the supported source file types.
347 BAD_SRCS := $$(strip $$(filter-out $${ALL_SRC_EXTS},$${SOURCES}))
348 ifneq "$${BAD_SRCS}" ""
349 $$(error Unsupported source file(s) found in ${1} [$${BAD_SRCS}])
352 # Qualify and canonicalize paths.
353 SOURCES := $$(call QUALIFY_PATH,$${DIR},$${SOURCES})
354 SOURCES := $$(call CANONICAL_PATH,$${SOURCES})
355 SRC_INCDIRS := $$(call QUALIFY_PATH,$${DIR},$${SRC_INCDIRS})
356 SRC_INCDIRS := $$(call CANONICAL_PATH,$${SRC_INCDIRS})
358 # Save the list of source files for this target.
359 $${TGT}_SOURCES += $${SOURCES}
361 # Convert the source file names to their corresponding object file
363 OBJS := $$(addprefix $${BUILD_DIR}/objs/,\
364 $$(addsuffix .${OBJ_EXT},$$(basename $${SOURCES})))
366 # Add the objects to the current target's list of objects, and create
367 # target-specific variables for the objects based on any source
368 # variables that were defined.
369 $${TGT}_OBJS += $${OBJS}
370 $${TGT}_DEPS += $$(addprefix $${BUILD_DIR}/make/src/,\
371 $$(addsuffix .mk,$$(basename $${SOURCES})))
373 # A "hook" to define variables needed by the "legacy" makefiles.
374 $$(eval $$(call ADD_LEGACY_VARIABLES,$$(dir ${1}),$${TGT}))
376 $${OBJS}: SRC_CFLAGS := $${SRC_CFLAGS}
377 $${OBJS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
378 $${OBJS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
379 $${OBJS}: SRC_INCDIRS := $$(addprefix -I,$${SRC_INCDIRS})
382 ifneq "$$(strip $${SUBMAKEFILES})" ""
383 # This makefile has submakefiles. Recursively include them.
384 $$(foreach MK,$${SUBMAKEFILES},\
385 $$(eval $$(call INCLUDE_SUBMAKEFILE,\
386 $$(call CANONICAL_PATH,\
387 $$(call QUALIFY_PATH,$${DIR},$${MK})))))
390 # Reset the "current" target to it's previous value.
391 TGT_STACK := $$(call POP,$${TGT_STACK})
392 # If we're about to change targets, create the rules for the target
393 ifneq "$${TGT}" "$$(call PEEK,$${TGT_STACK})"
394 # add rules to build the target, and have "all" depend on it.
395 $$(eval $$(call ADD_TARGET_TO_ALL,$${TGT}))
397 # A "hook" to add rules for ${TARGET_DIR}/foo, if TARGET_DIR
398 # is defined. Otherwise, we leave the source directory untouched.
399 $$(eval $$(call ADD_TARGET_DIR,$${TGT}))
401 # A "hook" to build the libtool target.
402 $$(eval $$(call ADD_LIBTOOL_TARGET))
404 # Choose the correct linker.
405 ifeq "$$(strip $$(filter $${CXX_SRC_EXTS},$${$${TGT}_SOURCES}))" ""
406 ifeq "$${$${TGT}_LINKER}" ""
407 $${TGT}_LINKER := ${LL}$${LINK.c}
410 ifeq "$${$${TGT}_LINKER}" ""
411 $${TGT}_LINKER := ${LL}$${LINK.cxx}
415 # add rules to build the target
416 $$(eval $$(call ADD_TARGET_RULE$${$${TGT}_SUFFIX},$${TGT}))
418 # generate the clean rule for this target.
419 $$(eval $$(call ADD_CLEAN_RULE,$${TGT}))
421 # Hook to add an installation target
422 $$(eval $$(call ADD_INSTALL_TARGET,$${TGT}))
424 # Hook to add a configuration target
425 $$(eval $$(call ADD_TARGET_CONFIG,$${TGT}))
427 # "hook" for legacy Makefiles
428 $$(eval $$(call ADD_LEGACY_RULE,$${TGT}))
431 TGT := $$(call PEEK,$${TGT_STACK})
433 # Reset the "current" directory to it's previous value.
434 DIR_STACK := $$(call POP,$${DIR_STACK})
435 DIR := $$(call PEEK,$${DIR_STACK})
438 # MIN - Parameterized "function" that results in the minimum lexical value of
439 # the two values given.
441 $(firstword $(sort ${1} ${2}))
444 # PEEK - Parameterized "function" that results in the value at the top of the
445 # specified colon-delimited stack.
447 $(lastword $(subst :, ,${1}))
450 # POP - Parameterized "function" that pops the top value off of the specified
451 # colon-delimited stack, and results in the new value of the stack. Note that
452 # the popped value cannot be obtained using this function; use peek for that.
454 ${1:%:$(lastword $(subst :, ,${1}))=%}
457 # PUSH - Parameterized "function" that pushes a value onto the specified colon-
458 # delimited stack, and results in the new value of the stack.
463 # QUALIFY_PATH - Given a "root" directory and one or more paths, qualifies the
464 # paths using the "root" directory (i.e. appends the root directory name to
465 # the paths) except for paths that are absolute.
467 $(addprefix ${1}/,$(filter-out /%,${2})) $(filter /%,${2})
470 ###############################################################################
472 # Start of Makefile Evaluation
474 ###############################################################################
476 # Older versions of GNU Make lack capabilities needed by boilermake.
477 # With older versions, "make" may simply output "nothing to do", likely leading
478 # to confusion. To avoid this, check the version of GNU make up-front and
479 # inform the user if their version of make doesn't meet the minimum required.
480 MIN_MAKE_VERSION := 3.81
481 MIN_MAKE_VER_MSG := boilermake requires GNU Make ${MIN_MAKE_VERSION} or greater
482 ifeq "${MAKE_VERSION}" ""
483 $(info GNU Make not detected)
484 $(error ${MIN_MAKE_VER_MSG})
486 ifneq "${MIN_MAKE_VERSION}" "$(call MIN,${MIN_MAKE_VERSION},${MAKE_VERSION})"
487 $(info This is GNU Make version ${MAKE_VERSION})
488 $(error ${MIN_MAKE_VER_MSG})
491 # Define the source file extensions that we know how to handle.
494 CXX_SRC_EXTS := %.C %.cc %.cp %.cpp %.CPP %.cxx %.c++
495 ALL_SRC_EXTS := ${C_SRC_EXTS} ${CXX_SRC_EXTS}
497 # Initialize global variables.
504 ifeq "${top_builddir}" ""
508 # Ensure that valid values are set for BUILD_DIR
509 ifeq "$(strip ${BUILD_DIR})" ""
510 ifeq "${top_builddir}" "${PWD}"
513 BUILD_DIR := ${top_builddir}/build
516 BUILD_DIR := $(call CANONICAL_PATH,${BUILD_DIR})
519 # Define compilers and linkers
527 # Set ECHO to "true" for *very* quiet builds
530 # Define the "all" target (which simply builds all user-defined targets) as the
535 # Add "clean" rules to remove all build-generated files.
539 top_makedir := $(dir $(lastword ${MAKEFILE_LIST}))
541 -include ${top_makedir}/install.mk
542 -include ${top_makedir}/libtool.mk
544 # Include the main user-supplied submakefile. This also recursively includes
545 # all other user-supplied submakefiles.
546 $(eval $(call INCLUDE_SUBMAKEFILE,${top_builddir}/main.mk))
548 # Perform post-processing on global variables as needed.
549 DEFS := $(addprefix -D,${DEFS})
550 INCDIRS := $(addprefix -I,$(call CANONICAL_PATH,${INCDIRS}))
552 # Add pattern rule(s) for creating compiled object code from C source.
553 $(foreach EXT,${C_SRC_EXTS},\
554 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_C_CMDS})))
556 # Add pattern rule(s) for creating compiled object code from C++ source.
557 $(foreach EXT,${CXX_SRC_EXTS},\
558 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_CXX_CMDS})))
560 # Don't include the target dependencies if we're doing a "make clean"
561 # Future: have a list of targets that don't require dependency generation,
562 # and see if MAKECMDGOALS is one of them.
563 ifneq "$(MAKECMDGOALS)" "clean"
564 $(foreach TGT,${ALL_TGTS},\
565 $(eval -include ${${TGT}_DEPS}))