1. 程式人生 > >安卓makefile全域性變數建立流程分析

安卓makefile全域性變數建立流程分析

  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. # Clears a list of variables using ":=".
  18. #
  19. # E.g.,
  20. # $(call clear-var-list,A B C)
  21. # would be the same as:
  22. # A :=
  23. # B :=
  24. # C :=
  25. #
  26. # $(1): list of variable names to clear
  27. #
  28. define clear-var-list
  29. $(foreach v,$(1),$(eval $(v):=))
  30. endef
  31. #
  32. # Copies a list of variables into another list of variables.
  33. # The target list is the same as the source list, but has
  34. # a dotted prefix affixed to it.
  35. #
  36. # E.g.,
  37. # $(call copy-var-list, PREFIX, A B)
  38. # would be the same as:
  39. # PREFIX.A := $(A)
  40. # PREFIX.B := $(B)
  41. #
  42. # $(1): destination prefix
  43. # $(2): list of variable names to copy
  44. #
  45. define copy-var-list
  46. $(foreach v,$(2),$(eval $(strip $(1)).$(v):=$($(v))))
  47. endef
  48. #
  49. # Moves a list of variables into another list of variables.
  50. # The variable names differ by a prefix. After moving, the
  51. # source variable is cleared.
  52. #
  53. # NOTE: Spaces are not allowed around the prefixes.
  54. #
  55. # E.g.,
  56. # $(call move-var-list,SRC,DST,A B)
  57. # would be the same as:
  58. # DST.A := $(SRC.A)
  59. # SRC.A :=
  60. # DST.B := $(SRC.B)
  61. # SRC.B :=
  62. #
  63. # $(1): source prefix
  64. # $(2): destination prefix
  65. # $(3): list of variable names to move
  66. #
  67. define move-var-list
  68. $(foreach v,$(3), \
  69. $(eval $(2).$(v) := $($(1).$(v))) \
  70. $(eval $(1).$(v) :=) \
  71. )
  72. endef
  73. #
  74. # $(1): haystack
  75. # $(2): needle
  76. #
  77. # Guarantees that needle appears at most once in haystack,
  78. # without changing the order of other elements in haystack.
  79. # If needle appears multiple times, only the first occurrance
  80. # will survive.
  81. #
  82. # How it works:
  83. #
  84. # - Stick everything in haystack into a single word,
  85. # with "|||" separating the words.
  86. # - Replace occurrances of "|||$(needle)|||" with "||| |||",
  87. # breaking haystack back into multiple words, with spaces
  88. # where needle appeared.
  89. # - Add needle between the first and second words of haystack.
  90. # - Replace "|||" with spaces, breaking haystack back into
  91. # individual words.
  92. #
  93. empty :=
  94. space := $(empty) $(empty)
  95. define uniq-word
  96. $(strip \
  97. $(if $(filter $(2),$(1)), \
  98. $(eval h := |||$(subst $(space),|||,$(strip $(1)))|||) \
  99. $(eval h := $(subst |||$(strip $(2))|||,|||$(space)|||,$(h))) \
  100. $(eval h := $(word 1,$(h)) $(2) $(wordlist 2,9999,$(h))) \
  101. $(subst |||,$(space),$(h)) \
  102. , \
  103. $(1) \
  104. ))
  105. endef
  106. INHERIT_TAG := @inherit:
  107. #
  108. # Walks through the list of variables, each qualified by the prefix,
  109. # and finds instances of words beginning with INHERIT_TAG. Scrape
  110. # off INHERIT_TAG from each matching word, and return the sorted,
  111. # unique set of those words.
  112. #
  113. # E.g., given
  114. # PREFIX.A := A $(INHERIT_TAG)aaa B C
  115. # PREFIX.B := B $(INHERIT_TAG)aaa C $(INHERIT_TAG)bbb D E
  116. # Then
  117. # $(call get-inherited-nodes,PREFIX,A B)
  118. # returns
  119. # aaa bbb
  120. #
  121. # $(1): variable prefix
  122. # $(2): list of variables to check
  123. #
  124. define get-inherited-nodes
  125. $(sort \
  126. $(subst $(INHERIT_TAG),, \
  127. $(filter $(INHERIT_TAG)%, \
  128. $(foreach v,$(2),$($(1).$(v))) \
  129. )))
  130. endef
  131. #
  132. # for each variable ( (prefix + name) * vars ):
  133. # get list of inherited words; if not empty:
  134. # for each inherit:
  135. # replace the first occurrence with (prefix + inherited + var)
  136. # clear the source var so we can't inherit the value twice
  137. #
  138. # $(1): context prefix
  139. # $(2): name of this node
  140. # $(3): list of variable names
  141. #
  142. define _expand-inherited-values
  143. $(foreach v,$(3), \
  144. $(eval ### "Shorthand for the name of the target variable") \
  145. $(eval _eiv_tv := $(1).$(2).$(v)) \
  146. $(eval ### "Get the list of nodes that this variable inherits") \
  147. $(eval _eiv_i := \
  148. $(sort \
  149. $(patsubst $(INHERIT_TAG)%,%, \
  150. $(filter $(INHERIT_TAG)%, $($(_eiv_tv)) \
  151. )))) \
  152. $(foreach i,$(_eiv_i), \
  153. $(eval ### "Make sure that this inherit appears only once") \
  154. $(eval $(_eiv_tv) := \
  155. $(call uniq-word,$($(_eiv_tv)),$(INHERIT_TAG)$(i))) \
  156. $(eval ### "Expand the inherit tag") \
  157. $(eval $(_eiv_tv) := \
  158. $(strip \
  159. $(patsubst $(INHERIT_TAG)$(i),$($(1).$(i).$(v)), \
  160. $($(_eiv_tv))))) \
  161. $(eval ### "Clear the child so DAGs don't create duplicate entries" ) \
  162. $(eval $(1).$(i).$(v) :=) \
  163. $(eval ### "If we just inherited ourselves, it's a cycle.") \
  164. $(if $(filter $(INHERIT_TAG)$(2),$($(_eiv_tv))), \
  165. $(warning Cycle detected between "$(2)" and "$(i)" for context "$(1)") \
  166. $(error import of "$(2)" failed) \
  167. ) \
  168. ) \
  169. ) \
  170. $(eval _eiv_tv :=) \
  171. $(eval _eiv_i :=)
  172. endef
  173. #
  174. # $(1): context prefix
  175. # $(2): makefile representing this node
  176. # $(3): list of node variable names
  177. #
  178. # _include_stack contains the list of included files, with the most recent files first.
  179. define _import-node
  180. $(eval _include_stack := $(2) $$(_include_stack))
  181. $(call clear-var-list, $(3))
  182. $(eval LOCAL_PATH := $(patsubst %/,%,$(dir $(2))))
  183. $(eval MAKEFILE_LIST :=)
  184. $(eval include $(2))
  185. $(eval _included := $(filter-out $(2),$(MAKEFILE_LIST)))
  186. $(eval MAKEFILE_LIST :=)
  187. $(eval LOCAL_PATH :=)
  188. $(call copy-var-list, $(1).$(2), $(3))
  189. $(call clear-var-list, $(3))
  190. $(eval $(1).$(2).inherited := \
  191. $(call get-inherited-nodes,$(1).$(2),$(3)))
  192. $(call _import-nodes-inner,$(1),$($(1).$(2).inherited),$(3))
  193. $(call _expand-inherited-values,$(1),$(2),$(3))
  194. $(eval $(1).$(2).inherited :=)
  195. $(eval _include_stack := $(wordlist 2,9999,$$(_include_stack)))
  196. endef
  197. #
  198. # This will generate a warning for _included above
  199. # $(if $(_included), \
  200. # $(eval $(warning product spec file: $(2)))\
  201. # $(foreach _inc,$(_included),$(eval $(warning $(space)$(space)$(space)includes: $(_inc)))),)
  202. #
  203. #
  204. # $(1): context prefix
  205. # $(2): list of makefiles representing nodes to import
  206. # $(3): list of node variable names
  207. #
  208. #TODO: Make the "does not exist" message more helpful;
  209. # should print out the name of the file trying to include it.
  210. define _import-nodes-inner
  211. $(foreach _in,$(2), \
  212. $(if $(wildcard $(_in)), \
  213. $(if $($(1).$(_in).seen), \
  214. $(eval ### "skipping already-imported $(_in)") \
  215. , \
  216. $(eval $(1).$(_in).seen := true) \
  217. $(call _import-node,$(1),$(strip $(_in)),$(3)) \
  218. ) \
  219. , \
  220. $(error $(1): "$(_in)" does not exist) \
  221. ) \
  222. )
  223. endef
  224. #
  225. # $(1): output list variable name, like "PRODUCTS" or "DEVICES"
  226. # $(2): list of makefiles representing nodes to import
  227. # $(3): list of node variable names
  228. #
  229. define import-nodes
  230. $(if \
  231. $(foreach _in,$(2), \
  232. $(eval _node_import_context := _nic.$(1).[[$(_in)]]) \
  233. $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \
  234. should be empty here: $(_include_stack))),) \
  235. $(eval _include_stack := ) \
  236. $(call _import-nodes-inner,$(_node_import_context),$(_in),$(3)) \
  237. $(call move-var-list,$(_node_import_context).$(_in),$(1).$(_in),$(3)) \
  238. $(eval _node_import_context :=) \
  239. $(eval $(1) := $($(1)) $(_in)) \
  240. $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \
  241. should be empty here: $(_include_stack))),) \
  242. ) \
  243. ,)
  244. endef