From d40c94633b7b7a1778f4f45b054282ce0a8fd68d Mon Sep 17 00:00:00 2001 From: Sasha Smundak Date: Fri, 4 Mar 2022 13:11:10 -0800 Subject: [PATCH 1/2] Clean up product definitions reading. Bug: 220379303 Test: treehugger Change-Id: I6b9b3bd486a1680ace8278eb5160e09885543f4b --- core/product.mk | 92 ------------------------------------------ core/product_config.mk | 88 ++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 123 deletions(-) diff --git a/core/product.mk b/core/product.mk index 43724a8328..60b2dc9281 100644 --- a/core/product.mk +++ b/core/product.mk @@ -14,98 +14,6 @@ # limitations under the License. # -# -# Functions for including AndroidProducts.mk files -# PRODUCT_MAKEFILES is set up in AndroidProducts.mks. -# Format of PRODUCT_MAKEFILES: -# : -# If the is the same as the base file name (without dir -# and the .mk suffix) of the product makefile, ":" can be -# omitted. - -# -# Returns the list of all AndroidProducts.mk files. -# $(call ) isn't necessary. -# -define _find-android-products-files -$(file <$(OUT_DIR)/.module_paths/AndroidProducts.mk.list) \ - $(SRC_TARGET_DIR)/product/AndroidProducts.mk -endef - -# -# For entries returned by get-product-makefiles, decode an entry to a short -# product name. These either may be in the form of :path/to/file.mk or -# path/to/.mk -# $(1): The entry to decode -# -# Returns two words: -# -# -define _decode-product-name -$(strip \ - $(eval _cpm_words := $(subst :,$(space),$(1))) \ - $(if $(word 2,$(_cpm_words)), \ - $(wordlist 1,2,$(_cpm_words)), \ - $(basename $(notdir $(1))) $(1))) -endef - -# -# Validates the new common lunch choices -- ensures that they're in an -# appropriate form, and are paired with definitions of their products. -# $(1): The new list of COMMON_LUNCH_CHOICES -# $(2): The new list of PRODUCT_MAKEFILES -# -define _validate-common-lunch-choices -$(strip $(foreach choice,$(1),\ - $(eval _parts := $(subst -,$(space),$(choice))) \ - $(if $(call math_lt,$(words $(_parts)),2), \ - $(error $(LOCAL_DIR): $(choice): Invalid lunch choice)) \ - $(if $(call math_gt_or_eq,$(words $(_parts)),4), \ - $(error $(LOCAL_DIR): $(choice): Invalid lunch choice)) \ - $(if $(filter-out eng userdebug user,$(word 2,$(_parts))), \ - $(error $(LOCAL_DIR): $(choice): Invalid variant: $(word 2,$(_parts)))) \ - $(if $(filter-out $(foreach p,$(2),$(call _decode-product-name,$(p))),$(word 1,$(_parts))), \ - $(error $(LOCAL_DIR): $(word 1,$(_parts)): Product not defined in this file)) \ - )) -endef - -# -# Returns the sorted concatenation of PRODUCT_MAKEFILES -# variables set in the given AndroidProducts.mk files. -# $(1): the list of AndroidProducts.mk files. -# -# As a side-effect, COMMON_LUNCH_CHOICES will be set to a -# union of all of the COMMON_LUNCH_CHOICES definitions within -# each AndroidProducts.mk file. -# -define get-product-makefiles -$(sort \ - $(eval _COMMON_LUNCH_CHOICES :=) \ - $(foreach f,$(1), \ - $(eval PRODUCT_MAKEFILES :=) \ - $(eval COMMON_LUNCH_CHOICES :=) \ - $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \ - $(eval include $(f)) \ - $(call _validate-common-lunch-choices,$(COMMON_LUNCH_CHOICES),$(PRODUCT_MAKEFILES)) \ - $(eval _COMMON_LUNCH_CHOICES += $(COMMON_LUNCH_CHOICES)) \ - $(PRODUCT_MAKEFILES) \ - ) \ - $(eval PRODUCT_MAKEFILES :=) \ - $(eval LOCAL_DIR :=) \ - $(eval COMMON_LUNCH_CHOICES := $(sort $(_COMMON_LUNCH_CHOICES))) \ - $(eval _COMMON_LUNCH_CHOICES :=) \ - ) -endef - -# -# Returns the sorted concatenation of all PRODUCT_MAKEFILES -# variables set in all AndroidProducts.mk files. -# $(call ) isn't necessary. -# -define get-all-product-makefiles -$(call get-product-makefiles,$(_find-android-products-files)) -endef - # Variables that are meant to hold only a single value. # - The value set in the current makefile takes precedence over inherited values # - If multiple inherited makefiles set the var, the first-inherited value wins diff --git a/core/product_config.mk b/core/product_config.mk index 1deb39bbdf..34e393e86d 100644 --- a/core/product_config.mk +++ b/core/product_config.mk @@ -146,32 +146,64 @@ include $(BUILD_SYSTEM)/node_fns.mk include $(BUILD_SYSTEM)/product.mk include $(BUILD_SYSTEM)/device.mk -# Read in all of the product definitions specified by the AndroidProducts.mk -# files in the tree. -all_product_configs := $(get-all-product-makefiles) +# Read all product definitions. +# +# Products are defined in AndroidProducts.mk files: +android_products_makefiles := $(file <$(OUT_DIR)/.module_paths/AndroidProducts.mk.list) \ + $(SRC_TARGET_DIR)/product/AndroidProducts.mk -all_named_products := +# An AndroidProduct.mk file sets the following variables: +# PRODUCT_MAKEFILES specifies product makefiles. Each item in this list +# is either a :path/to/file.mk, or just path/to/ +# COMMON_LUNCH_CHOICES specifies - values to be shown +# in the `lunch` menu -# Find the product config makefile for the current product. -# all_product_configs consists items like: -# : -# or just in case the product name is the -# same as the base filename of the product config makefile. -current_product_makefile := -all_product_makefiles := -$(foreach f, $(all_product_configs),\ - $(eval _cpm_words := $(call _decode-product-name,$(f)))\ - $(eval _cpm_word1 := $(word 1,$(_cpm_words)))\ - $(eval _cpm_word2 := $(word 2,$(_cpm_words)))\ - $(eval all_product_makefiles += $(_cpm_word2))\ - $(eval all_named_products += $(_cpm_word1))\ - $(if $(filter $(TARGET_PRODUCT),$(_cpm_word1)),\ - $(eval current_product_makefile += $(_cpm_word2)),)) -_cpm_words := -_cpm_word1 := -_cpm_word2 := -current_product_makefile := $(strip $(current_product_makefile)) -all_product_makefiles := $(strip $(all_product_makefiles)) +# Builds a list of first/second elements of each pair: +# $(call _first,a:A b:B,:) returns 'a b' +# $(call _second,a-A b-B,-) returns 'A B' +_first=$(filter-out $(2)%,$(subst $(2),$(space)$(2),$(1))) +_second=$(filter-out %$(2),$(subst $(2),$(2)$(space),$(1))) + +# Returns : pair from a PRODUCT_MAKEFILE item. +# If an item is :path/to/file.mk, return it as is, +# otherwise assume that an item is path/to/.mk and +# return :path/to/.mk +_product-spec=$(strip $(if $(findstring :,$(1)),$(1),$(basename $(notdir $(1))):$(1))) + +# Reads given AndroidProduct.mk file and sets the following variables: +# ap_product_paths -- the list of : pairs +# ap_common_lunch_choices -- the list of - items +# In addition, validates COMMON_LUNCH_CHOICES values +define _read-ap-file + $(eval PRODUCT_MAKEFILES :=) \ + $(eval COMMON_LUNCH_CHOICES :=) \ + $(eval ap_product_paths :=) \ + $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \ + $(eval include $(f)) \ + $(foreach p, $(PRODUCT_MAKEFILES),$(eval ap_product_paths += $(call _product-spec,$(p)))) \ + $(eval ap_common_lunch_choices := $(COMMON_LUNCH_CHOICES)) \ + $(eval _products := $(call _first,$(ap_product_paths),:)) \ + $(eval _bad := $(filter-out $(_products),$(call _first,$(ap_common_lunch_choices),-))) \ + $(if $(_bad),$(error COMMON_LUNCH_CHOICES contains products(s) not defined in this file: $(_bad))) \ + $(eval _bad := $(filter-out %-eng %-userdebug %-user,$(ap_common_lunch_choices))) \ + $(if $(_bad),$(error invalid variant in COMMON_LUNCH_CHOICES: $(_bad))) +endef + +# Build cumulative lists of all product specs/lunch choices +product_paths := +common_lunch_choices := +$(foreach f,$(android_products_makefiles), \ + $(call _read-ap-file,$(f)) \ + $(eval product_paths += $(ap_product_paths)) \ + $(eval common_lunch_choices += $(ap_common_lunch_choices)) \ +) + +# Dedup, extract product names, etc. +product_paths :=$(sort $(product_paths)) +all_named_products := $(call _first,$(product_paths),:) +all_product_makefiles := $(call _second,$(product_paths),:) +current_product_makefile := $(call _second,$(filter $(TARGET_PRODUCT):%,$(product_paths)),:) +COMMON_LUNCH_CHOICES := $(sort $(common_lunch_choices)) load_all_product_makefiles := ifneq (,$(filter product-graph, $(MAKECMDGOALS))) @@ -195,13 +227,7 @@ ifeq ($(load_all_product_makefiles),true) $(call import-products, $(all_product_makefiles)) else # Import just the current product. -ifndef current_product_makefile -$(error Can not locate config makefile for product "$(TARGET_PRODUCT)") -endif -ifneq (1,$(words $(current_product_makefile))) -$(error Product "$(TARGET_PRODUCT)" ambiguous: matches $(current_product_makefile)) -endif - +$(if $(current_product_makefile),,$(error Can not locate config makefile for product "$(TARGET_PRODUCT)")) ifndef RBC_PRODUCT_CONFIG $(call import-products, $(current_product_makefile)) else From b87b773042981bf4c72c1742b0dd93bc666c0857 Mon Sep 17 00:00:00 2001 From: Sasha Smundak Date: Fri, 4 Mar 2022 13:51:18 -0800 Subject: [PATCH 2/2] Add an option to use Starlark-based configuration by default. In order to use Starlark-based product and board configuration for a product, add its name to the STARLARK_OPT_IN_PRODUCTS list in the AndroidProducts.mk file where this product is defined. Fixes: 220379303 Test: Temporarily added STARLARK_OPT_IN_PRODUCTS to build/make/target/product/AndroidProducts.mk Change-Id: Iacd23cec00a7a1958701873bce0fc9d5f79b1f17 --- core/product_config.mk | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/core/product_config.mk b/core/product_config.mk index 34e393e86d..be4aded376 100644 --- a/core/product_config.mk +++ b/core/product_config.mk @@ -157,6 +157,8 @@ android_products_makefiles := $(file <$(OUT_DIR)/.module_paths/AndroidProducts.m # is either a :path/to/file.mk, or just path/to/ # COMMON_LUNCH_CHOICES specifies - values to be shown # in the `lunch` menu +# STARLARK_OPT_IN_PRODUCTS specifies products to use Starlark-based +# product configuration by default # Builds a list of first/second elements of each pair: # $(call _first,a:A b:B,:) returns 'a b' @@ -173,29 +175,36 @@ _product-spec=$(strip $(if $(findstring :,$(1)),$(1),$(basename $(notdir $(1))): # Reads given AndroidProduct.mk file and sets the following variables: # ap_product_paths -- the list of : pairs # ap_common_lunch_choices -- the list of - items -# In addition, validates COMMON_LUNCH_CHOICES values +# ap_products_using_starlark_config -- the list of products using starlark config +# In addition, validates COMMON_LUNCH_CHOICES and STARLARK_OPT_IN_PRODUCTS values define _read-ap-file $(eval PRODUCT_MAKEFILES :=) \ $(eval COMMON_LUNCH_CHOICES :=) \ + $(eval STARLARK_OPT_IN_PRODUCTS := ) \ $(eval ap_product_paths :=) \ $(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \ $(eval include $(f)) \ $(foreach p, $(PRODUCT_MAKEFILES),$(eval ap_product_paths += $(call _product-spec,$(p)))) \ $(eval ap_common_lunch_choices := $(COMMON_LUNCH_CHOICES)) \ + $(eval ap_products_using_starlark_config := $(STARLARK_OPT_IN_PRODUCTS)) \ $(eval _products := $(call _first,$(ap_product_paths),:)) \ $(eval _bad := $(filter-out $(_products),$(call _first,$(ap_common_lunch_choices),-))) \ $(if $(_bad),$(error COMMON_LUNCH_CHOICES contains products(s) not defined in this file: $(_bad))) \ $(eval _bad := $(filter-out %-eng %-userdebug %-user,$(ap_common_lunch_choices))) \ $(if $(_bad),$(error invalid variant in COMMON_LUNCH_CHOICES: $(_bad))) + $(eval _bad := $(filter-out $(_products),$(ap_products_using_starlark_config))) \ + $(if $(_bad),$(error STARLARK_OPT_IN_PRODUCTS contains product(s) not defined in this file: $(_bad))) endef -# Build cumulative lists of all product specs/lunch choices +# Build cumulative lists of all product specs/lunch choices/Starlark-based products. product_paths := common_lunch_choices := +products_using_starlark_config := $(foreach f,$(android_products_makefiles), \ $(call _read-ap-file,$(f)) \ $(eval product_paths += $(ap_product_paths)) \ $(eval common_lunch_choices += $(ap_common_lunch_choices)) \ + $(eval products_using_starlark_config += $(ap_products_using_starlark_config)) \ ) # Dedup, extract product names, etc. @@ -228,6 +237,11 @@ $(call import-products, $(all_product_makefiles)) else # Import just the current product. $(if $(current_product_makefile),,$(error Can not locate config makefile for product "$(TARGET_PRODUCT)")) +ifneq (,$(filter $(TARGET_PRODUCT),$(products_using_starlark_config))) + RBC_PRODUCT_CONFIG := true + RBC_BOARD_CONFIG := true +endif + ifndef RBC_PRODUCT_CONFIG $(call import-products, $(current_product_makefile)) else