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