subdir.mk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. #
  3. # Copyright (C) 2007-2020 OpenWrt.org
  4. ifeq ($(MAKECMDGOALS),prereq)
  5. SUBTARGETS:=prereq
  6. PREREQ_ONLY:=1
  7. else
  8. SUBTARGETS:=$(DEFAULT_SUBDIR_TARGETS)
  9. endif
  10. subtarget-default = $(filter-out ., \
  11. $(if $($(1)/builddirs-$(2)),$($(1)/builddirs-$(2)), \
  12. $(if $($(1)/builddirs-default),$($(1)/builddirs-default), \
  13. $($(1)/builddirs))))
  14. define subtarget
  15. $(call warn_eval,$(1),t,T,$(1)/$(2): $($(1)/) $(foreach bd,$(call subtarget-default,$(1),$(2)),$(1)/$(bd)/$(2)))
  16. endef
  17. define ERROR
  18. ($(call MESSAGE, $(2)); $(if $(BUILD_LOG), echo "$(2)" >> $(BUILD_LOG_DIR)/$(1)/error.txt;) $(if $(3),, exit 1;))
  19. endef
  20. lastdir=$(word $(words $(subst /, ,$(1))),$(subst /, ,$(1)))
  21. diralias=$(if $(findstring $(1),$(call lastdir,$(1))),,$(call lastdir,$(1)))
  22. subdir_make_opts = \
  23. $(if $(SUBDIR_MAKE_DEBUG),-d) -r -C $(1) \
  24. BUILD_SUBDIR="$(1)" \
  25. BUILD_VARIANT="$(4)" \
  26. ALL_VARIANTS="$(5)"
  27. # 1: subdir
  28. # 2: target
  29. # 3: build type
  30. # 4: build variant
  31. # 5: all variants
  32. log_make = \
  33. $(if $(call debug,$(1),v),,@)+ \
  34. $(if $(BUILD_LOG), \
  35. set -o pipefail; \
  36. mkdir -p $(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4));) \
  37. $(SCRIPT_DIR)/time.pl "time: $(1)$(if $(4),/$(4))/$(if $(3),$(3)-)$(2)" \
  38. $$(SUBMAKE) $(subdir_make_opts) $(if $(3),$(3)-)$(2) \
  39. $(if $(BUILD_LOG),SILENT= 2>&1 | tee $(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4))/$(if $(3),$(3)-)$(2).txt)
  40. ifdef CONFIG_AUTOREMOVE
  41. rebuild_check = \
  42. @-$$(NO_TRACE_MAKE) $(subdir_make_opts) check-depends >/dev/null 2>/dev/null; \
  43. $(if $(BUILD_LOG),mkdir -p $(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4));) \
  44. $$(NO_TRACE_MAKE) $(if $(BUILD_LOG),-d) -q $(subdir_make_opts) .$(if $(3),$(3)-)$(2) \
  45. > $(if $(BUILD_LOG),$(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4))/check-$(if $(3),$(3)-)$(2).txt,/dev/null) 2>&1 || \
  46. $$(SUBMAKE) $(subdir_make_opts) clean-build >/dev/null 2>/dev/null
  47. endif
  48. # Parameters: <subdir>
  49. define subdir
  50. $(call warn,$(1),d,D $(1))
  51. $(foreach bd,$($(1)/builddirs),
  52. $(call warn,$(1),d,BD $(1)/$(bd))
  53. $(foreach target,$(SUBTARGETS) $($(1)/subtargets),
  54. $(foreach btype,$(buildtypes-$(bd)),
  55. $(call warn_eval,$(1)/$(bd),t,T,$(1)/$(bd)/$(btype)/$(target): $(if $(NO_DEPS)$(QUILT),,$($(1)/$(bd)/$(btype)/$(target)) $(call $(1)//$(btype)/$(target),$(1)/$(bd)/$(btype))))
  56. $(call log_make,$(1)/$(bd),$(target),$(btype),$(filter-out __default,$(variant)),$($(1)/$(bd)/variants)) \
  57. || $(call ERROR,$(2), ERROR: $(1)/$(bd) [$(btype)] failed to build.,$(findstring $(bd),$($(1)/builddirs-ignore-$(btype)-$(target))))
  58. $(if $(call diralias,$(bd)),$(call warn_eval,$(1)/$(bd),l,T,$(1)/$(call diralias,$(bd))/$(btype)/$(target): $(1)/$(bd)/$(btype)/$(target)))
  59. )
  60. $(call warn_eval,$(1)/$(bd),t,T,$(1)/$(bd)/$(target): $(if $(NO_DEPS)$(QUILT),,$($(1)/$(bd)/$(target)) $(call $(1)//$(target),$(1)/$(bd))))
  61. $(foreach variant,$(filter-out *,$(if $(BUILD_VARIANT),$(BUILD_VARIANT),$(if $(strip $($(1)/$(bd)/variants)),$($(1)/$(bd)/variants),$(if $($(1)/$(bd)/default-variant),$($(1)/$(bd)/default-variant),__default)))),
  62. $(if $(BUILD_LOG),@mkdir -p $(BUILD_LOG_DIR)/$(1)/$(bd)/$(filter-out __default,$(variant)))
  63. $(if $($(1)/autoremove),$(call rebuild_check,$(1)/$(bd),$(target),,$(filter-out __default,$(variant)),$($(1)/$(bd)/variants)))
  64. $(call log_make,$(1)/$(bd),$(target),,$(filter-out __default,$(variant)),$($(1)/$(bd)/variants)) \
  65. || $(call ERROR,$(1), ERROR: $(1)/$(bd) failed to build$(if $(filter-out __default,$(variant)), (build variant: $(variant))).,$(findstring $(bd),$($(1)/builddirs-ignore-$(target))))
  66. )
  67. $(if $(PREREQ_ONLY)$(DUMP_TARGET_DB),,
  68. # aliases
  69. $(if $(call diralias,$(bd)),$(call warn_eval,$(1)/$(bd),l,T,$(1)/$(call diralias,$(bd))/$(target): $(1)/$(bd)/$(target)))
  70. )
  71. )
  72. )
  73. $(foreach target,$(SUBTARGETS) $($(1)/subtargets),$(call subtarget,$(1),$(target)))
  74. endef
  75. ifndef DUMP_TARGET_DB
  76. # Parameters: <subdir> <name> <target> <depends> <config options> <stampfile location>
  77. define stampfile
  78. $(1)/stamp-$(3):=$(if $(6),$(6),$(STAGING_DIR))/stamp/.$(2)_$(3)$(5)
  79. $$($(1)/stamp-$(3)): $(TMP_DIR)/.build $(4)
  80. @+$(SCRIPT_DIR)/timestamp.pl -n $$($(1)/stamp-$(3)) $(1) $(4) || \
  81. $(MAKE) $(if $(QUIET),--no-print-directory) $$($(1)/flags-$(3)) $(1)/$(3)
  82. @mkdir -p $$$$(dirname $$($(1)/stamp-$(3)))
  83. @touch $$($(1)/stamp-$(3))
  84. $$(if $(call debug,$(1),v),,.SILENT: $$($(1)/stamp-$(3)))
  85. .PRECIOUS: $$($(1)/stamp-$(3)) # work around a make bug
  86. $(1)//clean:=$(1)/stamp-$(3)/clean
  87. $(1)/stamp-$(3)/clean: FORCE
  88. @rm -f $$($(1)/stamp-$(3))
  89. endef
  90. endif