subdir.mk revision 9edf5cdd69d2d2a74b37ec14e475c5d6367e6eec
1# This file is intended to be included from each subdirectory makefile.
2#
3# Subdirectory makefiles must define:
4#   SubDirs - The subdirectories to traverse.
5#   ObjNames - The objects available in that directory.
6#   Target - The library configuration the objects should go in (Generic or
7#            Optimized)
8#   Dependencies - Any dependences for the object files.
9#
10# Subdirectory makefiles may define:
11#   OnlyArchs - Only build the objects for the listed archs.
12#   OnlyConfigs - Only build the objects for the listed configurations.
13
14ifeq ($(Dir),)
15  $(error "No Dir variable defined.")
16endif
17
18# Expand template for each configuration and architecture.
19#
20# FIXME: This level of logic should be in primary Makefile?
21ifeq ($(OnlyConfigs),)
22  ConfigsToTraverse := $(Configs)
23else
24  ConfigsToTraverse := $(OnlyConfigs)
25endif
26
27ifeq ($(OnlyArchs),)
28  ArchsToTraverse := $(Archs)
29else
30  ArchsToTraverse := $(OnlyArchs)
31endif
32
33# If we are only targetting a single arch, only traverse that.
34ifneq ($(TargetArch),)
35  ArchsToTraverse := $(filter $(TargetArch), $(ArchsToTraverse))
36endif
37
38$(foreach config,$(ConfigsToTraverse), \
39  $(foreach arch,$(ArchsToTraverse), \
40    $(eval $(call CNA_subdir_template,$(config),$(arch),$(Dir)))))
41
42###
43# Include child makefile fragments
44
45# The list of variables which are intended to be overridden in a subdirectory
46# makefile.
47RequiredSubdirVariables := SubDirs ObjNames Target Dependencies
48OptionalSubdirVariables := OnlyArchs OnlyConfigs
49
50# Template: subdir_traverse_template subdir
51define subdir_traverse_template
52$(call Set,Dir,$(1))
53ifneq ($(DEBUGMAKE),)
54  $$(info MAKE: $(Dir): Processing subdirectory)
55endif
56
57# Construct the variable key for this directory.
58$(call Set,DirKey,SubDir.$(subst .,,$(subst /,__,$(1))))
59$(call Append,SubDirKeys,$(DirKey))
60$(call Set,$(DirKey).Dir,$(Dir))
61
62# Reset subdirectory specific variables to sentinel value.
63$$(foreach var,$$(RequiredSubdirVariables) $$(OptionalSubdirVariables),\
64  $$(call Set,$$(var),UNDEFINED))
65
66# Get the subdirectory variables.
67include $(1)/Makefile.mk
68
69ifeq ($(DEBUGMAKE),2)
70$$(foreach var,$(RequiredSubdirVariables) $(OptionalSubdirVariables),\
71  $$(if $$(call strneq,UNDEFINED,$$($$(var))), \
72	$$(info MAKE: $(Dir): $$(var) is defined), \
73	$$(info MAKE: $(Dir): $$(var) is undefined)))
74endif
75
76# Check for undefined required variables, and unset sentinel value from optional
77# variables.
78$$(foreach var,$(RequiredSubdirVariables),\
79  $$(if $$(call strneq,UNDEFINED,$$($$(var))),, \
80	$$(error $(Dir): variable '$$(var)' was not undefined)))
81$$(foreach var,$(OptionalSubdirVariables),\
82  $$(if $$(call strneq,UNDEFINED,$$($$(var))),, \
83	$$(call Set,$$(var),)))
84
85# Collect all subdirectory variables for subsequent use.
86$$(foreach var,$(RequiredSubdirVariables) $(OptionalSubdirVariables),\
87  $$(call Set,$(DirKey).$$(var),$$($$(var))))
88
89# Recurse.
90include make/subdir.mk
91
92# Restore directory variable, for cleanliness.
93$$(call Set,Dir,$(1))
94
95ifneq ($(DEBUGMAKE),)
96  $$(info MAKE: $$(Dir): Done processing subdirectory)
97endif
98endef
99
100# Evaluate this now so we do not have to worry about order of evaluation.
101SubDirsList := $(SubDirs:%=$(Dir)/%)
102ifeq ($(SubDirsList),)
103else
104  ifneq ($(DEBUGMAKE),)
105    $(info MAKE: Descending into subdirs: $(SubDirsList))
106  endif
107
108  $(foreach subdir,$(SubDirsList),\
109	$(eval $(call subdir_traverse_template,$(subdir))))
110endif
111