autotargets.mk (3208B)
1 # -*- makefile -*- 2 # vim:set ts=8 sw=8 sts=8 noet: 3 # 4 # This Source Code Form is subject to the terms of the Mozilla Public 5 # License, v. 2.0. If a copy of the MPL was not distributed with this file, 6 # You can obtain one at http://mozilla.org/MPL/2.0/. 7 # 8 9 ifndef INCLUDED_AUTOTARGETS_MK #{ 10 11 # Conditional does not wrap the entire file so multiple 12 # includes will be able to accumulate dependencies. 13 14 ########################################################################### 15 # AUTO_DEPS - A list of deps/targets drived from other macros. 16 ########################################################################### 17 18 MKDIR ?= mkdir -p 19 TOUCH ?= touch 20 21 # declare for local use, rules.mk may not have been loaded 22 space = $(NULL) $(NULL) 23 24 # Deps will be considered intermediate when used as a pre-requisite for 25 # wildcard targets. Inhibit their removal, mkdir -p is a standalone op. 26 .PRECIOUS: %/.mkdir.done 27 28 ######################### 29 ##---] FUNCTIONS [---## 30 ######################### 31 32 # Squeeze can be overzealous, restore root for abspath 33 getPathPrefix =$(if $(filter /%,$(1)),/) 34 35 # Squeeze '//' from the path, easily created by string functions 36 _slashSqueeze =$(foreach val,$(getargv),$(call getPathPrefix,$(val))$(subst $(space),/,$(strip $(subst /,$(space),$(val))))) 37 38 # Squeeze extraneous directory slashes from the path 39 # o protect embedded spaces within the path 40 # o replace //+ sequences with / 41 slash_strip = \ 42 $(strip \ 43 $(subst <--[**]-->,$(space),\ 44 $(call _slashSqueeze,\ 45 $(subst $(space),<--[**]-->,$(1))\ 46 ))) 47 48 # Extract directory path from a dependency file. 49 mkdir_stem =$(foreach val,$(getargv),$(subst /.mkdir.done,$(NULL),$(val))) 50 51 ## Generate timestamp file for threadsafe directory creation 52 mkdir_deps =$(foreach dir,$(getargv),$(call slash_strip,$(dir)/.mkdir.done)) 53 54 ####################### 55 ##---] TARGETS [---## 56 ####################### 57 58 %/.mkdir.done: # mkdir -p -p => mkdir -p 59 $(subst $(space)-p,$(null),$(MKDIR)) -p '$(dir $@)' 60 # Make the timestamp old enough for not being a problem with symbolic links 61 # targets depending on it. Use Jan 3, 1980 to accomodate any timezone where 62 # 198001010000 would translate to something older than FAT epoch. 63 @$(TOUCH) -t 198001030000 '$@' 64 65 # A handful of makefiles are attempting "mkdir dot". 66 # tbpl/valgrind builds are using this target 67 # https://bugzilla.mozilla.org/show_bug.cgi?id=837754 68 .mkdir.done: 69 @echo 'WARNING: $(MKDIR) -dot- requested by $(MAKE) -C $(CURDIR) $(MAKECMDGOALS)' 70 @$(TOUCH) -t 198001030000 '$@' 71 72 INCLUDED_AUTOTARGETS_MK = 1 73 endif #} 74 75 76 ## Accumulate deps and cleanup 77 ifneq (,$(GENERATED_DIRS)) 78 GENERATED_DIRS := $(strip $(sort $(GENERATED_DIRS))) 79 tmpauto :=$(call mkdir_deps,GENERATED_DIRS) 80 GENERATED_DIRS_DEPS +=$(tmpauto) 81 endif 82 83 ################################################################# 84 # One ring/dep to rule them all: 85 # config/rules.mk::all target is available by default 86 # Add $(AUTO_DEPS) as an explicit target dependency when needed. 87 ################################################################# 88 89 AUTO_DEPS +=$(GENERATED_DIRS_DEPS) 90 AUTO_DEPS := $(strip $(sort $(AUTO_DEPS))) 91 92 # Complain loudly if deps have not loaded so getargv != $(NULL) 93 $(call requiredfunction,getargv)