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 system_ext vendor
22
23
24# -----------------------------------------------------------------
25# Aconfig Flags
26
27# Create a summary file of build flags for each partition
28# $(1): built aconfig flags file (out)
29# $(2): installed aconfig flags file (out)
30# $(3): input aconfig files for the partition (in)
31define generate-partition-aconfig-flag-file
32$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
33$(eval $(strip $(1)): PRIVATE_IN := $(strip $(3)))
34$(strip $(1)): $(ACONFIG) $(strip $(3))
35	mkdir -p $$(dir $$(PRIVATE_OUT))
36	$$(if $$(PRIVATE_IN), \
37		$$(ACONFIG) dump --dedup --format protobuf --out $$(PRIVATE_OUT) \
38			$$(addprefix --cache ,$$(PRIVATE_IN)), \
39		echo -n > $$(PRIVATE_OUT) \
40	)
41$(call copy-one-file, $(1), $(2))
42endef
43
44
45$(foreach partition, $(_FLAG_PARTITIONS), \
46	$(eval aconfig_flag_summaries_protobuf.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig_flags.pb) \
47	$(eval $(call generate-partition-aconfig-flag-file, \
48				$(TARGET_OUT_FLAGS)/$(partition)/aconfig_flags.pb, \
49				$(aconfig_flag_summaries_protobuf.$(partition)), \
50				$(sort $(foreach m,$(call register-names-for-partition, $(partition)), \
51					$(ALL_MODULES.$(m).ACONFIG_FILES) \
52				)), \
53	)) \
54)
55
56# Collect the on-device flags into a single file, similar to all_aconfig_declarations.
57required_aconfig_flags_files := \
58		$(sort $(foreach partition, $(filter $(IMAGES_TO_BUILD), $(_FLAG_PARTITIONS)), \
59			$(aconfig_flag_summaries_protobuf.$(partition)) \
60		))
61
62.PHONY: device_aconfig_declarations
63device_aconfig_declarations: $(PRODUCT_OUT)/device_aconfig_declarations.pb
64$(eval $(call generate-partition-aconfig-flag-file, \
65			$(TARGET_OUT_FLAGS)/device_aconfig_declarations.pb, \
66			$(PRODUCT_OUT)/device_aconfig_declarations.pb, \
67			$(sort $(required_aconfig_flags_files)) \
68)) \
69
70# Create a set of storage file for each partition
71# $(1): built aconfig flags storage package map file (out)
72# $(2): built aconfig flags storage flag map file (out)
73# $(3): built aconfig flags storage flag val file (out)
74# $(4): installed aconfig flags storage package map file (out)
75# $(5): installed aconfig flags storage flag map file (out)
76# $(6): installed aconfig flags storage flag value file (out)
77# $(7): input aconfig files for the partition (in)
78# $(8): partition name
79define generate-partition-aconfig-storage-file
80$(eval $(strip $(1)): PRIVATE_OUT := $(strip $(1)))
81$(eval $(strip $(1)): PRIVATE_IN := $(strip $(7)))
82$(strip $(1)): $(ACONFIG) $(strip $(7))
83	mkdir -p $$(dir $$(PRIVATE_OUT))
84	$$(if $$(PRIVATE_IN), \
85		$$(ACONFIG) create-storage --container $(8) --file package_map --out $$(PRIVATE_OUT) \
86			$$(addprefix --cache ,$$(PRIVATE_IN)), \
87	)
88	touch $$(PRIVATE_OUT)
89$(eval $(strip $(2)): PRIVATE_OUT := $(strip $(2)))
90$(eval $(strip $(2)): PRIVATE_IN := $(strip $(7)))
91$(strip $(2)): $(ACONFIG) $(strip $(7))
92	mkdir -p $$(dir $$(PRIVATE_OUT))
93	$$(if $$(PRIVATE_IN), \
94		$$(ACONFIG) create-storage --container $(8) --file flag_map --out $$(PRIVATE_OUT) \
95			$$(addprefix --cache ,$$(PRIVATE_IN)), \
96	)
97	touch $$(PRIVATE_OUT)
98$(eval $(strip $(3)): PRIVATE_OUT := $(strip $(3)))
99$(eval $(strip $(3)): PRIVATE_IN := $(strip $(7)))
100$(strip $(3)): $(ACONFIG) $(strip $(7))
101	mkdir -p $$(dir $$(PRIVATE_OUT))
102	$$(if $$(PRIVATE_IN), \
103		$$(ACONFIG) create-storage --container $(8) --file flag_val --out $$(PRIVATE_OUT) \
104		$$(addprefix --cache ,$$(PRIVATE_IN)), \
105	)
106	touch $$(PRIVATE_OUT)
107$(call copy-one-file, $(strip $(1)), $(4))
108$(call copy-one-file, $(strip $(2)), $(5))
109$(call copy-one-file, $(strip $(3)), $(6))
110endef
111
112ifeq ($(RELEASE_CREATE_ACONFIG_STORAGE_FILE),true)
113$(foreach partition, $(_FLAG_PARTITIONS), \
114	$(eval aconfig_storage_package_map.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/package.map) \
115	$(eval aconfig_storage_flag_map.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/flag.map) \
116	$(eval aconfig_storage_flag_val.$(partition) := $(PRODUCT_OUT)/$(partition)/etc/aconfig/flag.val) \
117	$(eval $(call generate-partition-aconfig-storage-file, \
118				$(TARGET_OUT_FLAGS)/$(partition)/package.map, \
119				$(TARGET_OUT_FLAGS)/$(partition)/flag.map, \
120				$(TARGET_OUT_FLAGS)/$(partition)/flag.val, \
121				$(aconfig_storage_package_map.$(partition)), \
122				$(aconfig_storage_flag_map.$(partition)), \
123				$(aconfig_storage_flag_val.$(partition)), \
124				$(sort $(foreach m,$(call register-names-for-partition, $(partition)), \
125					$(ALL_MODULES.$(m).ACONFIG_FILES) \
126				)), \
127				$(partition), \
128	)) \
129)
130endif
131
132# -----------------------------------------------------------------
133# Install the ones we need for the configured product
134required_flags_files := \
135		$(sort $(foreach partition, $(filter $(IMAGES_TO_BUILD), $(_FLAG_PARTITIONS)), \
136			$(build_flag_summaries.$(partition)) \
137			$(aconfig_flag_summaries_protobuf.$(partition)) \
138			$(aconfig_storage_package_map.$(partition)) \
139			$(aconfig_storage_flag_map.$(partition)) \
140			$(aconfig_storage_flag_val.$(partition)) \
141		))
142
143ALL_DEFAULT_INSTALLED_MODULES += $(required_flags_files)
144ALL_FLAGS_FILES := $(required_flags_files)
145
146# TODO: Remove
147.PHONY: flag-files
148flag-files: $(required_flags_files)
149
150
151# Clean up
152required_flags_files:=
153required_aconfig_flags_files:=
154$(foreach partition, $(_FLAG_PARTITIONS), \
155	$(eval build_flag_summaries.$(partition):=) \
156	$(eval aconfig_flag_summaries_protobuf.$(partition):=) \
157	$(eval aconfig_storage_package_map.$(partition):=) \
158	$(eval aconfig_storage_flag_map.$(partition):=) \
159	$(eval aconfig_storage_flag_val.$(partition):=) \
160)
161
162