android编译系统中有两个概念product和device,product.mk是关于product相关的函数和变量。
下面的函数找到编译系统中的AndroidProducts.mk,在AndroidProducts.mk中一般都定义了具体产品相关makefile的文件。
#
# 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.
#
# 找到所有的AndroidProducts.mk,在device和vendor中找,最大深入6层子目录
# 这里还有个SRC_TARGET_DIR为build/target,aosp_arm等的mk在这个文件夹下
define _find-android-products-files
$(shell test -d device && find device -maxdepth 6 -name AndroidProducts.mk) \
$(shell test -d vendor && find vendor -maxdepth 6 -name AndroidProducts.mk) \
$(SRC_TARGET_DIR)/product/AndroidProducts.mk
endef
下面的函数的输入为AndroidProducts.mk,将这些makefile include进来,这些makefile中其实都是定义了一些PRODUCT_MAKEFILES变量,函数的返回值为将这些PRODUCT_MAKEFILES的值进行sort,sort就是按字母排序,同时删除重复的,所以返回值就是一些产品makefile文件的列表。例如
build/target/product/aosp_arm.mk build/target/product/full.mk
#
# Returns the sorted concatenation of PRODUCT_MAKEFILES
# variables set in the given AndroidProducts.mk files.
# $(1): the list of AndroidProducts.mk files.
#
define get-product-makefiles
$(sort \
$(foreach f,$(1), \
$(eval PRODUCT_MAKEFILES :=) \
$(eval LOCAL_DIR := $(patsubst %/,%,$(dir $(f)))) \
$(eval include $(f)) \
$(PRODUCT_MAKEFILES) \
) \
$(eval PRODUCT_MAKEFILES :=) \
$(eval LOCAL_DIR :=) \
)
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
下面这些变量都非常有用,每个产品都有一组这样的变量。在lunch时,会首先设置个变量TARGET_PRODUCT,而执行完makefile命令后,就会实现TARGET_PRODUCT的翻译,完成后就会得到这样一组变量。
#
# Functions for including product makefiles
#
_product_var_list := \
PRODUCT_NAME \
PRODUCT_MODEL \
PRODUCT_LOCALES \
PRODUCT_AAPT_CONFIG \
PRODUCT_AAPT_PREF_CONFIG \
PRODUCT_AAPT_PREBUILT_DPI \
PRODUCT_PACKAGES \
PRODUCT_PACKAGES_DEBUG \
PRODUCT_PACKAGES_ENG \
PRODUCT_PACKAGES_TESTS \
PRODUCT_DEVICE \
PRODUCT_MANUFACTURER \
PRODUCT_BRAND \
PRODUCT_PROPERTY_OVERRIDES \
PRODUCT_DEFAULT_PROPERTY_OVERRIDES \
PRODUCT_CHARACTERISTICS \
PRODUCT_COPY_FILES \
PRODUCT_OTA_PUBLIC_KEYS \
PRODUCT_EXTRA_RECOVERY_KEYS \
PRODUCT_PACKAGE_OVERLAYS \
DEVICE_PACKAGE_OVERLAYS \
PRODUCT_SDK_ATREE_FILES \
PRODUCT_SDK_ADDON_NAME \
PRODUCT_SDK_ADDON_COPY_FILES \
PRODUCT_SDK_ADDON_COPY_MODULES \
PRODUCT_SDK_ADDON_DOC_MODULES \
PRODUCT_SDK_ADDON_SYS_IMG_SOURCE_PROP \
PRODUCT_DEFAULT_WIFI_CHANNELS \
PRODUCT_DEFAULT_DEV_CERTIFICATE \
PRODUCT_RESTRICT_VENDOR_FILES \
PRODUCT_VENDOR_KERNEL_HEADERS \
PRODUCT_BOOT_JARS \
PRODUCT_SUPPORTS_BOOT_SIGNER \
PRODUCT_SUPPORTS_VBOOT \
PRODUCT_SUPPORTS_VERITY \
PRODUCT_OEM_PROPERTIES \
PRODUCT_SYSTEM_PROPERTY_BLACKLIST \
PRODUCT_SYSTEM_SERVER_JARS \
PRODUCT_VBOOT_SIGNING_KEY \
PRODUCT_VERITY_SIGNING_KEY \
PRODUCT_SYSTEM_VERITY_PARTITION \
PRODUCT_VENDOR_VERITY_PARTITION \
PRODUCT_DEX_PREOPT_MODULE_CONFIGS \
PRODUCT_DEX_PREOPT_DEFAULT_FLAGS \
PRODUCT_DEX_PREOPT_BOOT_FLAGS \
下面的函数将每个产品的上面的一组变量都打印出来,从函数可以看出,AOSP中有很多的产品(PRODUCTS),每个产品对应的PRODUCT_*相关的变量v的值是通过PRODUCTS.*.v来区分,其中两个点中间的星号是不同产品的makefile的名字(带路径)。
define dump-product
$(info ==== $(1) ====)\
$(foreach v,$(_product_var_list),\
$(info PRODUCTS.$(1).$(v) := $(PRODUCTS.$(1).$(v))))\
$(info --------)
endef
define dump-products
$(foreach p,$(PRODUCTS),$(call dump-product,$(p)))
endef
下面定义了非常重要的函数,继承某个产品,android不同的产品肯定是有一些共性的东东,这些东东就可以作为公共的部分让其他产品去继承。
#
# $(1): product to inherit
#
# Does three things:
# 1. Inherits all of the variables from $1.
# 2. Records the inheritance in the .INHERITS_FROM variable
# 3. Records that we've visited this node, in ALL_PRODUCTS
# 这个函数的参数为需要继承的产品的makefile,例如build/target/product/full.mk
# 函数在执行时,首先获取原_product_var_list中的值,例如PRODUCT_NAME=aosp_arm,在
# 值后加后缀@inherit:build/target/product/full.mk,表明从哪个node去继承
# inherit_var= PRODUCTS.aosp_arm.mk.INHERITS_FROM,这里_include_stack就是类似一个需要import
# 的node的一个栈,正在import的node在最前面(word 1),假如此时为aosp_arm.mk,变量的含义很清楚,
# aosp_arm.mk从哪继承。并将inherit_var赋值为build/target/product/full.mk
# android编译系统有个全局的变量ALL_PRODUCTS,用来描述已经import的product。
define inherit-product
$(foreach v,$(_product_var_list), \
$(eval $(v) := $($(v)) $(INHERIT_TAG)$(strip $(1)) )) \
$(eval inherit_var := \
PRODUCTS.$(strip $(word 1,$(_include_stack))).INHERITS_FROM) \
$(eval $(inherit_var) := $(sort $($(inherit_var)) $(strip $(1)))) \
$(eval inherit_var:=) \
$(eval ALL_PRODUCTS := $(sort $(ALL_PRODUCTS) $(word 1,$(_include_stack))))
endef
#
# Do inherit-product only if $(1) exists
#
define inherit-product-if-exists
$(if $(wildcard $(1)),$(call inherit-product,$(1)),)
endef
下面是个非常重要的函数,import-products,将产品的相关makefile导入,import-nodes在node_fns.mk中,记住,所有node相关的函数都在node_fns.mk中。如果某个产品的makefile中定义了PRODUCT_NAME=aosp_arm,通过这个函数,变量存储到另外一个变量,_nic.PRODUCTS.[[build/target/product/aosp_arm.mk]].build/target/product/aosp_arm.mk.PRODUCT_NAME=aosp_arm
,将变量的值和相应的产品的makefile的联系了起来。
#
# $(1): product makefile list
#
#TODO: check to make sure that products have all the necessary vars defined
define import-products
$(call import-nodes,PRODUCTS,$(1),$(_product_var_list))
endef
下面这个函数检查某个产品参数是否设置正确,后续如果要添加自己的产品,起码要满足下面的条件。
#
# Does various consistency checks on all of the known products.
# Takes no parameters, so $(call ) is not necessary.
#
# PRODUCT_NAME必须定义
# 且多个PRODUCTS名字不能重复,名字命名符合c描述符
# PRODUCT_BRAND必须定义,
# PRODUCT_COPY_FILES 格式为<source file>:file>[:].file> 所以最后
# 的words可以是2 也可以是3
define check-all-products
$(if ,, \
$(eval _cap_names :=) \
$(foreach p,$(PRODUCTS), \
$(eval pn := $(strip $(PRODUCTS.$(p).PRODUCT_NAME))) \
$(if $(pn),,$(error $(p): PRODUCT_NAME must be defined.)) \
$(if $(filter $(pn),$(_cap_names)), \
$(error $(p): PRODUCT_NAME must be unique; "$(pn)" already used by $(strip \
$(foreach \
pp,$(PRODUCTS),
$(if $(filter $(pn),$(PRODUCTS.$(pp).PRODUCT_NAME)), \
$(pp) \
))) \
) \
) \
$(eval _cap_names += $(pn)) \
$(if $(call is-c-identifier,$(pn)),, \
$(error $(p): PRODUCT_NAME must be a valid C identifier, not "$(pn)") \
) \
$(eval pb := $(strip $(PRODUCTS.$(p).PRODUCT_BRAND))) \
$(if $(pb),,$(error $(p): PRODUCT_BRAND must be defined.)) \
$(foreach cf,$(strip $(PRODUCTS.$(p).PRODUCT_COPY_FILES)), \
$(if $(filter 2 3,$(words $(subst :,$(space),$(cf)))),, \
$(error $(p): malformed COPY_FILE "$(cf)") \
) \
) \
) \
)
endef
通过短的产品名称,例如generic,返回产品makefile的路径
#
# Returns the product makefile path for the product with the provided name
#
# $(1): short product name like "generic"
#
define _resolve-short-product-name
$(eval pn := $(strip $(1)))
$(eval p := \
$(foreach p,$(PRODUCTS), \
$(if $(filter $(pn),$(PRODUCTS.$(p).PRODUCT_NAME)), \
$(p) \
)) \
)
$(eval p := $(sort $(p)))
$(if $(filter 1,$(words $(p))), \
$(p), \
$(if $(filter 0,$(words $(p))), \
$(error No matches for product "$(pn)"), \
$(error Product "$(pn)" ambiguous: matches $(p)) \
) \
)
endef
define resolve-short-product-name
$(strip $(call _resolve-short-product-name,$(1)))
endef
定义stash相关的变量
_product_stash_var_list := $(_product_var_list) \
PRODUCT_BOOTCLASSPATH \
PRODUCT_SYSTEM_SERVER_CLASSPATH \
TARGET_ARCH \
TARGET_ARCH_VARIANT \
TARGET_CPU_VARIANT \
TARGET_BOARD_PLATFORM \
TARGET_BOARD_PLATFORM_GPU \
TARGET_BOARD_KERNEL_HEADERS \
TARGET_DEVICE_KERNEL_HEADERS \
TARGET_PRODUCT_KERNEL_HEADERS \
TARGET_BOOTLOADER_BOARD_NAME \
TARGET_NO_BOOTLOADER \
TARGET_NO_KERNEL \
TARGET_NO_RECOVERY \
TARGET_NO_RADIOIMAGE \
TARGET_HARDWARE_3D \
TARGET_PROVIDES_INIT_RC \
TARGET_CPU_ABI \
TARGET_CPU_ABI2 \
_product_stash_var_list += \
BOARD_WPA_SUPPLICANT_DRIVER \
BOARD_WLAN_DEVICE \
BOARD_USES_GENERIC_AUDIO \
BOARD_KERNEL_CMDLINE \
BOARD_KERNEL_BASE \
BOARD_HAVE_BLUETOOTH \
BOARD_VENDOR_USE_AKMD \
BOARD_EGL_CFG \
BOARD_BOOTIMAGE_PARTITION_SIZE \
BOARD_RECOVERYIMAGE_PARTITION_SIZE \
BOARD_SYSTEMIMAGE_PARTITION_SIZE \
BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE \
BOARD_USERDATAIMAGE_FILE_SYSTEM_TYPE \
BOARD_USERDATAIMAGE_PARTITION_SIZE \
BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE \
BOARD_CACHEIMAGE_PARTITION_SIZE \
BOARD_FLASH_BLOCK_SIZE \
BOARD_VENDORIMAGE_PARTITION_SIZE \
BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE \
BOARD_INSTALLER_CMDLINE \
_product_stash_var_list += \
DEFAULT_SYSTEM_DEV_CERTIFICATE \
WITH_DEXPREOPT \
WITH_DEXPREOPT_BOOT_IMG_ONLY
_product_stash_var_list += \
GLOBAL_CFLAGS_NO_OVERRIDE \
GLOBAL_CPPFLAGS_NO_OVERRIDE \
下面两个函数,
#
# Stash values of the variables in _product_stash_var_list.
# $(1): Renamed prefix
#
define stash-product-vars
$(foreach v,$(_product_stash_var_list), \
$(eval $(strip $(1))_$(call rot13,$(v)):=$$($$(v))) \
)
endef
#
# Assert that the the variable stashed by stash-product-vars remains untouched.
# $(1): The prefix as supplied to stash-product-vars
#
define assert-product-vars
$(strip \
$(eval changed_variables:=)
$(foreach v,$(_product_stash_var_list), \
$(if $(call streq,$($(v)),$($(strip $(1))_$(call rot13,$(v)))),, \
$(eval $(warning $(v) has been modified: $($(v)))) \
$(eval $(warning previous value: $($(strip $(1))_$(call rot13,$(v))))) \
$(eval changed_variables := $(changed_variables) $(v))) \
) \
$(if $(changed_variables),\
$(eval $(error The following variables have been changed: $(changed_variables))),)
)
endef
define add-to-product-copy-files-if-exists
$(if $(wildcard $(word 1,$(subst :, ,$(1)))),$(1))
endef
模块dex preopt相关的配置?
# whitespace placeholder when we record module's dex-preopt config.
_PDPMC_SP_PLACE_HOLDER := |@SP@|
# Set up dex-preopt config for a module.
# $(1) list of module names
# $(2) the modules' dex-preopt config
define add-product-dex-preopt-module-config
$(eval _c := $(subst $(space),$(_PDPMC_SP_PLACE_HOLDER),$(strip $(2))))\
$(eval PRODUCT_DEX_PREOPT_MODULE_CONFIGS += \
$(foreach m,$(1),$(m)=$(_c)))
endef