xref: /aosp_15_r20/build/make/core/packaging/flags.mk (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15#
16# This file is included by build/make/core/Makefile, and contains the logic for
17# the combined flags files.
18#
19
20# TODO: Should we do all of the images in $(IMAGES_TO_BUILD)?
21_FLAG_PARTITIONS := product system vendor
22
23
24# -----------------------------------------------------------------
25# Aconfig Flags
26
27# Create a summary file of build flags for a single partition
28# $(1): built aconfig flags file (out)
29# $(2): installed aconfig flags file (out)
30# $(3): the partition (in)
31# $(4): input aconfig files for the partition (in)
32define generate-partition-aconfig-flag-file
33$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
34$(eval $(strip $(1)): PRIVATE_IN := $(strip $(4)))
35$(strip $(1)): $(ACONFIG) $(strip $(4))
36	mkdir -p $$(dir $$(PRIVATE_OUT))
37	$$(if $$(PRIVATE_IN), \
38		$$(ACONFIG) dump --dedup --format protobuf --out $$(PRIVATE_OUT) \
39			--filter container:$(strip $(3))+state:ENABLED \
40			--filter container:$(strip $(3))+permission:READ_WRITE \
41			$$(addprefix --cache ,$$(PRIVATE_IN)), \
42		echo -n > $$(PRIVATE_OUT) \
43	)
44$(call copy-one-file, $(1), $(2))
45endef
46
47
48# Create a summary file of build flags for each partition
49# $(1): built aconfig flags file (out)
50# $(2): installed aconfig flags file (out)
51# $(3): input aconfig files for the partition (in)
52define generate-global-aconfig-flag-file
53$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
54$(eval $(strip $(1)): PRIVATE_IN := $(strip $(3)))
55$(strip $(1)): $(ACONFIG) $(strip $(3))
56	mkdir -p $$(dir $$(PRIVATE_OUT))
57	$$(if $$(PRIVATE_IN), \
58		$$(ACONFIG) dump --dedup --format protobuf --out $$(PRIVATE_OUT) \
59			$$(addprefix --cache ,$$(PRIVATE_IN)), \
60		echo -n > $$(PRIVATE_OUT) \
61	)
62$(call copy-one-file, $(1), $(2))
63endef
64
65$(foreach partition, $(_FLAG_PARTITIONS), \
66	$(eval aconfig_flag_summaries_protobuf.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig_flags.pb) \
67	$(eval $(call generate-partition-aconfig-flag-file, \
68			$(TARGET_OUT_FLAGS)/$(partition)/aconfig_flags.pb, \
69			$(aconfig_flag_summaries_protobuf.$(partition)), \
70			$(partition), \
71			$(sort \
72				$(foreach m, $(call register-names-for-partition, $(partition)), \
73					$(ALL_MODULES.$(m).ACONFIG_FILES) \
74				) \
75				$(if $(filter system, $(partition)), \
76					$(foreach m, $(call register-names-for-partition, system_ext), \
77						$(ALL_MODULES.$(m).ACONFIG_FILES) \
78					) \
79				) \
80			) \
81	)) \
82)
83
84# Collect the on-device flags into a single file, similar to all_aconfig_declarations.
85required_aconfig_flags_files := \
86		$(sort $(foreach partition, $(filter $(IMAGES_TO_BUILD), $(_FLAG_PARTITIONS)), \
87			$(aconfig_flag_summaries_protobuf.$(partition)) \
88		))
89
90.PHONY: device_aconfig_declarations
91device_aconfig_declarations: $(PRODUCT_OUT)/device_aconfig_declarations.pb
92$(eval $(call generate-global-aconfig-flag-file, \
93			$(TARGET_OUT_FLAGS)/device_aconfig_declarations.pb, \
94			$(PRODUCT_OUT)/device_aconfig_declarations.pb, \
95			$(sort $(required_aconfig_flags_files)) \
96)) \
97
98# Create a set of storage file for each partition
99# $(1): built aconfig flags storage package map file (out)
100# $(2): built aconfig flags storage flag map file (out)
101# $(3): built aconfig flags storage flag val file (out)
102# $(4): built aconfig flags storage flag info file (out)
103# $(5): installed aconfig flags storage package map file (out)
104# $(6): installed aconfig flags storage flag map file (out)
105# $(7): installed aconfig flags storage flag value file (out)
106# $(8): installed aconfig flags storage flag info file (out)
107# $(9): input aconfig files for the partition (in)
108# $(10): partition name
109define generate-partition-aconfig-storage-file
110$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
111$(eval $(strip $(1)): PRIVATE_IN := $(strip $(9)))
112$(strip $(1)): $(ACONFIG) $(strip $(9))
113	mkdir -p $$(dir $$(PRIVATE_OUT))
114	$$(if $$(PRIVATE_IN), \
115		$$(ACONFIG) create-storage --container $(10) --file package_map --out $$(PRIVATE_OUT) \
116			$$(addprefix --cache ,$$(PRIVATE_IN)), \
117	)
118	touch $$(PRIVATE_OUT)
119$(eval $(strip $(2)): PRIVATE_OUT := $(strip $(2)))
120$(eval $(strip $(2)): PRIVATE_IN := $(strip $(9)))
121$(strip $(2)): $(ACONFIG) $(strip $(9))
122	mkdir -p $$(dir $$(PRIVATE_OUT))
123	$$(if $$(PRIVATE_IN), \
124		$$(ACONFIG) create-storage --container $(10) --file flag_map --out $$(PRIVATE_OUT) \
125			$$(addprefix --cache ,$$(PRIVATE_IN)), \
126	)
127	touch $$(PRIVATE_OUT)
128$(eval $(strip $(3)): PRIVATE_OUT := $(strip $(3)))
129$(eval $(strip $(3)): PRIVATE_IN := $(strip $(9)))
130$(strip $(3)): $(ACONFIG) $(strip $(9))
131	mkdir -p $$(dir $$(PRIVATE_OUT))
132	$$(if $$(PRIVATE_IN), \
133		$$(ACONFIG) create-storage --container $(10) --file flag_val --out $$(PRIVATE_OUT) \
134		$$(addprefix --cache ,$$(PRIVATE_IN)), \
135	)
136	touch $$(PRIVATE_OUT)
137$(eval $(strip $(4)): PRIVATE_OUT := $(strip $(4)))
138$(eval $(strip $(4)): PRIVATE_IN := $(strip $(9)))
139$(strip $(4)): $(ACONFIG) $(strip $(9))
140	mkdir -p $$(dir $$(PRIVATE_OUT))
141	$$(if $$(PRIVATE_IN), \
142		$$(ACONFIG) create-storage --container $(10) --file flag_info --out $$(PRIVATE_OUT) \
143		$$(addprefix --cache ,$$(PRIVATE_IN)), \
144	)
145	touch $$(PRIVATE_OUT)
146$(call copy-one-file, $(strip $(1)), $(5))
147$(call copy-one-file, $(strip $(2)), $(6))
148$(call copy-one-file, $(strip $(3)), $(7))
149$(call copy-one-file, $(strip $(4)), $(8))
150endef
151
152ifeq ($(RELEASE_CREATE_ACONFIG_STORAGE_FILE),true)
153$(foreach partition, $(_FLAG_PARTITIONS), \
154	$(eval aconfig_storage_package_map.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/package.map) \
155	$(eval aconfig_storage_flag_map.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/flag.map) \
156	$(eval aconfig_storage_flag_val.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/flag.val) \
157	$(eval aconfig_storage_flag_info.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/flag.info) \
158	$(eval $(call generate-partition-aconfig-storage-file, \
159				$(TARGET_OUT_FLAGS)/$(partition)/package.map, \
160				$(TARGET_OUT_FLAGS)/$(partition)/flag.map, \
161				$(TARGET_OUT_FLAGS)/$(partition)/flag.val, \
162				$(TARGET_OUT_FLAGS)/$(partition)/flag.info, \
163				$(aconfig_storage_package_map.$(partition)), \
164				$(aconfig_storage_flag_map.$(partition)), \
165				$(aconfig_storage_flag_val.$(partition)), \
166				$(aconfig_storage_flag_info.$(partition)), \
167				$(aconfig_flag_summaries_protobuf.$(partition)), \
168				$(partition), \
169	)) \
170)
171endif
172
173# -----------------------------------------------------------------
174# Install the ones we need for the configured product
175required_flags_files := \
176		$(sort $(foreach partition, $(filter $(IMAGES_TO_BUILD), $(_FLAG_PARTITIONS)), \
177			$(build_flag_summaries.$(partition)) \
178			$(aconfig_flag_summaries_protobuf.$(partition)) \
179			$(aconfig_storage_package_map.$(partition)) \
180			$(aconfig_storage_flag_map.$(partition)) \
181			$(aconfig_storage_flag_val.$(partition)) \
182			$(aconfig_storage_flag_info.$(partition)) \
183		))
184
185ALL_DEFAULT_INSTALLED_MODULES += $(required_flags_files)
186ALL_FLAGS_FILES := $(required_flags_files)
187
188# TODO: Remove
189.PHONY: flag-files
190flag-files: $(required_flags_files)
191
192
193# Clean up
194required_flags_files:=
195required_aconfig_flags_files:=
196$(foreach partition, $(_FLAG_PARTITIONS), \
197	$(eval build_flag_summaries.$(partition):=) \
198	$(eval aconfig_flag_summaries_protobuf.$(partition):=) \
199	$(eval aconfig_storage_package_map.$(partition):=) \
200	$(eval aconfig_storage_flag_map.$(partition):=) \
201	$(eval aconfig_storage_flag_val.$(partition):=) \
202	$(eval aconfig_storage_flag_info.$(partition):=) \
203)
204