1#
2# Copyright (C) 2008 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# Generic functions
19# TODO: Move these to definitions.make once we're able to include
20# definitions.make before config.make.
21
22###########################################################
23## Return non-empty if $(1) is a C identifier; i.e., if it
24## matches /^[a-zA-Z_][a-zA-Z0-9_]*$/.  We do this by first
25## making sure that it isn't empty and doesn't start with
26## a digit, then by removing each valid character.  If the
27## final result is empty, then it was a valid C identifier.
28##
29## $(1): word to check
30###########################################################
31
32_ici_digits := 0 1 2 3 4 5 6 7 8 9
33_ici_alphaunderscore := \
34    a b c d e f g h i j k l m n o p q r s t u v w x y z \
35    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _
36define is-c-identifier
37$(strip \
38  $(if $(1), \
39    $(if $(filter $(addsuffix %,$(_ici_digits)),$(1)), \
40     , \
41      $(eval w := $(1)) \
42      $(foreach c,$(_ici_digits) $(_ici_alphaunderscore), \
43        $(eval w := $(subst $(c),,$(w))) \
44       ) \
45      $(if $(w),,TRUE) \
46      $(eval w :=) \
47     ) \
48   ) \
49 )
50endef
51
52# TODO: push this into the combo files; unfortunately, we don't even
53# know HOST_OS at this point.
54trysed := $(shell echo a | sed -E -e 's/a/b/' 2>/dev/null)
55ifeq ($(trysed),b)
56  SED_EXTENDED := sed -E
57else
58  trysed := $(shell echo c | sed -r -e 's/c/d/' 2>/dev/null)
59  ifeq ($(trysed),d)
60    SED_EXTENDED := sed -r
61  else
62    $(error Unknown sed version)
63  endif
64endif
65
66###########################################################
67## List all of the files in a subdirectory in a format
68## suitable for PRODUCT_COPY_FILES and
69## PRODUCT_SDK_ADDON_COPY_FILES
70##
71## $(1): Glob to match file name
72## $(2): Source directory
73## $(3): Target base directory
74###########################################################
75
76define find-copy-subdir-files
77$(sort $(shell find $(2) -name "$(1)" -type f | $(SED_EXTENDED) "s:($(2)/?(.*)):\\1\\:$(3)/\\2:" | sed "s://:/:g"))
78endef
79
80# ---------------------------------------------------------------
81# Check for obsolete PRODUCT- and APP- goals
82ifeq ($(CALLED_FROM_SETUP),true)
83product_goals := $(strip $(filter PRODUCT-%,$(MAKECMDGOALS)))
84ifdef product_goals
85  $(error The PRODUCT-* goal is no longer supported. Use `TARGET_PRODUCT=<product> m droid` instead)
86endif
87unbundled_goals := $(strip $(filter APP-%,$(MAKECMDGOALS)))
88ifdef unbundled_goals
89  $(error The APP-* goal is no longer supported. Use `TARGET_BUILD_APPS="<app>" m droid` instead)
90endif # unbundled_goals
91endif
92
93# Default to building dalvikvm on hosts that support it...
94ifeq ($(HOST_OS),linux)
95# ... or if the if the option is already set
96ifeq ($(WITH_HOST_DALVIK),)
97  WITH_HOST_DALVIK := true
98endif
99endif
100
101# ---------------------------------------------------------------
102# Include the product definitions.
103# We need to do this to translate TARGET_PRODUCT into its
104# underlying TARGET_DEVICE before we start defining any rules.
105#
106include $(BUILD_SYSTEM)/node_fns.mk
107include $(BUILD_SYSTEM)/product.mk
108include $(BUILD_SYSTEM)/device.mk
109
110# Read in all of the product definitions specified by the AndroidProducts.mk
111# files in the tree.
112all_product_configs := $(get-all-product-makefiles)
113
114all_named_products :=
115
116# Find the product config makefile for the current product.
117# all_product_configs consists items like:
118# <product_name>:<path_to_the_product_makefile>
119# or just <path_to_the_product_makefile> in case the product name is the
120# same as the base filename of the product config makefile.
121current_product_makefile :=
122all_product_makefiles :=
123$(foreach f, $(all_product_configs),\
124    $(eval _cpm_words := $(call _decode-product-name,$(f)))\
125    $(eval _cpm_word1 := $(word 1,$(_cpm_words)))\
126    $(eval _cpm_word2 := $(word 2,$(_cpm_words)))\
127    $(eval all_product_makefiles += $(_cpm_word2))\
128    $(eval all_named_products += $(_cpm_word1))\
129    $(if $(filter $(TARGET_PRODUCT),$(_cpm_word1)),\
130        $(eval current_product_makefile += $(_cpm_word2)),))
131_cpm_words :=
132_cpm_word1 :=
133_cpm_word2 :=
134current_product_makefile := $(strip $(current_product_makefile))
135all_product_makefiles := $(strip $(all_product_makefiles))
136
137load_all_product_makefiles :=
138ifneq (,$(filter product-graph, $(MAKECMDGOALS)))
139ifeq ($(ANDROID_PRODUCT_GRAPH),--all)
140load_all_product_makefiles := true
141endif
142endif
143ifneq (,$(filter dump-products,$(MAKECMDGOALS)))
144ifeq ($(ANDROID_DUMP_PRODUCTS),all)
145load_all_product_makefiles := true
146endif
147endif
148
149ifeq ($(load_all_product_makefiles),true)
150# Import all product makefiles.
151$(call import-products, $(all_product_makefiles))
152else
153# Import just the current product.
154ifndef current_product_makefile
155$(error Can not locate config makefile for product "$(TARGET_PRODUCT)")
156endif
157ifneq (1,$(words $(current_product_makefile)))
158$(error Product "$(TARGET_PRODUCT)" ambiguous: matches $(current_product_makefile))
159endif
160$(call import-products, $(current_product_makefile))
161endif  # Import all or just the current product makefile
162
163# Import all the products that have made artifact path requirements, so that we can verify
164# the artifacts they produce.
165$(foreach makefile,$(ARTIFACT_PATH_REQUIREMENT_PRODUCTS),\
166  $(if $(filter-out $(makefile),$(PRODUCTS)),$(eval $(call import-products,$(makefile))))\
167)
168
169# Quick check
170$(check-all-products)
171
172ifneq ($(filter dump-products, $(MAKECMDGOALS)),)
173$(dump-products)
174endif
175
176# Convert a short name like "sooner" into the path to the product
177# file defining that product.
178#
179INTERNAL_PRODUCT := $(call resolve-short-product-name, $(TARGET_PRODUCT))
180ifneq ($(current_product_makefile),$(INTERNAL_PRODUCT))
181$(error PRODUCT_NAME inconsistent in $(current_product_makefile) and $(INTERNAL_PRODUCT))
182endif
183current_product_makefile :=
184all_product_makefiles :=
185all_product_configs :=
186
187# Jacoco agent JARS to be built and installed, if any.
188ifeq ($(EMMA_INSTRUMENT),true)
189  ifneq ($(EMMA_INSTRUMENT_STATIC),true)
190    # For instrumented build, if Jacoco is not being included statically
191    # in instrumented packages then include Jacoco classes into the
192    # bootclasspath.
193    $(foreach product,$(PRODUCTS),\
194      $(eval PRODUCTS.$(product).PRODUCT_PACKAGES += jacocoagent)\
195      $(eval PRODUCTS.$(product).PRODUCT_BOOT_JARS += jacocoagent))
196  endif # EMMA_INSTRUMENT_STATIC
197endif # EMMA_INSTRUMENT
198
199############################################################################
200# Strip and assign the PRODUCT_ variables.
201$(call strip-product-vars)
202
203#############################################################################
204# Quick check and assign default values
205
206TARGET_DEVICE := $(PRODUCT_DEVICE)
207
208# TODO: also keep track of things like "port", "land" in product files.
209
210# Figure out which resoure configuration options to use for this
211# product.
212# If CUSTOM_LOCALES contains any locales not already included
213# in PRODUCT_LOCALES, add them to PRODUCT_LOCALES.
214extra_locales := $(filter-out $(PRODUCT_LOCALES),$(CUSTOM_LOCALES))
215ifneq (,$(extra_locales))
216  ifneq ($(CALLED_FROM_SETUP),true)
217    # Don't spam stdout, because envsetup.sh may be scraping values from it.
218    $(info Adding CUSTOM_LOCALES [$(extra_locales)] to PRODUCT_LOCALES [$(PRODUCT_LOCALES)])
219  endif
220  PRODUCT_LOCALES += $(extra_locales)
221  extra_locales :=
222endif
223
224# Add PRODUCT_LOCALES to PRODUCT_AAPT_CONFIG
225PRODUCT_AAPT_CONFIG := $(PRODUCT_LOCALES) $(PRODUCT_AAPT_CONFIG)
226
227# Keep a copy of the space-separated config
228PRODUCT_AAPT_CONFIG_SP := $(PRODUCT_AAPT_CONFIG)
229PRODUCT_AAPT_CONFIG := $(subst $(space),$(comma),$(PRODUCT_AAPT_CONFIG))
230
231# Extra boot jars must be appended at the end after common boot jars.
232PRODUCT_BOOT_JARS += $(PRODUCT_BOOT_JARS_EXTRA)
233
234# Add 'platform:' prefix to unqualified boot jars
235PRODUCT_BOOT_JARS := $(foreach pair,$(PRODUCT_BOOT_JARS), \
236  $(if $(findstring :,$(pair)),,platform:)$(pair))
237
238# The extra system server jars must be appended at the end after common system server jars.
239PRODUCT_SYSTEM_SERVER_JARS += $(PRODUCT_SYSTEM_SERVER_JARS_EXTRA)
240
241ifndef PRODUCT_SYSTEM_NAME
242  PRODUCT_SYSTEM_NAME := $(PRODUCT_NAME)
243endif
244ifndef PRODUCT_SYSTEM_DEVICE
245  PRODUCT_SYSTEM_DEVICE := $(PRODUCT_DEVICE)
246endif
247ifndef PRODUCT_SYSTEM_BRAND
248  PRODUCT_SYSTEM_BRAND := $(PRODUCT_BRAND)
249endif
250ifndef PRODUCT_MODEL
251  PRODUCT_MODEL := $(PRODUCT_NAME)
252endif
253ifndef PRODUCT_SYSTEM_MODEL
254  PRODUCT_SYSTEM_MODEL := $(PRODUCT_MODEL)
255endif
256
257ifndef PRODUCT_MANUFACTURER
258  PRODUCT_MANUFACTURER := unknown
259endif
260ifndef PRODUCT_SYSTEM_MANUFACTURER
261  PRODUCT_SYSTEM_MANUFACTURER := $(PRODUCT_MANUFACTURER)
262endif
263
264ifndef PRODUCT_CHARACTERISTICS
265  TARGET_AAPT_CHARACTERISTICS := default
266else
267  TARGET_AAPT_CHARACTERISTICS := $(PRODUCT_CHARACTERISTICS)
268endif
269
270ifdef PRODUCT_DEFAULT_DEV_CERTIFICATE
271  ifneq (1,$(words $(PRODUCT_DEFAULT_DEV_CERTIFICATE)))
272    $(error PRODUCT_DEFAULT_DEV_CERTIFICATE='$(PRODUCT_DEFAULT_DEV_CERTIFICATE)', \
273      only 1 certificate is allowed.)
274  endif
275endif
276
277$(foreach pair,$(PRODUCT_UPDATABLE_BOOT_JARS), \
278  $(eval jar := $(call word-colon,2,$(pair))) \
279  $(if $(findstring $(jar), $(PRODUCT_BOOT_JARS)), \
280    $(error A jar in PRODUCT_UPDATABLE_BOOT_JARS must not be in PRODUCT_BOOT_JARS, but $(jar) is)))
281
282ENFORCE_SYSTEM_CERTIFICATE := $(PRODUCT_ENFORCE_ARTIFACT_SYSTEM_CERTIFICATE_REQUIREMENT)
283ENFORCE_SYSTEM_CERTIFICATE_ALLOW_LIST := $(PRODUCT_ARTIFACT_SYSTEM_CERTIFICATE_REQUIREMENT_ALLOW_LIST)
284
285PRODUCT_OTA_PUBLIC_KEYS := $(sort $(PRODUCT_OTA_PUBLIC_KEYS))
286PRODUCT_EXTRA_RECOVERY_KEYS := $(sort $(PRODUCT_EXTRA_RECOVERY_KEYS))
287
288# Resolve and setup per-module dex-preopt configs.
289DEXPREOPT_DISABLED_MODULES :=
290# If a module has multiple setups, the first takes precedence.
291_pdpmc_modules :=
292$(foreach c,$(PRODUCT_DEX_PREOPT_MODULE_CONFIGS),\
293  $(eval m := $(firstword $(subst =,$(space),$(c))))\
294  $(if $(filter $(_pdpmc_modules),$(m)),,\
295    $(eval _pdpmc_modules += $(m))\
296    $(eval cf := $(patsubst $(m)=%,%,$(c)))\
297    $(eval cf := $(subst $(_PDPMC_SP_PLACE_HOLDER),$(space),$(cf)))\
298    $(if $(filter disable,$(cf)),\
299      $(eval DEXPREOPT_DISABLED_MODULES += $(m)),\
300      $(eval DEXPREOPT.$(TARGET_PRODUCT).$(m).CONFIG := $(cf)))))
301_pdpmc_modules :=
302
303
304# Resolve and setup per-module sanitizer configs.
305# If a module has multiple setups, the first takes precedence.
306_psmc_modules :=
307$(foreach c,$(PRODUCT_SANITIZER_MODULE_CONFIGS),\
308  $(eval m := $(firstword $(subst =,$(space),$(c))))\
309  $(if $(filter $(_psmc_modules),$(m)),,\
310    $(eval _psmc_modules += $(m))\
311    $(eval cf := $(patsubst $(m)=%,%,$(c)))\
312    $(eval cf := $(subst $(_PSMC_SP_PLACE_HOLDER),$(space),$(cf)))\
313    $(eval SANITIZER.$(TARGET_PRODUCT).$(m).CONFIG := $(cf))))
314_psmc_modules :=
315
316# Reset ADB keys for non-debuggable builds
317ifeq (,$(filter eng userdebug,$(TARGET_BUILD_VARIANT)),)
318  PRODUCT_ADB_KEYS :=
319endif
320ifneq ($(filter-out 0 1,$(words $(PRODUCT_ADB_KEYS))),)
321  $(error Only one file may be in PRODUCT_ADB_KEYS: $(PRODUCT_ADB_KEYS))
322endif
323
324ifndef PRODUCT_USE_DYNAMIC_PARTITIONS
325  PRODUCT_USE_DYNAMIC_PARTITIONS := $(PRODUCT_RETROFIT_DYNAMIC_PARTITIONS)
326endif
327
328# All requirements of PRODUCT_USE_DYNAMIC_PARTITIONS falls back to
329# PRODUCT_USE_DYNAMIC_PARTITIONS if not defined.
330ifndef PRODUCT_USE_DYNAMIC_PARTITION_SIZE
331  PRODUCT_USE_DYNAMIC_PARTITION_SIZE := $(PRODUCT_USE_DYNAMIC_PARTITIONS)
332endif
333
334ifndef PRODUCT_BUILD_SUPER_PARTITION
335  PRODUCT_BUILD_SUPER_PARTITION := $(PRODUCT_USE_DYNAMIC_PARTITIONS)
336endif
337
338ifeq ($(PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS),)
339  ifdef PRODUCT_SHIPPING_API_LEVEL
340    ifeq (true,$(call math_gt_or_eq,$(PRODUCT_SHIPPING_API_LEVEL),29))
341      PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS := true
342    endif
343  endif
344endif
345
346ifdef PRODUCT_SHIPPING_API_LEVEL
347  ifneq (,$(call math_gt_or_eq,29,$(PRODUCT_SHIPPING_API_LEVEL)))
348    PRODUCT_PACKAGES += $(PRODUCT_PACKAGES_SHIPPING_API_LEVEL_29)
349  endif
350endif
351
352# If build command defines OVERRIDE_PRODUCT_EXTRA_VNDK_VERSIONS,
353# override PRODUCT_EXTRA_VNDK_VERSIONS with it.
354ifdef OVERRIDE_PRODUCT_EXTRA_VNDK_VERSIONS
355  PRODUCT_EXTRA_VNDK_VERSIONS := $(OVERRIDE_PRODUCT_EXTRA_VNDK_VERSIONS)
356endif
357
358$(KATI_obsolete_var OVERRIDE_PRODUCT_EXTRA_VNDK_VERSIONS \
359    ,Use PRODUCT_EXTRA_VNDK_VERSIONS instead)
360
361ifdef OVERRIDE_PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE
362  ifeq (false,$(OVERRIDE_PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE))
363    PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE :=
364  else
365    PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE := $(OVERRIDE_PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE)
366  endif
367else ifeq ($(PRODUCT_SHIPPING_API_LEVEL),)
368  # No shipping level defined
369else ifeq ($(call math_gt,$(PRODUCT_SHIPPING_API_LEVEL),29),true)
370  PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE := true
371endif
372
373$(KATI_obsolete_var OVERRIDE_PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE,Use PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE instead)
374
375define product-overrides-config
376$$(foreach rule,$$(PRODUCT_$(1)_OVERRIDES),\
377    $$(if $$(filter 2,$$(words $$(subst :,$$(space),$$(rule)))),,\
378        $$(error Rule "$$(rule)" in PRODUCT_$(1)_OVERRIDE is not <module_name>:<new_value>)))
379endef
380
381$(foreach var, \
382    MANIFEST_PACKAGE_NAME \
383    PACKAGE_NAME \
384    CERTIFICATE, \
385  $(eval $(call product-overrides-config,$(var))))
386
387# Macro to use below. $(1) is the name of the partition
388define product-build-image-config
389ifneq ($$(filter-out true false,$$(PRODUCT_BUILD_$(1)_IMAGE)),)
390    $$(error Invalid PRODUCT_BUILD_$(1)_IMAGE: $$(PRODUCT_BUILD_$(1)_IMAGE) -- true false and empty are supported)
391endif
392endef
393
394# Copy and check the value of each PRODUCT_BUILD_*_IMAGE variable
395$(foreach image, \
396    SYSTEM \
397    SYSTEM_OTHER \
398    VENDOR \
399    PRODUCT \
400    SYSTEM_EXT \
401    ODM \
402    VENDOR_DLKM \
403    ODM_DLKM \
404    CACHE \
405    RAMDISK \
406    USERDATA \
407    BOOT \
408    RECOVERY, \
409  $(eval $(call product-build-image-config,$(image))))
410
411product-build-image-config :=
412
413$(call readonly-product-vars)
414