2 # Copyright (C) 2007 The Android Open Source Project
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 # Clears a list of variables using ":=".
21 # $(call clear-var-list,A B C)
22 # would be the same as:
27 # $(1): list of variable names to clear
30 $(foreach v,$(1),$(eval $(v):=))
34 # Copies a list of variables into another list of variables.
35 # The target list is the same as the source list, but has
36 # a dotted prefix affixed to it.
39 # $(call copy-var-list, PREFIX, A B)
40 # would be the same as:
44 # $(1): destination prefix
45 # $(2): list of variable names to copy
48 $(foreach v,$(2),$(eval $(strip $(1)).$(v):=$($(v))))
52 # Moves a list of variables into another list of variables.
53 # The variable names differ by a prefix. After moving, the
54 # source variable is cleared.
56 # NOTE: Spaces are not allowed around the prefixes.
59 # $(call move-var-list,SRC,DST,A B)
60 # would be the same as:
67 # $(2): destination prefix
68 # $(3): list of variable names to move
72 $(eval $(2).$(v) := $($(1).$(v))) \
73 $(eval $(1).$(v) :=) \
81 # Guarantees that needle appears at most once in haystack,
82 # without changing the order of other elements in haystack.
83 # If needle appears multiple times, only the first occurrance
88 # - Stick everything in haystack into a single word,
89 # with "|||" separating the words.
90 # - Replace occurrances of "|||$(needle)|||" with "||| |||",
91 # breaking haystack back into multiple words, with spaces
92 # where needle appeared.
93 # - Add needle between the first and second words of haystack.
94 # - Replace "|||" with spaces, breaking haystack back into
99 $(if $(filter-out 0 1,$(words $(filter $(2),$(1)))), \
100 $(eval h := |||$(subst $(space),|||,$(strip $(1)))|||) \
101 $(eval h := $(subst |||$(strip $(2))|||,|||$(space)|||,$(h))) \
102 $(eval h := $(word 1,$(h)) $(2) $(wordlist 2,9999,$(h))) \
103 $(subst |||,$(space),$(h)) \
109 INHERIT_TAG := @inherit:
112 # Walks through the list of variables, each qualified by the prefix,
113 # and finds instances of words beginning with INHERIT_TAG. Scrape
114 # off INHERIT_TAG from each matching word, and return the sorted,
115 # unique set of those words.
118 # PREFIX.A := A $(INHERIT_TAG)aaa B C
119 # PREFIX.B := B $(INHERIT_TAG)aaa C $(INHERIT_TAG)bbb D E
121 # $(call get-inherited-nodes,PREFIX,A B)
125 # $(1): variable prefix
126 # $(2): list of variables to check
128 define get-inherited-nodes
130 $(subst $(INHERIT_TAG),, \
131 $(filter $(INHERIT_TAG)%, \
132 $(foreach v,$(2),$($(1).$(v))) \
137 # for each variable ( (prefix + name) * vars ):
138 # get list of inherited words; if not empty:
140 # replace the first occurrence with (prefix + inherited + var)
141 # clear the source var so we can't inherit the value twice
143 # $(1): context prefix
144 # $(2): name of this node
145 # $(3): list of variable names
147 define _expand-inherited-values
149 $(eval ### "Shorthand for the name of the target variable") \
150 $(eval _eiv_tv := $(1).$(2).$(v)) \
151 $(eval ### "Get the list of nodes that this variable inherits") \
154 $(patsubst $(INHERIT_TAG)%,%, \
155 $(filter $(INHERIT_TAG)%, $($(_eiv_tv)) \
157 $(foreach i,$(_eiv_i), \
158 $(eval ### "Make sure that this inherit appears only once") \
159 $(eval $(_eiv_tv) := \
160 $(call uniq-word,$($(_eiv_tv)),$(INHERIT_TAG)$(i))) \
161 $(eval ### "Expand the inherit tag") \
162 $(eval $(_eiv_tv) := \
164 $(patsubst $(INHERIT_TAG)$(i),$($(1).$(i).$(v)), \
166 $(eval ### "Clear the child so DAGs don't create duplicate entries" ) \
167 $(eval $(1).$(i).$(v) :=) \
168 $(eval ### "If we just inherited ourselves, it's a cycle.") \
169 $(if $(filter $(INHERIT_TAG)$(2),$($(_eiv_tv))), \
170 $(warning Cycle detected between "$(2)" and "$(i)" for context "$(1)") \
171 $(error import of "$(2)" failed) \
180 # $(1): context prefix
181 # $(2): makefile representing this node
182 # $(3): list of node variable names
184 # _include_stack contains the list of included files, with the most recent files first.
186 $(eval _include_stack := $(2) $$(_include_stack))
187 $(call clear-var-list, $(3))
188 $(eval LOCAL_PATH := $(patsubst %/,%,$(dir $(2))))
189 $(eval MAKEFILE_LIST :=)
191 $(eval _included := $(filter-out $(2),$(MAKEFILE_LIST)))
192 $(eval MAKEFILE_LIST :=)
193 $(eval LOCAL_PATH :=)
194 $(call copy-var-list, $(1).$(2), $(3))
195 $(call clear-var-list, $(3))
197 $(eval $(1).$(2).inherited := \
198 $(call get-inherited-nodes,$(1).$(2),$(3)))
199 $(call _import-nodes-inner,$(1),$($(1).$(2).inherited),$(3))
201 $(call _expand-inherited-values,$(1),$(2),$(3))
203 $(eval $(1).$(2).inherited :=)
204 $(eval _include_stack := $(wordlist 2,9999,$$(_include_stack)))
208 # This will generate a warning for _included above
209 # $(if $(_included), \
210 # $(eval $(warning product spec file: $(2)))\
211 # $(foreach _inc,$(_included),$(eval $(warning $(space)$(space)$(space)includes: $(_inc)))),)
215 # $(1): context prefix
216 # $(2): list of makefiles representing nodes to import
217 # $(3): list of node variable names
219 #TODO: Make the "does not exist" message more helpful;
220 # should print out the name of the file trying to include it.
221 define _import-nodes-inner
222 $(foreach _in,$(2), \
223 $(if $(wildcard $(_in)), \
224 $(if $($(1).$(_in).seen), \
225 $(eval ### "skipping already-imported $(_in)") \
227 $(eval $(1).$(_in).seen := true) \
228 $(call _import-node,$(1),$(strip $(_in)),$(3)) \
231 $(error $(1): "$(_in)" does not exist) \
237 # $(1): output list variable name, like "PRODUCTS" or "DEVICES"
238 # $(2): list of makefiles representing nodes to import
239 # $(3): list of node variable names
243 $(foreach _in,$(2), \
244 $(eval _node_import_context := _nic.$(1).[[$(_in)]]) \
245 $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \
246 should be empty here: $(_include_stack))),) \
247 $(eval _include_stack := ) \
248 $(call _import-nodes-inner,$(_node_import_context),$(_in),$(3)) \
249 $(call move-var-list,$(_node_import_context).$(_in),$(1).$(_in),$(3)) \
250 $(eval _node_import_context :=) \
251 $(eval $(1) := $($(1)) $(_in)) \
252 $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \
253 should be empty here: $(_include_stack))),) \