blob: bb519804d11957b3424e9c770a1e858193a11923 [file] [log] [blame]
Joe Onorato964f4012023-05-06 12:29:01 -07001# 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
Joe Onoratod6df20a2023-06-09 18:51:00 -070015
16# -----------------------------------------------------------------
17# Choose the flag files
18# -----------------------------------------------------------------
Joe Onorato1f655512023-06-12 23:29:25 -070019# Release configs are defined in reflease_config_map files, which map
20# the short name (e.g. -next) used in lunch to the starlark files
21# defining the build flag values.
22#
23# (If you're thinking about aconfig flags, there is one build flag,
Joe Onorato6aa48f82023-06-21 15:17:42 -070024# RELEASE_ACONFIG_VALUE_SETS, that sets which aconfig_value_set
Joe Onorato1f655512023-06-12 23:29:25 -070025# module to use to set the aconfig flag values.)
26#
27# The short release config names *can* appear multiple times, to allow
28# for AOSP and vendor specific flags under the same name, but the
29# individual flag values must appear in exactly one config. Vendor
30# does not override AOSP, or anything like that. This is because
31# vendor code usually includes prebuilts, and having vendor compile
32# with different flags from AOSP increases the likelihood of flag
33# mismatch.
34
Joe Onoratod6df20a2023-06-09 18:51:00 -070035# Do this first, because we're going to unset TARGET_RELEASE before
36# including anyone, so they don't start making conditionals based on it.
37# This logic is in make because starlark doesn't understand optional
38# vendor files.
39
40# If this is a google source tree, restrict it to only the one file
41# which has OWNERS control. If it isn't let others define their own.
42# TODO: Remove wildcard for build/release one when all branch manifests
43# have updated.
LaMont Jones20dd4c22024-05-01 11:24:08 -070044_must_protobuf :=
LaMont Jones91fa4322024-03-29 14:50:50 -070045config_map_files := $(wildcard build/release/release_config_map.mk) \
LaMont Jones318dafe2024-03-07 17:08:05 -080046 $(wildcard vendor/google_shared/build/release/release_config_map.mk) \
Joe Onoratod6df20a2023-06-09 18:51:00 -070047 $(if $(wildcard vendor/google/release/release_config_map.mk), \
48 vendor/google/release/release_config_map.mk, \
49 $(sort \
50 $(wildcard device/*/release/release_config_map.mk) \
51 $(wildcard device/*/*/release/release_config_map.mk) \
52 $(wildcard vendor/*/release/release_config_map.mk) \
53 $(wildcard vendor/*/*/release/release_config_map.mk) \
54 ) \
55 )
56
LaMont Jones20dd4c22024-05-01 11:24:08 -070057protobuf_map_files := $(wildcard build/release/release_config_map.textproto) \
58 $(wildcard vendor/google_shared/build/release/release_config_map.textproto) \
59 $(if $(wildcard vendor/google/release/release_config_map.textproto), \
60 vendor/google/release/release_config_map.textproto, \
61 $(sort \
62 $(wildcard device/*/release/release_config_map.textproto) \
63 $(wildcard device/*/*/release/release_config_map.textproto) \
64 $(wildcard vendor/*/release/release_config_map.textproto) \
65 $(wildcard vendor/*/*/release/release_config_map.textproto) \
66 ) \
67 )
68
LaMont Jones38b195e2023-11-06 22:14:51 +000069# PRODUCT_RELEASE_CONFIG_MAPS is set by Soong using an initial run of product
70# config to capture only the list of config maps needed by the build.
71# Keep them in the order provided, but remove duplicates.
LaMont Jones20dd4c22024-05-01 11:24:08 -070072# Treat .mk and .textproto as equal for duplicate elimination, but force
73# protobuf if any PRODUCT_RELEASE_CONFIG_MAPS specify .textproto.
LaMont Jones38b195e2023-11-06 22:14:51 +000074$(foreach map,$(PRODUCT_RELEASE_CONFIG_MAPS), \
LaMont Jones20dd4c22024-05-01 11:24:08 -070075 $(if $(filter $(basename $(map)),$(basename $(config_map_files))),, \
76 $(eval config_map_files += $(map))) \
77 $(if $(filter $(basename $(map)).textproto,$(map)),$(eval _must_protobuf := true)) \
LaMont Jones38b195e2023-11-06 22:14:51 +000078)
79
LaMont Jones20dd4c22024-05-01 11:24:08 -070080
81# If we are missing the textproto version of any of $(config_map_files), we cannot use protobuf.
82_can_protobuf := true
83$(foreach map,$(config_map_files), \
84 $(if $(wildcard $(basename $(map)).textproto),,$(eval _can_protobuf :=)) \
85)
86# If we are missing the mk version of any of $(protobuf_map_files), we must use protobuf.
87$(foreach map,$(protobuf_map_files), \
88 $(if $(wildcard $(basename $(map)).mk),,$(eval _must_protobuf := true)) \
89)
90
91ifneq (,$(_must_protobuf))
92 ifeq (,$(_can_protobuf))
93 # We must use protobuf, but we cannot use protobuf.
94 $(error release config is a mixture of .scl and .textproto)
95 endif
96endif
97
98_use_protobuf :=
99ifneq (,$(_must_protobuf))
100 _use_protobuf := true
101else
102 ifneq ($(_can_protobuf),)
103 # Determine the default
104 $(foreach map,$(config_map_files), \
105 $(if $(wildcard $(dir $(map))/build_config/DEFAULT=proto),$(eval _use_protobuf := true)) \
106 $(if $(wildcard $(dir $(map))/build_config/DEFAULT=make),$(eval _use_protobuf := )) \
107 )
108 # Update for this specific release config only (no inheritance).
109 $(foreach map,$(config_map_files), \
110 $(if $(wildcard $(dir $(map))/build_config/$(TARGET_RELEASE)=proto),$(eval _use_protobuf := true)) \
111 $(if $(wildcard $(dir $(map))/build_config/$(TARGET_RELEASE)=make),$(eval _use_protobuf := )) \
112 )
113 endif
114endif
115
116ifneq (,$(_use_protobuf))
117 # The .textproto files are the canonical source of truth.
118 _args := $(foreach map,$(config_map_files), --map $(map) )
119 ifneq (,$(_must_protobuf))
120 # Disable the build flag in release-config.
121 _args += --guard=false
122 endif
123 $(KATI_shell_no_rerun $(OUT_DIR)/release-config $(_args) >$(OUT_DIR)/release-config.out && touch -t 200001010000 $(OUT_DIR)/release-config.out)
124 $(if $(filter-out 0,$(.SHELLSTATUS)),$(error release-config failed to run))
125 # This will also set _all_release_configs for us.
126 $(eval include $(OUT_DIR)/soong/release-config/release_config-$(TARGET_PRODUCT)-$(TARGET_RELEASE).mk)
127 $(KATI_extra_file_deps $(OUT_DIR)/release-config $(config_map_files))
128 ifeq (,$(_must_protobuf)$(RELEASE_BUILD_FLAGS_IN_PROTOBUF))
129 _use_protobuf :=
130 endif
131endif
132ifeq (,$(_use_protobuf))
133 # The .mk files are the canonical source of truth.
134
135
LaMont Jones61b0f792024-01-30 23:04:02 +0000136# Declare an alias release-config
137#
138# This should be used to declare a release as an alias of another, meaning no
139# release config files should be present.
140#
141# $1 config name
142# $2 release config for which it is an alias
143define alias-release-config
144 $(call _declare-release-config,$(1),,$(2),true)
145endef
146
LaMont Jones38b195e2023-11-06 22:14:51 +0000147# Declare or extend a release-config.
148#
LaMont Jones55d5fc52024-01-23 19:15:49 +0000149# The order of processing is:
150# 1. Recursively apply any overridden release configs. Only apply each config
151# the first time we reach it.
152# 2. Apply any files for this release config, in the order they were added to
153# the declaration.
154#
155# Example:
156# With these declarations:
157# $(declare-release-config foo, foo.scl)
158# $(declare-release-config bar, bar.scl, foo)
159# $(declare-release-config baz, baz.scl, bar)
160# $(declare-release-config bif, bif.scl, foo baz)
161# $(declare-release-config bop, bop.scl, bar baz)
162#
163# TARGET_RELEASE:
164# - bar will use: foo.scl bar.scl
165# - baz will use: foo.scl bar.scl baz.scl
166# - bif will use: foo.scl bar.scl baz.scl bif.scl
167# - bop will use: foo.scl bar.scl baz.scl bop.scl
168#
Joe Onoratod6df20a2023-06-09 18:51:00 -0700169# $1 config name
170# $2 release config files
LaMont Jones55d5fc52024-01-23 19:15:49 +0000171# $3 overridden release config
Joe Onoratod6df20a2023-06-09 18:51:00 -0700172define declare-release-config
LaMont Jones61b0f792024-01-30 23:04:02 +0000173 $(call _declare-release-config,$(1),$(2),$(3),)
174endef
175
176define _declare-release-config
LaMont Jones55d5fc52024-01-23 19:15:49 +0000177 $(if $(strip $(2)$(3)),, \
178 $(error declare-release-config: config $(strip $(1)) must have release config files, override another release config, or both) \
Joe Onoratod6df20a2023-06-09 18:51:00 -0700179 )
LaMont Jones61b0f792024-01-30 23:04:02 +0000180 $(if $(strip $(4)),$(eval _all_release_configs.$(strip $(1)).ALIAS := true))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700181 $(eval _all_release_configs := $(sort $(_all_release_configs) $(strip $(1))))
LaMont Jonesa6b6e672023-11-27 20:12:36 +0000182 $(if $(strip $(3)), \
183 $(if $(filter $(_all_release_configs), $(strip $(3))),
184 $(if $(filter $(_all_release_configs.$(strip $(1)).OVERRIDES),$(strip $(3))),,
185 $(eval _all_release_configs.$(strip $(1)).OVERRIDES := $(_all_release_configs.$(strip $(1)).OVERRIDES) $(strip $(3)))), \
186 $(error No release config $(strip $(3))) \
187 ) \
188 )
Joe Onorato1f655512023-06-12 23:29:25 -0700189 $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included) $(_all_release_configs.$(strip $(1)).DECLARED_IN))
190 $(eval _all_release_configs.$(strip $(1)).FILES := $(_all_release_configs.$(strip $(1)).FILES) $(strip $(2)))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700191endef
192
LaMont Jones38b195e2023-11-06 22:14:51 +0000193# Include the config map files and populate _flag_declaration_files.
LaMont Jones451abd62024-03-14 10:48:55 -0700194# If the file is found more than once, only include it the first time.
LaMont Jones38b195e2023-11-06 22:14:51 +0000195_flag_declaration_files :=
LaMont Jones451abd62024-03-14 10:48:55 -0700196_included_config_map_files :=
Joe Onoratod6df20a2023-06-09 18:51:00 -0700197$(foreach f, $(config_map_files), \
LaMont Jones38b195e2023-11-06 22:14:51 +0000198 $(eval FLAG_DECLARATION_FILES:= ) \
LaMont Jones451abd62024-03-14 10:48:55 -0700199 $(if $(filter $(_included_config_map_files),$(f)),,\
200 $(eval _included := $(f)) \
201 $(eval include $(f)) \
202 $(eval _flag_declaration_files += $(FLAG_DECLARATION_FILES)) \
203 $(eval _included_config_map_files += $(f)) \
204 ) \
LaMont Jones38b195e2023-11-06 22:14:51 +0000205)
206FLAG_DECLARATION_FILES :=
207
LaMont Jones76452d82024-03-20 12:24:21 -0700208# Verify that all inherited/overridden release configs are declared.
209$(foreach config,$(_all_release_configs),\
210 $(foreach r,$(all_release_configs.$(r).OVERRIDES),\
211 $(if $(strip $(_all_release_configs.$(r).FILES)$(_all_release_configs.$(r).OVERRIDES)),,\
212 $(error Release config $(config) [declared in: $(_all_release_configs.$(r).DECLARED_IN)] inherits from non-existent $(r).)\
213)))
214# Verify that alias configs do not have config files.
215$(foreach r,$(_all_release_configs),\
216 $(if $(_all_release_configs.$(r).ALIAS),$(if $(_all_release_configs.$(r).FILES),\
217 $(error Alias release config "$(r)" may not specify release config files $(_all_release_configs.$(r).FILES))\
218)))
219
LaMont Jones20dd4c22024-05-01 11:24:08 -0700220# Use makefiles
221endif
222
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000223ifeq ($(TARGET_RELEASE),)
224 # We allow some internal paths to explicitly set TARGET_RELEASE to the
225 # empty string. For the most part, 'make' treats unset and empty string as
226 # the same. But the following line differentiates, and will only assign
227 # if the variable was completely unset.
228 TARGET_RELEASE ?= was_unset
229 ifeq ($(TARGET_RELEASE),was_unset)
230 $(error No release config set for target; please set TARGET_RELEASE, or if building on the command line use 'lunch <target>-<release>-<build_type>', where release is one of: $(_all_release_configs))
231 endif
232 # Instead of leaving this string empty, we want to default to a valid
233 # setting. Full builds coming through this path is a bug, but in case
234 # of such a bug, we want to at least get consistent, valid results.
235 TARGET_RELEASE = trunk_staging
236endif
237
LaMont Jones76452d82024-03-20 12:24:21 -0700238# During pass 1 of product config, using a non-existent release config is not an error.
239# We can safely assume that we are doing pass 1 if DUMP_MANY_VARS=="PRODUCT_RELEASE_CONFIG_MAPS".
240ifneq (PRODUCT_RELEASE_CONFIG_MAPS,$(DUMP_MANY_VARS))
241 ifeq ($(filter $(_all_release_configs), $(TARGET_RELEASE)),)
242 $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(_all_release_configs))
243 endif
Joe Onoratod6df20a2023-06-09 18:51:00 -0700244endif
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000245
LaMont Jones20dd4c22024-05-01 11:24:08 -0700246ifeq (,$(_use_protobuf))
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000247# Choose flag files
248# Don't sort this, use it in the order they gave us.
249# Do allow duplicate entries, retaining only the first usage.
Joe Onoratod6df20a2023-06-09 18:51:00 -0700250flag_value_files :=
LaMont Jones55d5fc52024-01-23 19:15:49 +0000251
252# Apply overrides recursively
253#
254# $1 release config that we override
255applied_releases :=
256define _apply-release-config-overrides
257$(foreach r,$(1), \
258 $(if $(filter $(r),$(applied_releases)),, \
259 $(foreach o,$(_all_release_configs.$(r).OVERRIDES),$(call _apply-release-config-overrides,$(o)))\
260 $(eval applied_releases += $(r))\
LaMont Jonesa6b6e672023-11-27 20:12:36 +0000261 $(foreach f,$(_all_release_configs.$(r).FILES), \
262 $(if $(filter $(f),$(flag_value_files)),,$(eval flag_value_files += $(f)))\
263 )\
LaMont Jones55d5fc52024-01-23 19:15:49 +0000264 )\
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000265)
LaMont Jones55d5fc52024-01-23 19:15:49 +0000266endef
267$(call _apply-release-config-overrides,$(TARGET_RELEASE))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700268# Unset variables so they can't use them
269define declare-release-config
270$(error declare-release-config can only be called from inside release_config_map.mk files)
271endef
LaMont Jones76452d82024-03-20 12:24:21 -0700272define _apply-release-config-overrides
LaMont Jones55d5fc52024-01-23 19:15:49 +0000273$(error invalid use of apply-release-config-overrides)
274endef
Joe Onoratod6df20a2023-06-09 18:51:00 -0700275
LaMont Jones20dd4c22024-05-01 11:24:08 -0700276# use makefiles
277endif
278
Joe Onoratod6df20a2023-06-09 18:51:00 -0700279# TODO: Remove this check after enough people have sourced lunch that we don't
280# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
281ifneq ($(CALLED_FROM_SETUP),true)
282define TARGET_RELEASE
283$(error TARGET_RELEASE may not be accessed directly. Use individual flags.)
284endef
285else
286TARGET_RELEASE:=
287endif
288.KATI_READONLY := TARGET_RELEASE
289
LaMont Jones20dd4c22024-05-01 11:24:08 -0700290ifeq (,$(_use_protobuf))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700291$(foreach config, $(_all_release_configs), \
292 $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
293 $(eval _all_release_configs.$(config).FILES:= ) \
294)
LaMont Jones20dd4c22024-05-01 11:24:08 -0700295applied_releases:=
296# use makefiles
297endif
Joe Onoratod6df20a2023-06-09 18:51:00 -0700298_all_release_configs:=
299config_map_files:=
LaMont Jones20dd4c22024-05-01 11:24:08 -0700300protobuf_map_files:=
Joe Onoratod6df20a2023-06-09 18:51:00 -0700301
302
LaMont Jones20dd4c22024-05-01 11:24:08 -0700303ifeq (,$(_use_protobuf))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700304# -----------------------------------------------------------------
305# Flag declarations and values
306# -----------------------------------------------------------------
307# This part is in starlark. We generate a root starlark file that loads
308# all of the flags declaration files that we found, and the flag_value_files
309# that we chose from the config map above. Then we run that, and load the
310# results of that into the make environment.
311
LaMont Jones38b195e2023-11-06 22:14:51 +0000312# _flag_declaration_files is the combined list of FLAG_DECLARATION_FILES set by
313# release_config_map.mk files above.
Joe Onorato964f4012023-05-06 12:29:01 -0700314
Cole Faust386b3742023-06-06 16:55:58 -0700315# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
316# contains the result of the above wildcards for the starlark code to use.
317filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
Cole Faust9a106f32023-11-08 09:51:04 -0800318_c:=load("//build/make/core/release_config.scl", "release_config")
Joe Onoratod6df20a2023-06-09 18:51:00 -0700319_c+=$(newline)def add(d, k, v):
320_c+=$(newline)$(space)d = dict(d)
321_c+=$(newline)$(space)d[k] = v
322_c+=$(newline)$(space)return d
LaMont Jones38b195e2023-11-06 22:14:51 +0000323_c+=$(foreach f,$(_flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
324_c+=$(newline)all_flags = [] $(foreach f,$(_flag_declaration_files),+ [add(x, "declared_in", "$(f)") for x in flags_$(call filename_to_starlark,$(f))])
Joe Onoratod6df20a2023-06-09 18:51:00 -0700325_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
326_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
327_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
Cole Faust9a106f32023-11-08 09:51:04 -0800328$(file >$(OUT_DIR)/release_config_entrypoint.scl,$(_c))
Cole Faust386b3742023-06-06 16:55:58 -0700329_c:=
330filename_to_starlark:=
Joe Onorato964f4012023-05-06 12:29:01 -0700331
Cole Faust386b3742023-06-06 16:55:58 -0700332# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
333# rerun kati every build. Kati will replay the $(file) command that generates it every build,
334# updating its timestamp.
335#
336# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
337# outside of the source tree.
Cole Faust9a106f32023-11-08 09:51:04 -0800338$(call run-starlark,$(OUT_DIR)/release_config_entrypoint.scl,$(OUT_DIR)/release_config_entrypoint.scl,--allow_external_entrypoint)
Joe Onorato7b578d32023-05-19 09:13:36 -0700339
LaMont Jones20dd4c22024-05-01 11:24:08 -0700340# use makefiles
341endif
342_can_protobuf :=
343_must_protobuf :=
344_use_protobuf :=