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.
31 # You can watch what it's doing by:
33 # $ VERBOSE=1 make ... args ...
41 # ADD_CLEAN_RULE - Parameterized "function" that adds a new rule and phony
42 # target for cleaning the specified target (removing its build-generated
48 clean: clean_$(notdir ${1})
49 .PHONY: clean_$(notdir ${1})
51 $(Q)$(strip rm -f ${${1}_BUILD}/${1} $${${1}_OBJS} $${${1}_DEPS} $${${1}_OBJS:%.${OBJ_EXT}=%.[do]}) $(if ${TARGET_DIR},$${TARGET_DIR}/$(notdir ${1}))
56 # FILTER_DEPENDS: function to turn a *.d file into a *.mk file.
57 # We start off with the dependencies as created by the compiler,
58 # CPP, or makedepend. We then ensure that there is an empty dependency
59 # for each header file. The blank target ensures that the build
60 # can proceed even if the header file has been deleted.
64 # remove dependencies on global include files
65 # remove empty dependencies
66 # remove CPP hacks like "foo: <built-in>"
68 # 1) Filter the .d file to remove unnecessary cruft
71 # Replace ".o" with "${OBJ_EXT}"
72 # delete empty continuation lines
74 # replace "build/" with "${BUILD_DIR}/" when it's at the start of a line
75 # delete references to ${BUILD_DIR}/make/include, the "config.mk"
76 # file adds these dependencies automatically.
77 # replace "build/" with "${BUILD_DIR}/" when it's in the middle of a line
79 # remove sequential duplicate lines
81 # 2) Create empty dependencies from the files
84 # remove existing targets
85 # remove continuations (to make the targets stand by themselves)
87 # add in empty dependency for each file.
88 # remove sequential duplicate lines
91 $(Q)mkdir -p $$(dir $${BUILD_DIR}/make/src/$$*)
92 $(Q)mkdir -p $$(dir $${BUILD_DIR}/objs/$$*)
93 $(Q)sed -e 's/#.*//' \
94 -e 's,^$${top_srcdir},$$$${top_srcdir},' \
95 -e 's, $${top_srcdir}, $$$${top_srcdir},' \
96 -e 's,^$${BUILD_DIR},$$$${BUILD_DIR},' \
97 -e 's, $${BUILD_DIR}/make/include/[^ :]*,,' \
98 -e 's, $${BUILD_DIR}, $$$${BUILD_DIR},' \
100 -e 's,^ *[^:]* *: *$$$$,,' \
102 -e 's/\.o: /.$$$${OBJ_EXT}: /' \
104 < $${BUILD_DIR}/objs/$$*.d | sed -e '$$$$!N; /^\(.*\)\n\1$$$$/!P; D' \
105 > $${BUILD_DIR}/make/src/$$*.mk
106 $(Q)sed -e 's/#.*//' \
107 -e 's, $${BUILD_DIR}/make/include/[^ :]*,,' \
109 -e 's,^ *[^:]* *: *$$$$,,' \
114 < $${BUILD_DIR}/objs/$$*.d | sed -e '$$$$!N; /^\(.*\)\n\1$$$$/!P; D' \
115 >> $${BUILD_DIR}/make/src/$$*.mk
116 @rm -f $${BUILD_DIR}/objs/$$*.d
119 # ADD_OBJECT_RULE - Parameterized "function" that adds a pattern rule, using
120 # the commands from the second argument, for building object files from
121 # source files with the filename extension specified in the first argument.
123 # This function assumes that the C/C++ sources files have filenames
124 # *relative* to the source root. If they have absolute pathnames, it
125 # creates the wrong filenames...
129 ifeq "${CPP_MAKEDEPEND}" "yes"
130 define ADD_OBJECT_RULE
131 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1} ${JLIBTOOL}
133 $${CPP} $${CPPFLAGS} $${SRC_INCDIRS} $${SRC_DEFS} $$< | sed \
134 -n 's,^\# *[0-9][0-9]* *"\([^"]*\)".*,$$@: \1,p' > $${BUILD_DIR}/objs/$$*.d
139 define ADD_OBJECT_RULE
140 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1} ${JLIBTOOL}
146 define ADD_ANALYZE_RULE
147 $${BUILD_DIR}/plist/%.plist: ${1} ${JLIBTOOL}
151 # ADD_TARGET_DIR - Parameterized "function" that makes a link from
152 # TARGET_DIR to the executable or library in the BUILD_DIR directory.
156 ifneq "${TARGET_DIR}" ""
157 define ADD_TARGET_DIR
158 all: $${TARGET_DIR}/$$(notdir ${1})
160 $${TARGET_DIR}/$$(notdir ${1}): ${1}
161 [ -f $${TARGET_DIR}/$$(notdir ${1}) ] || ln -s ${1} $${TARGET_DIR}/$$(notdir ${1})
166 # ADD_TARGET_TO_ALL - Parameterized "function" that adds the target,
167 # and makes "all" depend on it.
171 define ADD_TARGET_TO_ALL
176 # ADD_TARGET_RULE.* - Parameterized "functions" that adds a new target to the
177 # Makefile. There should be one ADD_TARGET_RULE definition for each
178 # type of target that is used in the build.
180 # New rules can be added by copying one of the existing ones, and
181 # replacing the line after the "mkdir"
184 # ADD_TARGET_RULE.exe - Build an executable target.
188 define ADD_TARGET_RULE.exe
189 # So "make ${1}" works
191 ${1}: $${${1}_BUILD}/${1}
193 # Create executable ${1}
194 $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PRBIN} $${${1}_PRLIBS}
195 $(Q)$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
196 $(Q)$(ECHO) LINK $${${1}_BUILD}/${1}
197 $(Q)$${${1}_LINKER} -o $${${1}_BUILD}/${1} $${RPATH_FLAGS} $${LDFLAGS} \
198 $${${1}_LDFLAGS} $${${1}_OBJS} $${${1}_PRLIBS} \
199 $${LDLIBS} $${${1}_LDLIBS}
200 $(Q)$${${1}_POSTMAKE}
202 ifneq "${ANALYZE.c}" ""
203 scan.${1}: $${${1}_PLISTS}
207 # ADD_TARGET_RULE.a - Build a static library target.
211 define ADD_TARGET_RULE.a
212 # So "make ${1}" works
214 ${1}: $${${1}_BUILD}/${1}
216 # Create static library ${1}
217 $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PRLIBS}
218 $(Q)$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
219 $(Q)$(ECHO) LINK $${${1}_BUILD}/${1}
220 $(Q)$${AR} $${ARFLAGS} $${${1}_BUILD}/${1} $${${1}_OBJS}
221 $(Q)$${${1}_POSTMAKE}
223 ifneq "${ANALYZE.c}" ""
224 scan.${1}: $${${1}_PLISTS}
228 # ADD_TARGET_RULE.so - Build a ".so" target.
232 define ADD_TARGET_RULE.so
233 $(error Please add rules to build a ".so" file.)
236 # ADD_TARGET_RULE.dll - Build a ".dll" target.
240 define ADD_TARGET_RULE.dll
241 $(error Please add rules to build a ".dll" file.)
244 # ADD_TARGET_RULE.dylib - Build a ".dylib" target.
248 define ADD_TARGET_RULE.dylib
249 $(error Please add rules to build a ".dylib" file.)
252 # CANONICAL_PATH - Given one or more paths, converts the paths to the canonical
253 # form. The canonical form is the path, relative to the project's top-level
254 # directory (the directory from which "make" is run), and without
255 # any "./" or "../" sequences. For paths that are not located below the
256 # top-level directory, the canonical form is the absolute path (i.e. from
257 # the root of the filesystem) also without "./" or "../" sequences.
258 define CANONICAL_PATH
259 $(patsubst ${CURDIR}/%,%,$(abspath ${1}))
262 # COMPILE_C_CMDS - Commands for compiling C source code.
263 define COMPILE_C_CMDS
264 $(Q)mkdir -p $(dir $@)
266 $(Q)$(strip ${COMPILE.c} -o $@ -c -MD ${CPPFLAGS} ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} \
267 ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS} $<)
270 # ANALYZE_C_CMDS - Commands for analyzing C source code with clang.
271 define ANALYZE_C_CMDS
272 $(Q)mkdir -p $(dir $@)
274 ${Q}$(strip ${ANALYZE.c} --analyze -Xanalyzer -analyzer-output=html -c $< -o $@ ${CPPFLAGS} \
275 ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS}) || (rm -f $@ && false)
279 # COMPILE_CXX_CMDS - Commands for compiling C++ source code.
280 define COMPILE_CXX_CMDS
281 $(Q)mkdir -p $(dir $@)
282 $(Q)$(strip ${COMPILE.cxx} -o $@ -c -MD ${CPPFLAGS} ${CXXFLAGS} ${SRC_CXXFLAGS} ${INCDIRS} \
283 ${SRC_INCDIRS} ${SRC_DEFS} ${DEFS} $<)
286 # INCLUDE_SUBMAKEFILE - Parameterized "function" that includes a new
287 # "submakefile" fragment into the overall Makefile. It also recursively
288 # includes all submakefiles of the specified submakefile fragment.
292 define INCLUDE_SUBMAKEFILE
293 # Initialize all variables that can be defined by a makefile fragment, then
294 # include the specified makefile fragment.
316 # A directory stack is maintained so that the correct paths are used as we
317 # recursively include all submakefiles. Get the makefile's directory and
318 # push it onto the stack.
319 DIR := $(call CANONICAL_PATH,$(dir ${1}))
320 DIR_STACK := $$(call PUSH,$${DIR_STACK},$${DIR})
324 # Initialize internal local variables.
327 # Determine which target this makefile's variables apply to. A stack is
328 # used to keep track of which target is the "current" target as we
329 # recursively include other submakefiles.
330 ifneq "$$(strip $${TARGET})" ""
331 # This makefile defined a new target. Target variables defined by this
332 # makefile apply to this new target. Initialize the target's variables.
334 # libs go into ${BUILD_DIR}/lib
335 # everything else goes into ${BUILD_DIR}/bin
336 # TGT := $$(strip $$(if $$(suffix $${TARGET}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)/$${TARGET})
339 # A "hook" to rewrite "libfoo.a" -> "libfoo.la" when using libtool
340 $$(eval $$(call ADD_LIBTOOL_SUFFIX))
343 $${TGT}_LDFLAGS := $${TGT_LDFLAGS}
344 $${TGT}_LDLIBS := $${TGT_LDLIBS}
345 $${TGT}_LINKER := $${TGT_LINKER}
346 $${TGT}_POSTMAKE := $${TGT_POSTMAKE}
347 $${TGT}_POSTCLEAN := $${TGT_POSTCLEAN}
348 $${TGT}_POSTINSTALL := $${TGT_POSTINSTALL}
349 $${TGT}_PREREQS := $${TGT_PREREQS}
350 $${TGT}_PRBIN := $$(addprefix $${BUILD_DIR}/bin/,$$(filter-out %.a %.so %.la,$${TGT_PREREQS}))
351 $${TGT}_PRLIBS := $$(addprefix $${BUILD_DIR}/lib/,$$(filter %.a %.so %.la,$${TGT_PREREQS}))
355 $${TGT}_MAN := $${MAN}
356 $${TGT}_SUFFIX := $$(if $$(suffix $${TGT}),$$(suffix $${TGT}),.exe)
358 # If it's an EXE, ensure that transitive library linking works.
359 # i.e. we build libfoo.a which in turn requires -lbar. So, the executable
360 # has to be linked to both libfoo.a and -lbar.
361 ifeq "$${$${TGT}_SUFFIX}" ".exe"
362 $${TGT}_LDLIBS += $$(filter-out %.a %.so %.la,$${$${TGT_PREREQS}_LDLIBS})
365 $${TGT}_BUILD := $$(if $$(suffix $${TGT}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)
366 $${TGT}_MAKEFILES += ${1}
367 $${TGT}_CHECK_HEADERS := $${TGT_CHECK_HEADERS}
368 $${TGT}_CHECK_LIBS := $${TGT_CHECK_LIBS}
370 # The values defined by this makefile apply to the the "current" target
371 # as determined by which target is at the top of the stack.
372 TGT := $$(strip $$(call PEEK,$${TGT_STACK}))
373 $${TGT}_LDFLAGS += $${TGT_LDFLAGS}
374 $${TGT}_LDLIBS += $${TGT_LDLIBS}
375 $${TGT}_POSTCLEAN += $${TGT_POSTCLEAN}
376 $${TGT}_POSTMAKE += $${TGT_POSTMAKE}
377 $${TGT}_PREREQS += $${TGT_PREREQS}
380 # Push the current target onto the target stack.
381 TGT_STACK := $$(call PUSH,$${TGT_STACK},$${TGT})
383 # If there's no target, don't build the sources.
384 ifneq "$$(strip $${TARGET})" ""
386 # if there's no sources, don't do the automatic object build
387 ifneq "$$(strip $${SOURCES})" ""
388 # This makefile builds one or more objects from source. Validate the
389 # specified sources against the supported source file types.
390 BAD_SRCS := $$(strip $$(filter-out $${ALL_SRC_EXTS},$${SOURCES}))
391 ifneq "$${BAD_SRCS}" ""
392 $$(error Unsupported source file(s) found in ${1} [$${BAD_SRCS}])
395 # Qualify and canonicalize paths.
396 SOURCES := $$(call QUALIFY_PATH,$${DIR},$${SOURCES})
397 SOURCES := $$(call CANONICAL_PATH,$${SOURCES})
398 SRC_INCDIRS := $$(call QUALIFY_PATH,$${DIR},$${SRC_INCDIRS})
399 SRC_INCDIRS := $$(call CANONICAL_PATH,$${SRC_INCDIRS})
401 # Save the list of source files for this target.
402 $${TGT}_SOURCES += $${SOURCES}
404 # Convert the source file names to their corresponding object file
406 OBJS := $$(addprefix $${BUILD_DIR}/objs/,\
407 $$(addsuffix .${OBJ_EXT},$$(basename $${SOURCES})))
409 PLISTS := $$(addprefix $${BUILD_DIR}/plist/,\
410 $$(addsuffix .plist,$$(basename $${SOURCES})))
411 ALL_PLISTS += ${PLISTS}
413 # Add the objects to the current target's list of objects, and create
414 # target-specific variables for the objects based on any source
415 # variables that were defined.
416 $${TGT}_OBJS += $${OBJS}
417 $${TGT}_PLISTS += $${PLISTS}
418 $${TGT}_DEPS += $$(addprefix $${BUILD_DIR}/make/src/,\
419 $$(addsuffix .mk,$$(basename $${SOURCES})))
421 # A "hook" to define variables needed by the "legacy" makefiles.
422 $$(eval $$(call ADD_LEGACY_VARIABLES,$$(dir ${1}),$${TGT}))
424 $${OBJS}: SRC_CFLAGS := $${SRC_CFLAGS}
425 $${OBJS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
426 $${OBJS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
427 $${OBJS}: SRC_INCDIRS := $$(addprefix -I,$${SRC_INCDIRS})
430 $${PLISTS}: SRC_CFLAGS := $${SRC_CFLAGS}
431 $${PLISTS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
432 $${PLISTS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
433 $${PLISTS}: SRC_INCDIRS := $$(addprefix -I,$${SRC_INCDIRS})
438 ifneq "$$(strip $${SUBMAKEFILES})" ""
439 # This makefile has submakefiles. Recursively include them.
440 $$(foreach MK,$${SUBMAKEFILES},\
441 $$(eval $$(call INCLUDE_SUBMAKEFILE,\
442 $$(call CANONICAL_PATH,\
443 $$(call QUALIFY_PATH,$${DIR},$${MK})))))
446 # Reset the "current" target to it's previous value.
447 TGT_STACK := $$(call POP,$${TGT_STACK})
448 # If we're about to change targets, create the rules for the target
449 ifneq "$${TGT}" "$$(call PEEK,$${TGT_STACK})"
450 # add rules to build the target, and have "all" depend on it.
451 $$(eval $$(call ADD_TARGET_TO_ALL,$${TGT}))
453 # A "hook" to add rules for ${TARGET_DIR}/foo, if TARGET_DIR
454 # is defined. Otherwise, we leave the source directory untouched.
455 $$(eval $$(call ADD_TARGET_DIR,$${TGT}))
457 # A "hook" to build the libtool target.
458 $$(eval $$(call ADD_LIBTOOL_TARGET))
460 # Choose the correct linker.
461 ifeq "$$(strip $$(filter $${CXX_SRC_EXTS},$${$${TGT}_SOURCES}))" ""
462 ifeq "$${$${TGT}_LINKER}" ""
463 $${TGT}_LINKER := ${LL}$${LINK.c}
466 ifeq "$${$${TGT}_LINKER}" ""
467 $${TGT}_LINKER := ${LL}$${LINK.cxx}
471 # add rules to build the target
472 $$(eval $$(call ADD_TARGET_RULE$${$${TGT}_SUFFIX},$${TGT}))
474 # generate the clean rule for this target.
475 $$(eval $$(call ADD_CLEAN_RULE,$${TGT}))
477 # Hook to add an installation target
478 $$(eval $$(call ADD_INSTALL_TARGET,$${TGT}))
480 # Hook to add a configuration target
481 $$(eval $$(call ADD_TARGET_CONFIG,$${TGT}))
483 # "hook" for legacy Makefiles
484 $$(eval $$(call ADD_LEGACY_RULE,$${TGT}))
487 TGT := $$(call PEEK,$${TGT_STACK})
489 # Reset the "current" directory to it's previous value.
490 DIR_STACK := $$(call POP,$${DIR_STACK})
491 DIR := $$(call PEEK,$${DIR_STACK})
494 # MIN - Parameterized "function" that results in the minimum lexical value of
495 # the two values given.
497 $(firstword $(sort ${1} ${2}))
500 # PEEK - Parameterized "function" that results in the value at the top of the
501 # specified colon-delimited stack.
503 $(lastword $(subst :, ,${1}))
506 # POP - Parameterized "function" that pops the top value off of the specified
507 # colon-delimited stack, and results in the new value of the stack. Note that
508 # the popped value cannot be obtained using this function; use peek for that.
510 ${1:%:$(lastword $(subst :, ,${1}))=%}
513 # PUSH - Parameterized "function" that pushes a value onto the specified colon-
514 # delimited stack, and results in the new value of the stack.
519 # QUALIFY_PATH - Given a "root" directory and one or more paths, qualifies the
520 # paths using the "root" directory (i.e. appends the root directory name to
521 # the paths) except for paths that are absolute.
523 $(addprefix ${1}/,$(filter-out /%,${2})) $(filter /%,${2})
526 ###############################################################################
528 # Start of Makefile Evaluation
530 ###############################################################################
532 # Older versions of GNU Make lack capabilities needed by boilermake.
533 # With older versions, "make" may simply output "nothing to do", likely leading
534 # to confusion. To avoid this, check the version of GNU make up-front and
535 # inform the user if their version of make doesn't meet the minimum required.
536 MIN_MAKE_VERSION := 3.81
537 MIN_MAKE_VER_MSG := boilermake requires GNU Make ${MIN_MAKE_VERSION} or greater
538 ifeq "${MAKE_VERSION}" ""
539 $(info GNU Make not detected)
540 $(error ${MIN_MAKE_VER_MSG})
542 ifneq "${MIN_MAKE_VERSION}" "$(call MIN,${MIN_MAKE_VERSION},${MAKE_VERSION})"
543 $(info This is GNU Make version ${MAKE_VERSION})
544 $(error ${MIN_MAKE_VER_MSG})
547 # Define the source file extensions that we know how to handle.
550 CXX_SRC_EXTS := %.C %.cc %.cp %.cpp %.CPP %.cxx %.c++
551 ALL_SRC_EXTS := ${C_SRC_EXTS} ${CXX_SRC_EXTS}
553 # Initialize global variables.
560 ifeq "${top_builddir}" ""
564 # Ensure that valid values are set for BUILD_DIR
565 ifeq "$(strip ${BUILD_DIR})" ""
566 ifeq "${top_builddir}" "${PWD}"
569 BUILD_DIR := ${top_builddir}/build
572 BUILD_DIR := $(call CANONICAL_PATH,${BUILD_DIR})
575 # Define compilers and linkers
583 # Set ECHO to "true" for *very* quiet builds
586 # Define the "all" target (which simply builds all user-defined targets) as the
591 # Add "clean" rules to remove all build-generated files.
595 top_makedir := $(dir $(lastword ${MAKEFILE_LIST}))
597 -include ${top_makedir}/install.mk
598 -include ${top_makedir}/libtool.mk
600 # Include the main user-supplied submakefile. This also recursively includes
601 # all other user-supplied submakefiles.
602 $(eval $(call INCLUDE_SUBMAKEFILE,${top_builddir}/main.mk))
604 # Perform post-processing on global variables as needed.
605 DEFS := $(addprefix -D,${DEFS})
606 INCDIRS := $(addprefix -I,$(call CANONICAL_PATH,${INCDIRS}))
608 # Add pattern rule(s) for creating compiled object code from C source.
609 $(foreach EXT,${C_SRC_EXTS},\
610 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_C_CMDS})))
612 ifneq "${ANALYZE.c}" ""
613 $(foreach EXT,${C_SRC_EXTS},\
614 $(eval $(call ADD_ANALYZE_RULE,${EXT},$${ANALYZE_C_CMDS})))
617 # Add pattern rule(s) for creating compiled object code from C++ source.
618 $(foreach EXT,${CXX_SRC_EXTS},\
619 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_CXX_CMDS})))
621 # Don't include the target dependencies if we're doing a "make clean"
622 # Future: have a list of targets that don't require dependency generation,
623 # and see if MAKECMDGOALS is one of them.
624 ifneq "$(MAKECMDGOALS)" "clean"
625 $(foreach TGT,${ALL_TGTS},\
626 $(eval -include ${${TGT}_DEPS}))
633 $(Q)rm -f ${ALL_PLISTS}