1# 2# Copyright (C) 2007 The Android Open Source Project 3# 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 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 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. 15# 16 17# 18# Clears a list of variables using ":=". 19# 20# E.g., 21# $(call clear-var-list,A B C) 22# would be the same as: 23# A := 24# B := 25# C := 26# 27# $(1): list of variable names to clear 28# 29define clear-var-list 30$(foreach v,$(1),$(eval $(v):=)) 31endef 32 33# 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. 37# 38# E.g., 39# $(call copy-var-list, PREFIX, A B) 40# would be the same as: 41# PREFIX.A := $(A) 42# PREFIX.B := $(B) 43# 44# $(1): destination prefix 45# $(2): list of variable names to copy 46# 47define copy-var-list 48$(foreach v,$(2),$(eval $(strip $(1)).$(v):=$($(v)))) 49endef 50 51# 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. 55# 56# NOTE: Spaces are not allowed around the prefixes. 57# 58# E.g., 59# $(call move-var-list,SRC,DST,A B) 60# would be the same as: 61# DST.A := $(SRC.A) 62# SRC.A := 63# DST.B := $(SRC.B) 64# SRC.B := 65# 66# $(1): source prefix 67# $(2): destination prefix 68# $(3): list of variable names to move 69# 70define move-var-list 71$(foreach v,$(3), \ 72 $(eval $(2).$(v) := $($(1).$(v))) \ 73 $(eval $(1).$(v) :=) \ 74 ) 75endef 76 77# 78# $(1): haystack 79# $(2): needle 80# 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 84# will survive. 85# 86define uniq-word 87$(strip \ 88 $(if $(filter-out 0 1,$(words $(filter $(2),$(1)))), \ 89 $(eval _uniq_word_seen :=) \ 90 $(foreach w,$(1), \ 91 $(if $(filter $(2),$(w)), \ 92 $(if $(_uniq_word_seen),, \ 93 $(w) \ 94 $(eval _uniq_word_seen := true)), \ 95 $(w))), \ 96 $(1))) 97endef 98 99INHERIT_TAG := @inherit: 100 101# 102# Walks through the list of variables, each qualified by the prefix, 103# and finds instances of words beginning with INHERIT_TAG. Scrape 104# off INHERIT_TAG from each matching word, and return the sorted, 105# unique set of those words. 106# 107# E.g., given 108# PREFIX.A := A $(INHERIT_TAG)aaa B C 109# PREFIX.B := B $(INHERIT_TAG)aaa C $(INHERIT_TAG)bbb D E 110# Then 111# $(call get-inherited-nodes,PREFIX,A B) 112# returns 113# aaa bbb 114# 115# $(1): variable prefix 116# $(2): list of variables to check 117# 118define get-inherited-nodes 119$(sort \ 120 $(subst $(INHERIT_TAG),, \ 121 $(filter $(INHERIT_TAG)%, \ 122 $(foreach v,$(2),$($(1).$(v))) \ 123 ))) 124endef 125 126# 127# for each variable ( (prefix + name) * vars ): 128# get list of inherited words; if not empty: 129# for each inherit: 130# replace the first occurrence with (prefix + inherited + var) 131# clear the source var so we can't inherit the value twice 132# 133# $(1): context prefix 134# $(2): name of this node 135# $(3): list of node variable names 136# $(4): list of single value variable names (subset of $(3)) 137# 138define _expand-inherited-values 139 $(foreach v,$(3), \ 140 $(eval ### "Shorthand for the name of the target variable") \ 141 $(eval _eiv_tv := $(1).$(2).$(v)) \ 142 $(eval ### "Get the list of nodes that this variable inherits") \ 143 $(eval _eiv_i := \ 144 $(sort \ 145 $(patsubst $(INHERIT_TAG)%,%, \ 146 $(filter $(INHERIT_TAG)%, $($(_eiv_tv)) \ 147 )))) \ 148 $(eval ### "Whether this variable should only take a single value") \ 149 $(eval _eiv_sv := $(filter $(v),$(4))) \ 150 $(foreach i,$(_eiv_i), \ 151 $(eval ### "Make sure that this inherit appears only once") \ 152 $(eval $(_eiv_tv) := \ 153 $(call uniq-word,$($(_eiv_tv)),$(INHERIT_TAG)$(i))) \ 154 $(eval ### "The expanded value, empty if we want a single value and have one") \ 155 $(eval _eiv_ev := \ 156 $(if $(and $(_eiv_sv),$(filter-out $(INHERIT_TAG)%,$($(_eiv_tv)))),,\ 157 $($(1).$(i).$(v)) \ 158 ) \ 159 ) \ 160 $(eval ### "Expand the inherit tag") \ 161 $(eval $(_eiv_tv) := \ 162 $(strip $(patsubst $(INHERIT_TAG)$(i),$(_eiv_ev),$($(_eiv_tv))))) \ 163 $(eval ### "Clear the child so DAGs don't create duplicate entries" ) \ 164 $(eval $(1).$(i).$(v) :=) \ 165 $(eval ### "If we just inherited ourselves, it's a cycle.") \ 166 $(if $(filter $(INHERIT_TAG)$(2),$($(_eiv_tv))), \ 167 $(warning Cycle detected between "$(2)" and "$(i)" for context "$(1)") \ 168 $(error import of "$(2)" failed) \ 169 ) \ 170 ) \ 171 ) \ 172 $(eval _eiv_tv :=) \ 173 $(eval _eiv_i :=) 174endef 175 176# 177# $(1): context prefix 178# $(2): makefile representing this node 179# $(3): list of node variable names 180# $(4): list of single value variable names (subset of $(3)) 181# 182# _include_stack contains the list of included files, with the most recent files first. 183define _import-node 184 $(eval _include_stack := $(2) $$(_include_stack)) 185 $(call clear-var-list, $(3)) 186 $(eval LOCAL_PATH := $(patsubst %/,%,$(dir $(2)))) 187 $(eval MAKEFILE_LIST :=) 188 $(call dump-import-start,$(_include_stack)) 189 $(call dump-config-vals,$(2),before) 190 $(eval include $(2)) 191 $(call dump-import-done,$(_include_stack)) 192 $(call dump-config-vals,$(2),after) 193 $(eval _included := $(filter-out $(2),$(MAKEFILE_LIST))) 194 $(eval MAKEFILE_LIST :=) 195 $(eval LOCAL_PATH :=) 196 $(call copy-var-list, $(1).$(2), $(3)) 197 $(call clear-var-list, $(3)) 198 199 $(eval $(1).$(2).inherited := \ 200 $(call get-inherited-nodes,$(1).$(2),$(3))) 201 $(call _import-nodes-inner,$(1),$($(1).$(2).inherited),$(3),$(4)) 202 203 $(call _expand-inherited-values,$(1),$(2),$(3),$(4)) 204 205 $(eval $(1).$(2).inherited :=) 206 $(eval _include_stack := $(wordlist 2,9999,$(_include_stack))) 207endef 208 209# 210# This will generate a warning for _included above 211# $(if $(_included), \ 212# $(eval $(warning product spec file: $(2)))\ 213# $(foreach _inc,$(_included),$(eval $(warning $(space)$(space)$(space)includes: $(_inc)))),) 214# 215 216# 217# $(1): context prefix 218# $(2): list of makefiles representing nodes to import 219# $(3): list of node variable names 220# $(4): list of single value variable names (subset of $(3)) 221# 222#TODO: Make the "does not exist" message more helpful; 223# should print out the name of the file trying to include it. 224define _import-nodes-inner 225 $(foreach _in,$(2), \ 226 $(if $(wildcard $(_in)), \ 227 $(if $($(1).$(_in).seen), \ 228 $(eval ### "skipping already-imported $(_in)") \ 229 , \ 230 $(eval $(1).$(_in).seen := true) \ 231 $(call _import-node,$(1),$(strip $(_in)),$(3),$(4)) \ 232 ) \ 233 , \ 234 $(error $(1): "$(_in)" does not exist) \ 235 ) \ 236 ) 237endef 238 239# 240# $(1): output list variable name, like "PRODUCTS" or "DEVICES" 241# $(2): list of makefiles representing nodes to import 242# $(3): list of node variable names 243# $(4): list with subset of variable names that take only a single value, instead 244# of the default list semantics 245# 246define import-nodes 247$(call dump-phase-start,$(1),$(2),$(3),$(4),build/make/core/node_fns.mk) \ 248$(if \ 249 $(foreach _in,$(2), \ 250 $(eval _node_import_context := _nic.$(1).[[$(_in)]]) \ 251 $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \ 252 should be empty here: $(_include_stack))),) \ 253 $(eval _include_stack := ) \ 254 $(call _import-nodes-inner,$(_node_import_context),$(_in),$(3),$(4)) \ 255 $(call move-var-list,$(_node_import_context).$(_in),$(1).$(_in),$(3)) \ 256 $(eval _node_import_context :=) \ 257 $(eval $(1) := $($(1)) $(_in)) \ 258 $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \ 259 should be empty here: $(_include_stack))),) \ 260 ) \ 261,) \ 262$(call dump-phase-end,build/make/core/node_fns.mk) 263endef 264