###################################################################### # Makefile # # This makefile is the one called by user, but it's only a trampoline # to create the build directory and call the real Makefile.build # # make can be invoqued with 'make build= ...' # all other options and target are passed to the real makefile. # # As an exception to the rule of not modifying source directory, a # .build.mak file is created to cache the configured build directory. # Subsequent invocations of make could be done without passing # 'build=...' parameter. # # distclean target is catched to destroy both the .build.mak and # the whole build directory. # ###################################################################### # build directory is cached in BUILD_MAK file between make invocations BUILD_MAK = .build.mak ifneq ($(wildcard $(BUILD_MAK)),) include $(BUILD_MAK) endif # default build directory 'build' build ?= build # ($call quiet-command,CMD,MSG) # execute CMD and display MSG # if verbose mode is enabled (with 'make V=1`) print full CMD instead of MSG quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1)) # if the target is distclean, skip allmost rules # to doing anything which will be immediately # deleted after ifeq ($(filter distclean,$(MAKECMDGOALS)),) # default target (when one invokes 'make' without target) # so we have a non-empty target which could be catched by # the catch-all target below. default-all: # The catch-all target, which pass build to the main # makefile in the build directory. # strip-out 'default-all' for targets' list %: | $(build) $(call quiet-command, \ $(MAKE) -C $(build) $(filter-out default-all,$@) \ , "MAKE $(filter-out default-all,$@)") # Creates the buld directory # and populates it with a link to the build makefile $(build): | $(BUILD_MAK) $(call quiet-command,\ mkdir -p $@; \ ln -sf $(CURDIR)/Makefile.build $@/Makefile \ , "MKBUILD $@") # always create BUILD_MAK to cache build configuration # but don't update it if not changed $(BUILD_MAK): FORCE @echo build=$(build) > $@.tmp @cmp -s $@.tmp $@ || (mv $@.tmp $@ && echo "GEN $@") @rm -f $@.tmp else # if distclean distclean: $(call quiet-command, rm -rf $(build) $(BUILD_MAK), "DISTCLEAN $(build)") endif # distclean .PHONY: distclean FORCE #ifndef V #.SILENT: #endif