Automatically create install directories.
[freeradius.git] / scripts / boiler.mk
1 # boilermake: A reusable, but flexible, boilerplate Makefile.
2 #
3 # Copyright 2008, 2009, 2010 Dan Moulding, Alan T. DeKok
4 #
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.
9 #
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.
14 #
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/>.
17
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).
22
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.
29
30 #
31 #  You can watch what it's doing by:
32 #
33 #       $ VERBOSE=1 make ... args ...
34 #
35 ifeq "${VERBOSE}" ""
36     Q=@
37 else
38     Q=
39 endif
40
41 # ADD_CLEAN_RULE - Parameterized "function" that adds a new rule and phony
42 #   target for cleaning the specified target (removing its build-generated
43 #   files).
44 #
45 #   USE WITH EVAL
46 #
47 define ADD_CLEAN_RULE
48     clean: clean_$(notdir ${1})
49     .PHONY: clean_$(notdir ${1})
50     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}))
52         $${${1}_POSTCLEAN}
53
54 endef
55
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.
61 #
62 #  COMMON filters:
63 #       remove comments
64 #       remove dependencies on global include files
65 #       remove empty dependencies
66 #       remove CPP hacks like "foo: <built-in>"
67 #
68 #  1) Filter the .d file to remove unnecessary cruft
69 #
70 #       COMMON
71 #       Replace ".o" with "${OBJ_EXT}"
72 #       delete empty continuation lines
73 #       delete blank 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
78 #
79 #       remove sequential duplicate lines
80 #
81 #  2) Create empty dependencies from the files
82 #
83 #       COMMON
84 #       remove existing targets
85 #       remove continuations (to make the targets stand by themselves)
86 #       delete blank lines
87 #       add in empty dependency for each file.
88 #       remove sequential duplicate lines
89 #
90 define FILTER_DEPENDS
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},' \
99           -e 's, /[^: ]*,,g' \
100           -e 's,^ *[^:]* *: *$$$$,,' \
101           -e '/: </ d' \
102           -e 's/\.o: /.$$$${OBJ_EXT}: /' \
103           -e '/^ *\\$$$$/ d' \
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/[^ :]*,,' \
108           -e 's, /[^: ]*,,g' \
109           -e 's,^ *[^:]* *: *$$$$,,' \
110           -e '/: </ d' \
111           -e 's/^[^:]*: *//' \
112           -e 's/ *\\$$$$//' \
113           -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
117 endef
118
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.
122 #
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...
126 #
127 #   USE WITH EVAL
128 #
129 ifeq "${CPP_MAKEDEPEND}" "yes"
130 define ADD_OBJECT_RULE
131 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1} | ${BOOTSTRAP_BUILD}
132         ${2}
133         $${CPP} $${CPPFLAGS} $$(addprefix -I,$${SRC_INCDIRS}) $${SRC_DEFS} $$< | sed \
134           -n 's,^\# *[0-9][0-9]* *"\([^"]*\)".*,$$@: \1,p' > $${BUILD_DIR}/objs/$$*.d
135 ${FILTER_DEPENDS}
136 endef
137
138 else
139 define ADD_OBJECT_RULE
140 $${BUILD_DIR}/objs/%.${OBJ_EXT} $${BUILD_DIR}/objs/%.d: ${1} | ${BOOTSTRAP_BUILD}
141         ${2}
142 ${FILTER_DEPENDS}
143 endef
144 endif
145
146 define ADD_ANALYZE_RULE
147 $${BUILD_DIR}/plist/%.plist: ${1}
148         ${2}
149 endef
150
151 # ADD_TARGET_DIR - Parameterized "function" that makes a link from
152 #   TARGET_DIR to the executable or library in the BUILD_DIR directory.
153 #
154 #   USE WITH EVAL
155 #
156 ifneq "${TARGET_DIR}" ""
157     define ADD_TARGET_DIR
158         all: $${TARGET_DIR}/$$(notdir ${1})
159
160         $${TARGET_DIR}/$$(notdir ${1}): ${1}
161             [ -f $${TARGET_DIR}/$$(notdir ${1}) ] || ln -s ${1} $${TARGET_DIR}/$$(notdir ${1})
162
163     endef
164 endif
165
166 # ADD_TARGET_TO_ALL - Parameterized "function" that adds the target,
167 #   and makes "all" depend on it.
168 #
169 #   USE WITH EVAL
170 #
171 define ADD_TARGET_TO_ALL
172     all: ${1}
173
174 endef
175
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.
179 #
180 #   New rules can be added by copying one of the existing ones, and
181 #   replacing the line after the "mkdir"
182 #
183
184 # ADD_TARGET_RULE.exe - Build an executable target.
185 #
186 #   USE WITH EVAL
187 #
188 define ADD_TARGET_RULE.exe
189     # So "make ${1}" works
190     .PHONY: ${1}
191     ${1}: $${${1}_BUILD}/${1}
192
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}
201
202     ifneq "${ANALYZE.c}" ""
203         scan.${1}: $${${1}_PLISTS}
204     endif
205 endef
206
207 # ADD_TARGET_RULE.a - Build a static library target.
208 #
209 #   USE WITH EVAL
210 #
211 define ADD_TARGET_RULE.a
212     # So "make ${1}" works
213     .PHONY: ${1}
214     ${1}: $${${1}_BUILD}/${1}
215
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}
222
223     ifneq "${ANALYZE.c}" ""
224         scan.${1}: $${${1}_PLISTS}
225     endif
226 endef
227
228 # ADD_TARGET_RULE.so - Build a ".so" target.
229 #
230 #   USE WITH EVAL
231 #
232 define ADD_TARGET_RULE.so
233 $(error Please add rules to build a ".so" file.)
234 endef
235
236 # ADD_TARGET_RULE.dll - Build a ".dll" target.
237 #
238 #   USE WITH EVAL
239 #
240 define ADD_TARGET_RULE.dll
241 $(error Please add rules to build a ".dll" file.)
242 endef
243
244 # ADD_TARGET_RULE.dylib - Build a ".dylib" target.
245 #
246 #   USE WITH EVAL
247 #
248 define ADD_TARGET_RULE.dylib
249 $(error Please add rules to build a ".dylib" file.)
250 endef
251
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}))
260 endef
261
262 # COMPILE_C_CMDS - Commands for compiling C source code.
263 ifeq "$(CPPCHECK)" ""
264 define COMPILE_C_CMDS
265         $(Q)mkdir -p $(dir $@)
266         $(Q)$(ECHO) CC $<
267         $(Q)$(strip ${COMPILE.c} -o $@ -c -MD ${CPPFLAGS} ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} \
268             $(addprefix -I, ${SRC_INCDIRS}) ${SRC_DEFS} ${DEFS} $<)
269 endef
270 else
271 #
272 #  do cppcheck AND compilation, so that we have correct dependencies
273 #  Suppress variable scope warnings for now.  They're style, and don't really
274 #  affect anything.
275 #
276 define COMPILE_C_CMDS
277         $(Q)mkdir -p $(dir $@)
278         $(Q)$(ECHO) CC $<
279         $(Q)$(strip ${COMPILE.c} -o $@ -c -MD ${CPPFLAGS} ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} \
280              $(addprefix -I,${SRC_INCDIRS}) ${SRC_DEFS} ${DEFS} $<)
281         $(Q)cppcheck --enable=style -q ${CHECKFLAGS} $(filter -isystem%,${SRC_CFLAGS}) \
282              $(filter -I%,${SRC_CFLAGS}) $(filter -D%,${SRC_CFLAGS}) ${INCDIRS} \
283              $(addprefix -I,${SRC_INCDIRS}) ${SRC_DEFS} ${DEFS} --suppress=variableScope --suppress=invalidscanf $<
284 endef
285 endif
286
287 # ANALYZE_C_CMDS - Commands for analyzing C source code with clang.
288 define ANALYZE_C_CMDS
289         $(Q)mkdir -p $(dir $@)
290         $(Q)$(ECHO) SCAN $<
291         $(Q)$(strip ${ANALYZE.c} --analyze -Xanalyzer -analyzer-output=html -c $< -o $@ ${CPPFLAGS} \
292             ${CFLAGS} ${SRC_CFLAGS} ${INCDIRS} $(addprefix -I,${SRC_INCDIRS}) ${SRC_DEFS} ${DEFS}) || (rm -f $@ && false)
293         $(Q)touch $@
294 endef
295
296 # COMPILE_CXX_CMDS - Commands for compiling C++ source code.
297 define COMPILE_CXX_CMDS
298         $(Q)mkdir -p $(dir $@)
299         $(Q)$(strip ${COMPILE.cxx} -o $@ -c -MD ${CPPFLAGS} ${CXXFLAGS} ${SRC_CXXFLAGS} ${INCDIRS} \
300             $(addprefix -I,${SRC_INCDIRS}) ${SRC_DEFS} ${DEFS} $<)
301 endef
302
303 # INCLUDE_SUBMAKEFILE - Parameterized "function" that includes a new
304 #   "submakefile" fragment into the overall Makefile. It also recursively
305 #   includes all submakefiles of the specified submakefile fragment.
306 #
307 #   USE WITH EVAL
308 #
309 define INCLUDE_SUBMAKEFILE
310     # Initialize all variables that can be defined by a makefile fragment, then
311     # include the specified makefile fragment.
312     TARGET :=
313     TGT_LDFLAGS :=
314     TGT_LDLIBS :=
315     TGT_LINKER :=
316     TGT_POSTCLEAN :=
317     TGT_POSTMAKE :=
318     TGT_PREREQS :=
319     TGT_POSTINSTALL :=
320     TGT_INSTALLDIR := ..
321     TGT_CHECK_HEADERS :=
322     TGT_CHECK_LIBS :=
323
324     SOURCES :=
325     SRC_CFLAGS :=
326     SRC_CXXFLAGS :=
327     SRC_DEFS :=
328     SRC_INCDIRS :=
329     MAN :=
330
331     SUBMAKEFILES :=
332
333     # A directory stack is maintained so that the correct paths are used as we
334     # recursively include all submakefiles. Get the makefile's directory and
335     # push it onto the stack.
336     DIR := $(call CANONICAL_PATH,$(dir ${1}))
337     DIR_STACK := $$(call PUSH,$${DIR_STACK},$${DIR})
338
339     include ${1}
340
341     # Initialize internal local variables.
342     OBJS :=
343
344     # Determine which target this makefile's variables apply to. A stack is
345     # used to keep track of which target is the "current" target as we
346     # recursively include other submakefiles.
347     ifneq "$$(strip $${TARGET})" ""
348         # This makefile defined a new target. Target variables defined by this
349         # makefile apply to this new target. Initialize the target's variables.
350
351         # libs go into ${BUILD_DIR}/lib
352         # everything else goes into ${BUILD_DIR}/bin
353 #        TGT := $$(strip $$(if $$(suffix $${TARGET}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)/$${TARGET})
354         TGT := $${TARGET}
355
356         # A "hook" to rewrite "libfoo.a" -> "libfoo.la" when using libtool
357         $$(eval $$(call ADD_LIBTOOL_SUFFIX))
358
359         ALL_TGTS += $${TGT}
360         $${TGT}_LDFLAGS := $${TGT_LDFLAGS}
361         $${TGT}_LDLIBS := $${TGT_LDLIBS}
362         $${TGT}_LINKER := $${TGT_LINKER}
363         $${TGT}_POSTMAKE := $${TGT_POSTMAKE}
364         $${TGT}_POSTCLEAN := $${TGT_POSTCLEAN}
365         $${TGT}_POSTINSTALL := $${TGT_POSTINSTALL}
366         $${TGT}_PREREQS := $${TGT_PREREQS}
367         $${TGT}_PRBIN := $$(addprefix $${BUILD_DIR}/bin/,$$(filter-out %.a %.so %.la,$${TGT_PREREQS}))
368         $${TGT}_PRLIBS := $$(addprefix $${BUILD_DIR}/lib/,$$(filter %.a %.so %.la,$${TGT_PREREQS}))
369         $${TGT}_DEPS :=
370         $${TGT}_OBJS :=
371         $${TGT}_SOURCES :=
372         $${TGT}_MAN := $${MAN}
373         $${TGT}_SUFFIX := $$(if $$(suffix $${TGT}),$$(suffix $${TGT}),.exe)
374
375         # If it's an EXE, ensure that transitive library linking works.
376         # i.e. we build libfoo.a which in turn requires -lbar.  So, the executable
377         # has to be linked to both libfoo.a and -lbar.
378         ifeq "$${$${TGT}_SUFFIX}" ".exe"
379                 $${TGT}_LDLIBS += $$(filter-out %.a %.so %.la,$${$${TGT_PREREQS}_LDLIBS})
380         endif
381
382         $${TGT}_BUILD := $$(if $$(suffix $${TGT}),$${BUILD_DIR}/lib,$${BUILD_DIR}/bin)
383         $${TGT}_MAKEFILES += ${1}
384         $${TGT}_CHECK_HEADERS := $${TGT_CHECK_HEADERS}
385         $${TGT}_CHECK_LIBS := $${TGT_CHECK_LIBS}
386     else
387         # The values defined by this makefile apply to the the "current" target
388         # as determined by which target is at the top of the stack.
389         TGT := $$(strip $$(call PEEK,$${TGT_STACK}))
390         $${TGT}_LDFLAGS   += $${TGT_LDFLAGS}
391         $${TGT}_LDLIBS    += $${TGT_LDLIBS}
392         $${TGT}_POSTCLEAN += $${TGT_POSTCLEAN}
393         $${TGT}_POSTMAKE  += $${TGT_POSTMAKE}
394         $${TGT}_PREREQS   += $${TGT_PREREQS}
395     endif
396
397     # Push the current target onto the target stack.
398     TGT_STACK := $$(call PUSH,$${TGT_STACK},$${TGT})
399
400     # If there's no target, don't build the sources.
401     ifneq "$$(strip $${TARGET})" ""
402
403     # if there's no sources, don't do the automatic object build
404     ifneq "$$(strip $${SOURCES})" ""
405         # This makefile builds one or more objects from source. Validate the
406         # specified sources against the supported source file types.
407         BAD_SRCS := $$(strip $$(filter-out $${ALL_SRC_EXTS},$${SOURCES}))
408         ifneq "$${BAD_SRCS}" ""
409             $$(error Unsupported source file(s) found in ${1} [$${BAD_SRCS}])
410         endif
411
412         # Qualify and canonicalize paths.
413         SOURCES     := $$(call QUALIFY_PATH,$${DIR},$${SOURCES})
414         SOURCES     := $$(call CANONICAL_PATH,$${SOURCES})
415         SRC_INCDIRS := $$(call QUALIFY_PATH,$${DIR},$${SRC_INCDIRS})
416         SRC_INCDIRS := $$(call CANONICAL_PATH,$${SRC_INCDIRS})
417
418         # Save the list of source files for this target.
419         $${TGT}_SOURCES += $${SOURCES}
420
421         # Convert the source file names to their corresponding object file
422         # names.
423         OBJS := $$(addprefix $${BUILD_DIR}/objs/,\
424                    $$(addsuffix .${OBJ_EXT},$$(basename $${SOURCES})))
425
426         PLISTS := $$(addprefix $${BUILD_DIR}/plist/,\
427                    $$(addsuffix .plist,$$(basename $${SOURCES})))
428         ALL_PLISTS += ${PLISTS}
429
430         # Add the objects to the current target's list of objects, and create
431         # target-specific variables for the objects based on any source
432         # variables that were defined.
433         $${TGT}_OBJS += $${OBJS}
434         $${TGT}_PLISTS += $${PLISTS}
435         $${TGT}_DEPS += $$(addprefix $${BUILD_DIR}/make/src/,\
436                    $$(addsuffix .mk,$$(basename $${SOURCES})))
437
438         # A "hook" to define variables needed by the "legacy" makefiles.
439         $$(eval $$(call ADD_LEGACY_VARIABLES,$$(dir ${1}),$${TGT}))
440
441         $${OBJS}: SRC_CFLAGS := $${SRC_CFLAGS}
442         $${OBJS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
443         $${OBJS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
444         $${OBJS}: SRC_INCDIRS := $${SRC_INCDIRS}
445         $${OBJS}: ${1}
446
447         $${PLISTS}: SRC_CFLAGS := $${SRC_CFLAGS}
448         $${PLISTS}: SRC_CXXFLAGS := $${SRC_CXXFLAGS}
449         $${PLISTS}: SRC_DEFS := $$(addprefix -D,$${SRC_DEFS})
450         $${PLISTS}: SRC_INCDIRS := $${SRC_INCDIRS}
451         $${PLISTS}: ${1}
452     endif
453     endif
454
455     ifneq "$$(strip $${SUBMAKEFILES})" ""
456         # This makefile has submakefiles. Recursively include them.
457         $$(foreach MK,$${SUBMAKEFILES},\
458            $$(eval $$(call INCLUDE_SUBMAKEFILE,\
459                       $$(call CANONICAL_PATH,\
460                          $$(call QUALIFY_PATH,$${DIR},$${MK})))))
461     endif
462
463     # Reset the "current" target to it's previous value.
464     TGT_STACK := $$(call POP,$${TGT_STACK})
465     # If we're about to change targets, create the rules for the target
466     ifneq "$${TGT}" "$$(call PEEK,$${TGT_STACK})"
467         # add rules to build the target, and have "all" depend on it.
468         $$(eval $$(call ADD_TARGET_TO_ALL,$${TGT}))
469
470         # A "hook" to add rules for ${TARGET_DIR}/foo, if TARGET_DIR
471         # is defined.  Otherwise, we leave the source directory untouched.
472         $$(eval $$(call ADD_TARGET_DIR,$${TGT}))
473
474         # A "hook" to build the libtool target.
475         $$(eval $$(call ADD_LIBTOOL_TARGET))
476
477         # Choose the correct linker.
478         ifeq "$$(strip $$(filter $${CXX_SRC_EXTS},$${$${TGT}_SOURCES}))" ""
479             ifeq "$${$${TGT}_LINKER}" ""
480                 $${TGT}_LINKER := ${LL}$${LINK.c}
481             endif
482         else
483             ifeq "$${$${TGT}_LINKER}" ""
484                 $${TGT}_LINKER := ${LL}$${LINK.cxx}
485             endif
486         endif
487
488         # add rules to build the target
489         $$(eval $$(call ADD_TARGET_RULE$${$${TGT}_SUFFIX},$${TGT}))
490
491         # generate the clean rule for this target.
492         $$(eval $$(call ADD_CLEAN_RULE,$${TGT}))
493
494         # Hook to add an installation target
495         $$(eval $$(call ADD_INSTALL_TARGET,$${TGT}))
496
497         # Hook to add a configuration target
498         $$(eval $$(call ADD_TARGET_CONFIG,$${TGT}))
499
500         # "hook" for legacy Makefiles
501         $$(eval $$(call ADD_LEGACY_RULE,$${TGT}))
502     endif
503
504     TGT := $$(call PEEK,$${TGT_STACK})
505
506     # Reset the "current" directory to it's previous value.
507     DIR_STACK := $$(call POP,$${DIR_STACK})
508     DIR := $$(call PEEK,$${DIR_STACK})
509 endef
510
511 # MIN - Parameterized "function" that results in the minimum lexical value of
512 #   the two values given.
513 define MIN
514 $(firstword $(sort ${1} ${2}))
515 endef
516
517 # PEEK - Parameterized "function" that results in the value at the top of the
518 #   specified colon-delimited stack.
519 define PEEK
520 $(lastword $(subst :, ,${1}))
521 endef
522
523 # POP - Parameterized "function" that pops the top value off of the specified
524 #   colon-delimited stack, and results in the new value of the stack. Note that
525 #   the popped value cannot be obtained using this function; use peek for that.
526 define POP
527 ${1:%:$(lastword $(subst :, ,${1}))=%}
528 endef
529
530 # PUSH - Parameterized "function" that pushes a value onto the specified colon-
531 #   delimited stack, and results in the new value of the stack.
532 define PUSH
533 ${2:%=${1}:%}
534 endef
535
536 # QUALIFY_PATH - Given a "root" directory and one or more paths, qualifies the
537 #   paths using the "root" directory (i.e. appends the root directory name to
538 #   the paths) except for paths that are absolute.
539 define QUALIFY_PATH
540 $(addprefix ${1}/,$(filter-out /%,${2})) $(filter /%,${2})
541 endef
542
543 ###############################################################################
544 #
545 # Start of Makefile Evaluation
546 #
547 ###############################################################################
548
549 # Older versions of GNU Make lack capabilities needed by boilermake.
550 # With older versions, "make" may simply output "nothing to do", likely leading
551 # to confusion. To avoid this, check the version of GNU make up-front and
552 # inform the user if their version of make doesn't meet the minimum required.
553 MIN_MAKE_VERSION := 3.81
554 MIN_MAKE_VER_MSG := boilermake requires GNU Make ${MIN_MAKE_VERSION} or greater
555 ifeq "${MAKE_VERSION}" ""
556     $(info GNU Make not detected)
557     $(error ${MIN_MAKE_VER_MSG})
558 endif
559 ifneq "${MIN_MAKE_VERSION}" "$(call MIN,${MIN_MAKE_VERSION},${MAKE_VERSION})"
560     $(info This is GNU Make version ${MAKE_VERSION})
561     $(error ${MIN_MAKE_VER_MSG})
562 endif
563
564 # Define the source file extensions that we know how to handle.
565 OBJ_EXT := o
566 C_SRC_EXTS := %.c
567 CXX_SRC_EXTS := %.C %.cc %.cp %.cpp %.CPP %.cxx %.c++
568 ALL_SRC_EXTS := ${C_SRC_EXTS} ${CXX_SRC_EXTS}
569
570 # Initialize global variables.
571 ALL_TGTS :=
572 DEFS :=
573 DIR_STACK :=
574 INCDIRS :=
575 TGT_STACK :=
576
577 ifeq "${top_builddir}" ""
578     top_builddir := .
579 endif
580
581 # Ensure that valid values are set for BUILD_DIR
582 ifeq "$(strip ${BUILD_DIR})" ""
583     ifeq "${top_builddir}" "${PWD}"
584         BUILD_DIR := build
585     else
586         BUILD_DIR := ${top_builddir}/build
587     endif
588 else
589     BUILD_DIR := $(call CANONICAL_PATH,${BUILD_DIR})
590 endif
591
592 # Define compilers and linkers
593 #
594 BOOTSTRAP_BUILD = 
595 COMPILE.c = ${CC}
596 COMPILE.cxx = ${CXX}
597 CPP = cc -E
598 LINK.c = ${CC}
599 LINK.cxx = ${CXX}
600
601 # Set ECHO to "true" for *very* quiet builds
602 ECHO = echo
603
604 # Define the "all" target (which simply builds all user-defined targets) as the
605 # default goal.
606 .PHONY: all
607 all:
608
609 # Add "clean" rules to remove all build-generated files.
610 .PHONY: clean
611 clean:
612
613 top_makedir := $(dir $(lastword ${MAKEFILE_LIST}))
614
615 -include ${top_makedir}/install.mk
616 -include ${top_makedir}/libtool.mk
617
618 ifneq "${CPPCHECK}" ""
619 CHECKFLAGS := -DCPPCHECK $(filter -isystem%,$(CPPFLAGS) $(CFLAGS)) $(filter -I%,$(CPPFLAGS) $(CFLAGS)) $(filter -D%,$(CPPFLAGS) $(CFLAGS))
620 endif
621
622 # Include the main user-supplied submakefile. This also recursively includes
623 # all other user-supplied submakefiles.
624 $(eval $(call INCLUDE_SUBMAKEFILE,${top_builddir}/main.mk))
625
626 # Perform post-processing on global variables as needed.
627 DEFS := $(addprefix -D,${DEFS})
628 INCDIRS := $(addprefix -I,$(call CANONICAL_PATH,${INCDIRS}))
629
630 # Add pattern rule(s) for creating compiled object code from C source.
631 $(foreach EXT,${C_SRC_EXTS},\
632   $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_C_CMDS})))
633
634 ifneq "${ANALYZE.c}" ""
635 $(foreach EXT,${C_SRC_EXTS},\
636   $(eval $(call ADD_ANALYZE_RULE,${EXT},$${ANALYZE_C_CMDS})))
637 endif
638
639 # Add pattern rule(s) for creating compiled object code from C++ source.
640 $(foreach EXT,${CXX_SRC_EXTS},\
641   $(eval $(call ADD_OBJECT_RULE,${EXT},$${COMPILE_CXX_CMDS})))
642
643 # Don't include the target dependencies if we're doing a "make clean"
644 # Future: have a list of targets that don't require dependency generation,
645 #  and see if MAKECMDGOALS is one of them.
646 ifneq "$(MAKECMDGOALS)" "clean"
647     $(foreach TGT,${ALL_TGTS},\
648       $(eval -include ${${TGT}_DEPS}))
649 endif
650
651 # Build rules for installation subdirectories
652 $(foreach D,$(sort $(dir ${ALL_INSTALL})),\
653   $(eval $(call ADD_INSTALL_RULE.dir,${D})))
654
655
656 scan: ${ALL_PLISTS}
657
658 .PHONY: clean.scan
659 clean.scan:
660         $(Q)rm -rf ${ALL_PLISTS}
661
662 clean: clean.scan