initial attempt at cleaning up subdirectory handling. tools/Makefile is now being...
[openwrt.git] / include / subdir.mk
1 # debug flags:
2 #
3 # d: show subdirectory tree
4 # t: show added targets
5 # l: show legacy targets
6
7 ifeq ($(DEBUG),all)
8   build_debug:=dt
9 else
10   build_debug:=$(DEBUG)
11 endif
12
13 define debug
14 $$(findstring $(2),$$(if $$(DEBUG_DIR),$$(if $$(filter $$(DEBUG_DIR)%,$(1)),$(build_debug)),$(build_debug)))
15 endef
16
17 define warn
18 $$(if $(call debug,$(1),$(2)),$$(warning $(3)))
19 endef
20
21 define warn_eval
22 $(call warn,$(1),$(2),$(3)      $(4))
23 $(4)
24 endef
25
26 dep/compile=$(1)/prepare
27 dep/install=$(1)/compile
28 SUBTARGETS:=clean download prepare compile install update refresh prereq
29
30 # Parameters: <subdir>
31 define subdir
32   $(call warn,$(1),d,D $(1))
33   $(foreach bd,$($(1)/builddirs),
34     $(call warn,$(1),d,BD $(1)/$(bd))
35     $(foreach target,$(SUBTARGETS),
36       $(call warn_eval,$(1)/$(bd),t,T,$(1)/$(bd)/$(target): $(call dep/$(target),$(1)/$(bd)) $($(1)/$(bd)/$(target)) $($(1)//$(target)))
37                 @$$(MAKE) -j1 -C $(1)/$(bd) $(target)
38
39       # legacy targets
40       $(call warn_eval,$(1)/$(bd),l,T,$(1)/$(bd)-$(target): $(1)/$(bd)/$(target))
41         )
42   )
43   $(foreach target,$(SUBTARGETS),
44     $(call warn_eval,$(1),t,T,$(1)/$(target): $($(1)/) $(foreach bd,$($(1)/builddirs),$(1)/$(bd)/$(target)))
45   )
46 endef
47
48 # Parameters: <subdir> <name>
49 define stampfile
50   $(1)/stamp:=$(STAGING_DIR)/stampfiles/.$(2)_installed
51   $(call rdep,$(1),$$($(1)/stamp))
52
53   $$($(1)/stamp):
54         @$(MAKE) $(1)/install
55         @mkdir -p $$$$(dirname $$($(1)/stamp))
56         @touch $$($(1)/stamp)
57   .PRECIOUS: $$($(1)/stamp) # work around a make bug
58
59   $(1)//clean:=$(1)/stamp/clean
60   $(1)/stamp/clean: FORCE
61         @rm -f $$($(1)/stamp)
62
63 endef