autotargets.mk 3.2 KB

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