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# Functions for including AndroidProducts.mk files
19# PRODUCT_MAKEFILES is set up in AndroidProducts.mks.
20# Format of PRODUCT_MAKEFILES:
21# <product_name>:<path_to_the_product_makefile>
22# If the <product_name> is the same as the base file name (without dir
23# and the .mk suffix) of the product makefile, "<product_name>:" can be
24# omitted.
25
26#
27# Returns the list of all AndroidProducts.mk files.
28# $(call ) isn't necessary.
29#
30define _find-android-products-files
31$(file <$(OUT_DIR)/.module_paths/AndroidProducts.mk.list) \
32  $(SRC_TARGET_DIR)/product/AndroidProducts.mk
33endef
34
35#
36# For entries returned by get-product-makefiles, decode an entry to a short
37# product name. These either may be in the form of <name>:path/to/file.mk or
38# path/to/<name>.mk
39# $(1): The entry to decode
40#
41# Returns two words:
42#   <name> <file>
43#
44define _decode-product-name
45$(strip \
46  $(eval _cpm_words := $(subst :,$(space),$(1))) \
47  $(if $(word 2,$(_cpm_words)), \
48    $(wordlist 1,2,$(_cpm_words)), \
49    $(basename $(notdir $(1))) $(1)))
50endef
51
52#
53# Validates the new common lunch choices -- ensures that they're in an
54# appropriate form, and are paired with definitions of their products.
55# $(1): The new list of COMMON_LUNCH_CHOICES
56# $(2): The new list of PRODUCT_MAKEFILES
57#
58define _validate-common-lunch-choices
59$(strip $(foreach choice,$(1),\
60  $(eval _parts := $(subst -,$(space),$(choice))) \
61  $(if $(call math_lt,$(words $(_parts)),2), \
62    $(error $(LOCAL_DIR): $(choice): Invalid lunch choice)) \
63  $(if $(call math_gt_or_eq,$(words $(_parts)),4), \
64    $(error $(LOCAL_DIR): $(choice): Invalid lunch choice)) \
65  $(if $(filter-out eng userdebug user,$(word 2,$(_parts))), \
66    $(error $(LOCAL_DIR): $(choice): Invalid variant: $(word 2,$(_parts)))) \
67  $(if $(filter-out $(foreach p,$(2),$(call _decode-product-name,$(p))),$(word 1,$(_parts))), \
68    $(error $(LOCAL_DIR): $(word 1,$(_parts)): Product not defined in this file)) \
69  ))
70endef
71
72#
73# Returns the sorted concatenation of PRODUCT_MAKEFILES
74# variables set in the given AndroidProducts.mk files.
75# $(1): the list of AndroidProducts.mk files.
76#
77# As a side-effect, COMMON_LUNCH_CHOICES will be set to a
78# union of all of the COMMON_LUNCH_CHOICES definitions within
79# each AndroidProducts.mk file.
80#
81define get-product-makefiles
82$(sort \
83  $(eval _COMMON_LUNCH_CHOICES :=) \
84  $(foreach f,$(1), \
85    $(eval PRODUCT_MAKEFILES :=) \
86    $(eval COMMON_LUNCH_CHOICES :=) \
87    $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \
88    $(eval include $(f)) \
89    $(call _validate-common-lunch-choices,$(COMMON_LUNCH_CHOICES),$(PRODUCT_MAKEFILES)) \
90    $(eval _COMMON_LUNCH_CHOICES += $(COMMON_LUNCH_CHOICES)) \
91    $(PRODUCT_MAKEFILES) \
92   ) \
93  $(eval PRODUCT_MAKEFILES :=) \
94  $(eval LOCAL_DIR :=) \
95  $(eval COMMON_LUNCH_CHOICES := $(sort $(_COMMON_LUNCH_CHOICES))) \
96  $(eval _COMMON_LUNCH_CHOICES :=) \
97 )
98endef
99
100#
101# Returns the sorted concatenation of all PRODUCT_MAKEFILES
102# variables set in all AndroidProducts.mk files.
103# $(call ) isn't necessary.
104#
105define get-all-product-makefiles
106$(call get-product-makefiles,$(_find-android-products-files))
107endef
108
109# Variables that are meant to hold only a single value.
110# - The value set in the current makefile takes precedence over inherited values
111# - If multiple inherited makefiles set the var, the first-inherited value wins
112_product_single_value_vars :=
113
114# Variables that are lists of values.
115_product_list_vars :=
116
117_product_single_value_vars += PRODUCT_NAME
118_product_single_value_vars += PRODUCT_MODEL
119
120# The resoure configuration options to use for this product.
121_product_list_vars += PRODUCT_LOCALES
122_product_list_vars += PRODUCT_AAPT_CONFIG
123_product_list_vars += PRODUCT_AAPT_PREF_CONFIG
124_product_list_vars += PRODUCT_AAPT_PREBUILT_DPI
125_product_list_vars += PRODUCT_HOST_PACKAGES
126_product_list_vars += PRODUCT_PACKAGES
127_product_list_vars += PRODUCT_PACKAGES_DEBUG
128_product_list_vars += PRODUCT_PACKAGES_DEBUG_ASAN
129# Packages included only for eng/userdebug builds, when building with EMMA_INSTRUMENT=true
130_product_list_vars += PRODUCT_PACKAGES_DEBUG_JAVA_COVERAGE
131_product_list_vars += PRODUCT_PACKAGES_ENG
132_product_list_vars += PRODUCT_PACKAGES_TESTS
133
134# The device that this product maps to.
135_product_single_value_vars += PRODUCT_DEVICE
136_product_single_value_vars += PRODUCT_MANUFACTURER
137_product_single_value_vars += PRODUCT_BRAND
138
139# These PRODUCT_SYSTEM_* flags, if defined, are used in place of the
140# corresponding PRODUCT_* flags for the sysprops on /system.
141_product_single_value_vars += \
142    PRODUCT_SYSTEM_NAME \
143    PRODUCT_SYSTEM_MODEL \
144    PRODUCT_SYSTEM_DEVICE \
145    PRODUCT_SYSTEM_BRAND \
146    PRODUCT_SYSTEM_MANUFACTURER \
147
148# A list of property assignments, like "key = value", with zero or more
149# whitespace characters on either side of the '='.
150_product_list_vars += PRODUCT_PROPERTY_OVERRIDES
151
152# A list of property assignments, like "key = value", with zero or more
153# whitespace characters on either side of the '='.
154# used for adding properties to default.prop
155_product_list_vars += PRODUCT_DEFAULT_PROPERTY_OVERRIDES
156
157# A list of property assignments, like "key = value", with zero or more
158# whitespace characters on either side of the '='.
159# used for adding properties to build.prop of product partition
160_product_list_vars += PRODUCT_PRODUCT_PROPERTIES
161
162# A list of property assignments, like "key = value", with zero or more
163# whitespace characters on either side of the '='.
164# used for adding properties to build.prop of system_ext and odm partitions
165_product_list_vars += PRODUCT_SYSTEM_EXT_PROPERTIES
166_product_list_vars += PRODUCT_ODM_PROPERTIES
167
168# The characteristics of the product, which among other things is passed to aapt
169_product_single_value_vars += PRODUCT_CHARACTERISTICS
170
171# A list of words like <source path>:<destination path>[:<owner>].
172# The file at the source path should be copied to the destination path
173# when building  this product.  <destination path> is relative to
174# $(PRODUCT_OUT), so it should look like, e.g., "system/etc/file.xml".
175# The rules for these copy steps are defined in build/make/core/Makefile.
176# The optional :<owner> is used to indicate the owner of a vendor file.
177_product_list_vars += PRODUCT_COPY_FILES
178
179# The OTA key(s) specified by the product config, if any.  The names
180# of these keys are stored in the target-files zip so that post-build
181# signing tools can substitute them for the test key embedded by
182# default.
183_product_list_vars += PRODUCT_OTA_PUBLIC_KEYS
184_product_list_vars += PRODUCT_EXTRA_RECOVERY_KEYS
185
186# Should we use the default resources or add any product specific overlays
187_product_list_vars += PRODUCT_PACKAGE_OVERLAYS
188_product_list_vars += DEVICE_PACKAGE_OVERLAYS
189
190# Resource overlay list which must be excluded from enforcing RRO.
191_product_list_vars += PRODUCT_ENFORCE_RRO_EXCLUDED_OVERLAYS
192
193# Package list to apply enforcing RRO.
194_product_list_vars += PRODUCT_ENFORCE_RRO_TARGETS
195
196# Packages to skip auto-generating RROs for when PRODUCT_ENFORCE_RRO_TARGETS is set to *.
197_product_list_vars += PRODUCT_ENFORCE_RRO_EXEMPTED_TARGETS
198
199_product_list_vars += PRODUCT_SDK_ATREE_FILES
200_product_list_vars += PRODUCT_SDK_ADDON_NAME
201_product_list_vars += PRODUCT_SDK_ADDON_COPY_FILES
202_product_list_vars += PRODUCT_SDK_ADDON_COPY_MODULES
203_product_list_vars += PRODUCT_SDK_ADDON_DOC_MODULES
204_product_list_vars += PRODUCT_SDK_ADDON_SYS_IMG_SOURCE_PROP
205
206# which Soong namespaces to export to Make
207_product_list_vars += PRODUCT_SOONG_NAMESPACES
208
209_product_list_vars += PRODUCT_DEFAULT_WIFI_CHANNELS
210_product_list_vars += PRODUCT_DEFAULT_DEV_CERTIFICATE
211_product_list_vars += PRODUCT_MAINLINE_SEPOLICY_DEV_CERTIFICATES
212_product_list_vars += PRODUCT_RESTRICT_VENDOR_FILES
213
214# The list of product-specific kernel header dirs
215_product_list_vars += PRODUCT_VENDOR_KERNEL_HEADERS
216
217# A list of module names of BOOTCLASSPATH (jar files)
218_product_list_vars += PRODUCT_BOOT_JARS
219
220# A list of extra BOOTCLASSPATH jars (to be appended after common jars).
221# Products that include device-specific makefiles before AOSP makefiles should use this
222# instead of PRODUCT_BOOT_JARS, so that device-specific jars go after common jars.
223_product_list_vars += PRODUCT_BOOT_JARS_EXTRA
224
225_product_list_vars += PRODUCT_SUPPORTS_BOOT_SIGNER
226_product_list_vars += PRODUCT_SUPPORTS_VBOOT
227_product_list_vars += PRODUCT_SUPPORTS_VERITY
228_product_list_vars += PRODUCT_SUPPORTS_VERITY_FEC
229_product_list_vars += PRODUCT_OEM_PROPERTIES
230
231# A list of property assignments, like "key = value", with zero or more
232# whitespace characters on either side of the '='.
233# used for adding properties to default.prop of system partition
234_product_list_vars += PRODUCT_SYSTEM_DEFAULT_PROPERTIES
235
236_product_list_vars += PRODUCT_SYSTEM_PROPERTY_BLACKLIST
237_product_list_vars += PRODUCT_VENDOR_PROPERTY_BLACKLIST
238_product_list_vars += PRODUCT_SYSTEM_SERVER_APPS
239_product_list_vars += PRODUCT_SYSTEM_SERVER_JARS
240# List of system_server jars delivered via apex. Format = <apex name>:<jar name>.
241_product_list_vars += PRODUCT_UPDATABLE_SYSTEM_SERVER_JARS
242
243# Additional system server jars to be appended at the end of the common list.
244# This is necessary to avoid jars reordering due to makefile inheritance order.
245_product_list_vars += PRODUCT_SYSTEM_SERVER_JARS_EXTRA
246
247# All of the apps that we force preopt, this overrides WITH_DEXPREOPT.
248_product_list_vars += PRODUCT_ALWAYS_PREOPT_EXTRACTED_APK
249_product_list_vars += PRODUCT_DEXPREOPT_SPEED_APPS
250_product_list_vars += PRODUCT_LOADED_BY_PRIVILEGED_MODULES
251_product_single_value_vars += PRODUCT_VBOOT_SIGNING_KEY
252_product_single_value_vars += PRODUCT_VBOOT_SIGNING_SUBKEY
253_product_single_value_vars += PRODUCT_VERITY_SIGNING_KEY
254_product_single_value_vars += PRODUCT_SYSTEM_VERITY_PARTITION
255_product_single_value_vars += PRODUCT_VENDOR_VERITY_PARTITION
256_product_single_value_vars += PRODUCT_PRODUCT_VERITY_PARTITION
257_product_single_value_vars += PRODUCT_SYSTEM_EXT_VERITY_PARTITION
258_product_single_value_vars += PRODUCT_ODM_VERITY_PARTITION
259_product_single_value_vars += PRODUCT_SYSTEM_SERVER_DEBUG_INFO
260_product_single_value_vars += PRODUCT_OTHER_JAVA_DEBUG_INFO
261
262# Per-module dex-preopt configs.
263_product_list_vars += PRODUCT_DEX_PREOPT_MODULE_CONFIGS
264_product_list_vars += PRODUCT_DEX_PREOPT_DEFAULT_COMPILER_FILTER
265_product_list_vars += PRODUCT_DEX_PREOPT_DEFAULT_FLAGS
266_product_list_vars += PRODUCT_DEX_PREOPT_BOOT_FLAGS
267_product_list_vars += PRODUCT_DEX_PREOPT_PROFILE_DIR
268_product_list_vars += PRODUCT_DEX_PREOPT_GENERATE_DM_FILES
269_product_list_vars += PRODUCT_DEX_PREOPT_NEVER_ALLOW_STRIPPING
270_product_list_vars += PRODUCT_DEX_PREOPT_RESOLVE_STARTUP_STRINGS
271
272# Boot image options.
273_product_single_value_vars += \
274    PRODUCT_USE_PROFILE_FOR_BOOT_IMAGE \
275    PRODUCT_DEX_PREOPT_BOOT_IMAGE_PROFILE_LOCATION \
276    PRODUCT_USES_DEFAULT_ART_CONFIG \
277
278_product_list_vars += PRODUCT_SYSTEM_SERVER_COMPILER_FILTER
279# Per-module sanitizer configs
280_product_list_vars += PRODUCT_SANITIZER_MODULE_CONFIGS
281_product_single_value_vars += PRODUCT_SYSTEM_BASE_FS_PATH
282_product_single_value_vars += PRODUCT_VENDOR_BASE_FS_PATH
283_product_single_value_vars += PRODUCT_PRODUCT_BASE_FS_PATH
284_product_single_value_vars += PRODUCT_SYSTEM_EXT_BASE_FS_PATH
285_product_single_value_vars += PRODUCT_ODM_BASE_FS_PATH
286
287# The first API level this product shipped with
288_product_single_value_vars += PRODUCT_SHIPPING_API_LEVEL
289
290_product_list_vars += VENDOR_PRODUCT_RESTRICT_VENDOR_FILES
291_product_list_vars += VENDOR_EXCEPTION_MODULES
292_product_list_vars += VENDOR_EXCEPTION_PATHS
293# Whether the product wants to ship libartd. For rules and meaning, see art/Android.mk.
294_product_single_value_vars += PRODUCT_ART_TARGET_INCLUDE_DEBUG_BUILD
295
296# Make this art variable visible to soong_config.mk.
297_product_single_value_vars += PRODUCT_ART_USE_READ_BARRIER
298
299# Add reserved headroom to a system image.
300_product_single_value_vars += PRODUCT_SYSTEM_HEADROOM
301
302# Whether to save disk space by minimizing java debug info
303_product_single_value_vars += PRODUCT_MINIMIZE_JAVA_DEBUG_INFO
304
305# Whether any paths are excluded from sanitization when SANITIZE_TARGET=integer_overflow
306_product_list_vars += PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS
307
308_product_single_value_vars += PRODUCT_ADB_KEYS
309
310# Whether any paths should have CFI enabled for components
311_product_list_vars += PRODUCT_CFI_INCLUDE_PATHS
312
313# Whether any paths are excluded from sanitization when SANITIZE_TARGET=cfi
314_product_list_vars += PRODUCT_CFI_EXCLUDE_PATHS
315
316# Whether the Scudo hardened allocator is disabled platform-wide
317_product_single_value_vars += PRODUCT_DISABLE_SCUDO
318
319# A flag to override PRODUCT_COMPATIBLE_PROPERTY
320_product_single_value_vars += PRODUCT_COMPATIBLE_PROPERTY_OVERRIDE
321
322# List of extra VNDK versions to be included
323_product_list_vars += PRODUCT_EXTRA_VNDK_VERSIONS
324
325# VNDK version of product partition. It can be 'current' if the product
326# partitions uses PLATFORM_VNDK_VERSION.
327_product_single_value_var += PRODUCT_PRODUCT_VNDK_VERSION
328
329# Whether the list of allowed of actionable compatible properties should be disabled or not
330_product_single_value_vars += PRODUCT_ACTIONABLE_COMPATIBLE_PROPERTY_DISABLE
331
332_product_single_value_vars += PRODUCT_ENFORCE_ARTIFACT_PATH_REQUIREMENTS
333_product_single_value_vars += PRODUCT_ENFORCE_ARTIFACT_SYSTEM_CERTIFICATE_REQUIREMENT
334_product_list_vars += PRODUCT_ARTIFACT_SYSTEM_CERTIFICATE_REQUIREMENT_ALLOW_LIST
335_product_list_vars += PRODUCT_ARTIFACT_PATH_REQUIREMENT_HINT
336_product_list_vars += PRODUCT_ARTIFACT_PATH_REQUIREMENT_ALLOWED_LIST
337
338# List of modules that should be forcefully unmarked from being LOCAL_PRODUCT_MODULE, and hence
339# installed on /system directory by default.
340_product_list_vars += PRODUCT_FORCE_PRODUCT_MODULES_TO_SYSTEM_PARTITION
341
342# When this is true, dynamic partitions is retrofitted on a device that has
343# already been launched without dynamic partitions. Otherwise, the device
344# is launched with dynamic partitions.
345# This flag implies PRODUCT_USE_DYNAMIC_PARTITIONS.
346_product_single_value_vars += PRODUCT_RETROFIT_DYNAMIC_PARTITIONS
347
348# Other dynamic partition feature flags.PRODUCT_USE_DYNAMIC_PARTITION_SIZE and
349# PRODUCT_BUILD_SUPER_PARTITION default to the value of PRODUCT_USE_DYNAMIC_PARTITIONS.
350_product_single_value_vars += \
351    PRODUCT_USE_DYNAMIC_PARTITIONS \
352    PRODUCT_USE_DYNAMIC_PARTITION_SIZE \
353    PRODUCT_BUILD_SUPER_PARTITION \
354
355# If set, kernel configuration requirements are present in OTA package (and will be enforced
356# during OTA). Otherwise, kernel configuration requirements are enforced in VTS.
357# Devices that checks the running kernel (instead of the kernel in OTA package) should not
358# set this variable to prevent OTA failures.
359_product_list_vars += PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS
360
361# If set to true, this product builds a generic OTA package, which installs generic system images
362# onto matching devices. The product may only build a subset of system images (e.g. only
363# system.img), so devices need to install the package in a system-only OTA manner.
364_product_single_value_vars += PRODUCT_BUILD_GENERIC_OTA_PACKAGE
365
366_product_list_vars += PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES
367_product_list_vars += PRODUCT_PACKAGE_NAME_OVERRIDES
368_product_list_vars += PRODUCT_CERTIFICATE_OVERRIDES
369
370# Controls for whether different partitions are built for the current product.
371_product_single_value_vars += PRODUCT_BUILD_SYSTEM_IMAGE
372_product_single_value_vars += PRODUCT_BUILD_SYSTEM_OTHER_IMAGE
373_product_single_value_vars += PRODUCT_BUILD_VENDOR_IMAGE
374_product_single_value_vars += PRODUCT_BUILD_PRODUCT_IMAGE
375_product_single_value_vars += PRODUCT_BUILD_SYSTEM_EXT_IMAGE
376_product_single_value_vars += PRODUCT_BUILD_ODM_IMAGE
377_product_single_value_vars += PRODUCT_BUILD_CACHE_IMAGE
378_product_single_value_vars += PRODUCT_BUILD_RAMDISK_IMAGE
379_product_single_value_vars += PRODUCT_BUILD_USERDATA_IMAGE
380_product_single_value_vars += PRODUCT_BUILD_RECOVERY_IMAGE
381_product_single_value_vars += PRODUCT_BUILD_BOOT_IMAGE
382_product_single_value_vars += PRODUCT_BUILD_VBMETA_IMAGE
383
384# List of boot jars delivered via apex
385_product_list_vars += PRODUCT_UPDATABLE_BOOT_JARS
386
387# Whether the product would like to check prebuilt ELF files.
388_product_single_value_vars += PRODUCT_CHECK_ELF_FILES
389
390# If set, device uses virtual A/B.
391_product_single_value_vars += PRODUCT_VIRTUAL_AB_OTA
392
393# If set, device retrofits virtual A/B.
394_product_single_value_vars += PRODUCT_VIRTUAL_AB_OTA_RETROFIT
395
396# If set, forcefully generate a non-A/B update package.
397# Note: A device configuration should inherit from virtual_ab_ota_plus_non_ab.mk
398# instead of setting this variable directly.
399# Note: Use TARGET_OTA_ALLOW_NON_AB in the build system because
400# TARGET_OTA_ALLOW_NON_AB takes the value of AB_OTA_UPDATER into account.
401_product_single_value_vars += PRODUCT_OTA_FORCE_NON_AB_PACKAGE
402
403# If set, Java module in product partition cannot use hidden APIs.
404_product_single_value_vars += PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE
405
406_product_single_value_vars += PRODUCT_INSTALL_EXTRA_FLATTENED_APEXES
407
408.KATI_READONLY := _product_single_value_vars _product_list_vars
409_product_var_list :=$= $(_product_single_value_vars) $(_product_list_vars)
410
411define dump-product
412$(warning ==== $(1) ====)\
413$(foreach v,$(_product_var_list),\
414$(warning PRODUCTS.$(1).$(v) := $(call get-product-var,$(1),$(v))))\
415$(warning --------)
416endef
417
418define dump-products
419$(foreach p,$(PRODUCTS),$(call dump-product,$(p)))
420endef
421
422#
423# Functions for including product makefiles
424#
425
426#
427# $(1): product to inherit
428#
429# To be called from product makefiles, and is later evaluated during the import-nodes
430# call below. It does three things:
431#  1. Inherits all of the variables from $1.
432#  2. Records the inheritance in the .INHERITS_FROM variable
433#  3. Records the calling makefile in PARENT_PRODUCT_FILES
434#
435# (2) and (3) can be used together to reconstruct the include hierarchy
436# See e.g. product-graph.mk for an example of this.
437#
438define inherit-product
439  $(if $(findstring ../,$(1)),\
440    $(eval np := $(call normalize-paths,$(1))),\
441    $(eval np := $(strip $(1))))\
442  $(foreach v,$(_product_var_list), \
443      $(eval $(v) := $($(v)) $(INHERIT_TAG)$(np))) \
444  $(eval current_mk := $(strip $(word 1,$(_include_stack)))) \
445  $(eval inherit_var := PRODUCTS.$(current_mk).INHERITS_FROM) \
446  $(eval $(inherit_var) := $(sort $($(inherit_var)) $(np))) \
447  $(eval PARENT_PRODUCT_FILES := $(sort $(PARENT_PRODUCT_FILES) $(current_mk)))
448endef
449
450# Specifies a number of path prefixes, relative to PRODUCT_OUT, where the
451# product makefile hierarchy rooted in the current node places its artifacts.
452# Creating artifacts outside the specified paths will cause a build-time error.
453define require-artifacts-in-path
454  $(eval current_mk := $(strip $(word 1,$(_include_stack)))) \
455  $(eval PRODUCTS.$(current_mk).ARTIFACT_PATH_REQUIREMENTS := $(strip $(1))) \
456  $(eval PRODUCTS.$(current_mk).ARTIFACT_PATH_ALLOWED_LIST := $(strip $(2))) \
457  $(eval ARTIFACT_PATH_REQUIREMENT_PRODUCTS := \
458    $(sort $(ARTIFACT_PATH_REQUIREMENT_PRODUCTS) $(current_mk)))
459endef
460
461# Makes including non-existent modules in PRODUCT_PACKAGES an error.
462# $(1): list of non-existent modules to allow.
463define enforce-product-packages-exist
464  $(eval current_mk := $(strip $(word 1,$(_include_stack)))) \
465  $(eval PRODUCTS.$(current_mk).PRODUCT_ENFORCE_PACKAGES_EXIST := true) \
466  $(eval PRODUCTS.$(current_mk).PRODUCT_ENFORCE_PACKAGES_EXIST_ALLOW_LIST := $(1)) \
467  $(eval .KATI_READONLY := PRODUCTS.$(current_mk).PRODUCT_ENFORCE_PACKAGES_EXIST) \
468  $(eval .KATI_READONLY := PRODUCTS.$(current_mk).PRODUCT_ENFORCE_PACKAGES_EXIST_ALLOW_LIST)
469endef
470
471#
472# Do inherit-product only if $(1) exists
473#
474define inherit-product-if-exists
475  $(if $(wildcard $(1)),$(call inherit-product,$(1)),)
476endef
477
478#
479# $(1): product makefile list
480#
481#TODO: check to make sure that products have all the necessary vars defined
482define import-products
483$(call import-nodes,PRODUCTS,$(1),$(_product_var_list),$(_product_single_value_vars))
484endef
485
486
487#
488# Does various consistency checks on all of the known products.
489# Takes no parameters, so $(call ) is not necessary.
490#
491define check-all-products
492$(if ,, \
493  $(eval _cap_names :=) \
494  $(foreach p,$(PRODUCTS), \
495    $(eval pn := $(strip $(PRODUCTS.$(p).PRODUCT_NAME))) \
496    $(if $(pn),,$(error $(p): PRODUCT_NAME must be defined.)) \
497    $(if $(filter $(pn),$(_cap_names)), \
498      $(error $(p): PRODUCT_NAME must be unique; "$(pn)" already used by $(strip \
499          $(foreach \
500            pp,$(PRODUCTS),
501              $(if $(filter $(pn),$(PRODUCTS.$(pp).PRODUCT_NAME)), \
502                $(pp) \
503               ))) \
504       ) \
505     ) \
506    $(eval _cap_names += $(pn)) \
507    $(if $(call is-c-identifier,$(pn)),, \
508      $(error $(p): PRODUCT_NAME must be a valid C identifier, not "$(pn)") \
509     ) \
510    $(eval pb := $(strip $(PRODUCTS.$(p).PRODUCT_BRAND))) \
511    $(if $(pb),,$(error $(p): PRODUCT_BRAND must be defined.)) \
512    $(foreach cf,$(strip $(PRODUCTS.$(p).PRODUCT_COPY_FILES)), \
513      $(if $(filter 2 3,$(words $(subst :,$(space),$(cf)))),, \
514        $(error $(p): malformed COPY_FILE "$(cf)") \
515       ) \
516     ) \
517   ) \
518)
519endef
520
521
522#
523# Returns the product makefile path for the product with the provided name
524#
525# $(1): short product name like "generic"
526#
527define _resolve-short-product-name
528  $(eval pn := $(strip $(1)))
529  $(eval p := \
530      $(foreach p,$(PRODUCTS), \
531          $(if $(filter $(pn),$(PRODUCTS.$(p).PRODUCT_NAME)), \
532            $(p) \
533       )) \
534   )
535  $(eval p := $(sort $(p)))
536  $(if $(filter 1,$(words $(p))), \
537    $(p), \
538    $(if $(filter 0,$(words $(p))), \
539      $(error No matches for product "$(pn)"), \
540      $(error Product "$(pn)" ambiguous: matches $(p)) \
541    ) \
542  )
543endef
544define resolve-short-product-name
545$(strip $(call _resolve-short-product-name,$(1)))
546endef
547
548# BoardConfig variables that are also inherited in product mks. Should ideally
549# be cleaned up to not be product variables.
550_readonly_late_variables := \
551  DEVICE_PACKAGE_OVERLAYS \
552  WITH_DEXPREOPT_BOOT_IMG_AND_SYSTEM_SERVER_ONLY \
553
554# Modified internally in the build system
555_readonly_late_variables += \
556  PRODUCT_COPY_FILES \
557  PRODUCT_DEX_PREOPT_NEVER_ALLOW_STRIPPING \
558  PRODUCT_DEX_PREOPT_BOOT_FLAGS \
559
560_readonly_early_variables := $(filter-out $(_readonly_late_variables),$(_product_var_list))
561
562#
563# Mark the variables in _product_stash_var_list as readonly
564#
565define readonly-variables
566$(foreach v,$(1), \
567  $(eval $(v) ?=) \
568  $(eval .KATI_READONLY := $(v)) \
569 )
570endef
571define readonly-product-vars
572$(call readonly-variables,$(_readonly_early_variables))
573endef
574
575define readonly-final-product-vars
576$(call readonly-variables,$(_readonly_late_variables))
577endef
578
579# Macro re-defined inside strip-product-vars.
580get-product-var = $(PRODUCTS.$(strip $(1)).$(2))
581#
582# Strip the variables in _product_var_list and a few build-system
583# internal variables, and assign the ones for the current product
584# to a shorthand that is more convenient to read from elsewhere.
585#
586define strip-product-vars
587$(foreach v,\
588  $(_product_var_list) \
589    PRODUCT_ENFORCE_PACKAGES_EXIST \
590    PRODUCT_ENFORCE_PACKAGES_EXIST_ALLOW_LIST, \
591  $(eval $(v) := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).$(v)))) \
592  $(eval get-product-var = $$(if $$(filter $$(1),$$(INTERNAL_PRODUCT)),$$($$(2)),$$(PRODUCTS.$$(strip $$(1)).$$(2)))) \
593  $(KATI_obsolete_var PRODUCTS.$(INTERNAL_PRODUCT).$(v),Use $(v) instead) \
594)
595endef
596
597define add-to-product-copy-files-if-exists
598$(if $(wildcard $(word 1,$(subst :, ,$(1)))),$(1))
599endef
600
601# whitespace placeholder when we record module's dex-preopt config.
602_PDPMC_SP_PLACE_HOLDER := |@SP@|
603# Set up dex-preopt config for a module.
604# $(1) list of module names
605# $(2) the modules' dex-preopt config
606define add-product-dex-preopt-module-config
607$(eval _c := $(subst $(space),$(_PDPMC_SP_PLACE_HOLDER),$(strip $(2))))\
608$(eval PRODUCT_DEX_PREOPT_MODULE_CONFIGS += \
609  $(foreach m,$(1),$(m)=$(_c)))
610endef
611
612# whitespace placeholder when we record module's sanitizer config.
613_PSMC_SP_PLACE_HOLDER := |@SP@|
614# Set up sanitizer config for a module.
615# $(1) list of module names
616# $(2) the modules' sanitizer config
617define add-product-sanitizer-module-config
618$(eval _c := $(subst $(space),$(_PSMC_SP_PLACE_HOLDER),$(strip $(2))))\
619$(eval PRODUCT_SANITIZER_MODULE_CONFIGS += \
620  $(foreach m,$(1),$(m)=$(_c)))
621endef
622