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} ${JLIBTOOL}
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} ${JLIBTOOL}
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}_PRLIBS}
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 # If there's no target, don't build the sources.
345 ifneq "$$(strip $${TARGET})" ""
347 # if there's no sources, don't do the automatic object build
348 ifneq "$$(strip $${SOURCES})" ""
349 # This makefile builds one or more objects from source. Validate the
350 # specified sources against the supported source file types.
351 BAD_SRCS := $$(strip $$(filter-out $${ALL_SRC_EXTS},$${SOURCES}))
352 ifneq "$${BAD_SRCS}" ""
353 $$(error Unsupported source file(s) found in ${1} [$${BAD_SRCS}])
356 # Qualify and canonicalize paths.
357 SOURCES := $$(call QUALIFY_PATH,$${DIR},$${SOURCES})
358 SOURCES := $$(call CANONICAL_PATH,$${SOURCES})
359 SRC_INCDIRS := $$(call QUALIFY_PATH,$${DIR},$${SRC_INCDIRS})
360 SRC_INCDIRS := $$(call CANONICAL_PATH,$${SRC_INCDIRS})
362 # Save the list of source files for this target.
363 $${TGT}_SOURCES += $${SOURCES}
365 # Convert the source file names to their corresponding object file
367 OBJS := $$(addprefix $${BUILD_DIR}/objs/,\
368 $$(addsuffix .${OBJ_EXT},$$(basename $${SOURCES})))
370 # Add the objects to the current target's list of objects, and create
371 # target-specific variables for the objects based on any source
372 # variables that were defined.
373 $${TGT}_OBJS += $${OBJS}
374 $${TGT}_DEPS += $$(addprefix $${BUILD_DIR}/make/src/,\
375 $$(addsuffix .mk,$$(basename $${SOURCES})))
377 # A "hook" to define variables needed by the "legacy" makefiles.
378 $$(eval $$(call ADD_LEGACY_VARIABLES,$$(dir ${1}),$${TGT}))
380 $${OBJS}: SRC_CFLAGS := $${SRC_CFLAGS}
381 $${OBJS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
382 $${OBJS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
383 $${OBJS}: SRC_INCDIRS := $$(addprefix -I,$${SRC_INCDIRS})
387 ifneq "$$(strip $${SUBMAKEFILES})" ""
388 # This makefile has submakefiles. Recursively include them.
389 $$(foreach MK,$${SUBMAKEFILES},\
390 $$(eval $$(call INCLUDE_SUBMAKEFILE,\
391 $$(call CANONICAL_PATH,\
392 $$(call QUALIFY_PATH,$${DIR},$${MK})))))
395 # Reset the "current" target to it's previous value.
396 TGT_STACK := $$(call POP,$${TGT_STACK})
397 # If we're about to change targets, create the rules for the target
398 ifneq "$${TGT}" "$$(call PEEK,$${TGT_STACK})"
399 # add rules to build the target, and have "all" depend on it.
400 $$(eval $$(call ADD_TARGET_TO_ALL,$${TGT}))
402 # A "hook" to add rules for ${TARGET_DIR}/foo, if TARGET_DIR
403 # is defined. Otherwise, we leave the source directory untouched.
404 $$(eval $$(call ADD_TARGET_DIR,$${TGT}))
406 # A "hook" to build the libtool target.
407 $$(eval $$(call ADD_LIBTOOL_TARGET))
409 # Choose the correct linker.
410 ifeq "$$(strip $$(filter $${CXX_SRC_EXTS},$${$${TGT}_SOURCES}))" ""
411 ifeq "$${$${TGT}_LINKER}" ""
412 $${TGT}_LINKER := ${LL}$${LINK.c}
415 ifeq "$${$${TGT}_LINKER}" ""
416 $${TGT}_LINKER := ${LL}$${LINK.cxx}
420 # add rules to build the target
421 $$(eval $$(call ADD_TARGET_RULE$${$${TGT}_SUFFIX},$${TGT}))
423 # generate the clean rule for this target.
424 $$(eval $$(call ADD_CLEAN_RULE,$${TGT}))
426 # Hook to add an installation target
427 $$(eval $$(call ADD_INSTALL_TARGET,$${TGT}))
429 # Hook to add a configuration target
430 $$(eval $$(call ADD_TARGET_CONFIG,$${TGT}))
432 # "hook" for legacy Makefiles
433 $$(eval $$(call ADD_LEGACY_RULE,$${TGT}))
436 TGT := $$(call PEEK,$${TGT_STACK})
438 # Reset the "current" directory to it's previous value.
439 DIR_STACK := $$(call POP,$${DIR_STACK})
440 DIR := $$(call PEEK,$${DIR_STACK})
443 # MIN - Parameterized "function" that results in the minimum lexical value of
444 # the two values given.
446 $(firstword $(sort ${1} ${2}))
449 # PEEK - Parameterized "function" that results in the value at the top of the
450 # specified colon-delimited stack.
452 $(lastword $(subst :, ,${1}))
455 # POP - Parameterized "function" that pops the top value off of the specified
456 # colon-delimited stack, and results in the new value of the stack. Note that
457 # the popped value cannot be obtained using this function; use peek for that.
459 ${1:%:$(lastword $(subst :, ,${1}))=%}
462 # PUSH - Parameterized "function" that pushes a value onto the specified colon-
463 # delimited stack, and results in the new value of the stack.
468 # QUALIFY_PATH - Given a "root" directory and one or more paths, qualifies the
469 # paths using the "root" directory (i.e. appends the root directory name to
470 # the paths) except for paths that are absolute.
472 $(addprefix ${1}/,$(filter-out /%,${2})) $(filter /%,${2})
475 ###############################################################################
477 # Start of Makefile Evaluation
479 ###############################################################################
481 # Older versions of GNU Make lack capabilities needed by boilermake.
482 # With older versions, "make" may simply output "nothing to do", likely leading
483 # to confusion. To avoid this, check the version of GNU make up-front and
484 # inform the user if their version of make doesn't meet the minimum required.
485 MIN_MAKE_VERSION := 3.81
486 MIN_MAKE_VER_MSG := boilermake requires GNU Make ${MIN_MAKE_VERSION} or greater
487 ifeq "${MAKE_VERSION}" ""
488 $(info GNU Make not detected)
489 $(error ${MIN_MAKE_VER_MSG})
491 ifneq "${MIN_MAKE_VERSION}" "$(call MIN,${MIN_MAKE_VERSION},${MAKE_VERSION})"
492 $(info This is GNU Make version ${MAKE_VERSION})
493 $(error ${MIN_MAKE_VER_MSG})
496 # Define the source file extensions that we know how to handle.
499 CXX_SRC_EXTS := %.C %.cc %.cp %.cpp %.CPP %.cxx %.c++
500 ALL_SRC_EXTS := ${C_SRC_EXTS} ${CXX_SRC_EXTS}
502 # Initialize global variables.
509 ifeq "${top_builddir}" ""
513 # Ensure that valid values are set for BUILD_DIR
514 ifeq "$(strip ${BUILD_DIR})" ""
515 ifeq "${top_builddir}" "${PWD}"
518 BUILD_DIR := ${top_builddir}/build
521 BUILD_DIR := $(call CANONICAL_PATH,${BUILD_DIR})
524 # Define compilers and linkers
532 # Set ECHO to "true" for *very* quiet builds
535 # Define the "all" target (which simply builds all user-defined targets) as the
540 # Add "clean" rules to remove all build-generated files.
544 top_makedir := $(dir $(lastword ${MAKEFILE_LIST}))
546 -include ${top_makedir}/install.mk
547 -include ${top_makedir}/libtool.mk
549 # Include the main user-supplied submakefile. This also recursively includes
550 # all other user-supplied submakefiles.
551 $(eval $(call INCLUDE_SUBMAKEFILE,${top_builddir}/main.mk))
553 # Perform post-processing on global variables as needed.
554 DEFS := $(addprefix -D,${DEFS})
555 INCDIRS := $(addprefix -I,$(call CANONICAL_PATH,${INCDIRS}))
557 # Add pattern rule(s) for creating compiled object code from C source.
558 $(foreach EXT,${C_SRC_EXTS},\
559 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_C_CMDS})))
561 # Add pattern rule(s) for creating compiled object code from C++ source.
562 $(foreach EXT,${CXX_SRC_EXTS},\
563 $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_CXX_CMDS})))
565 # Don't include the target dependencies if we're doing a "make clean"
566 # Future: have a list of targets that don't require dependency generation,
567 # and see if MAKECMDGOALS is one of them.
568 ifneq "$(MAKECMDGOALS)" "clean"
569 $(foreach TGT,${ALL_TGTS},\
570 $(eval -include ${${TGT}_DEPS}))