blob: 3e51af5a44eeb281cd1b21bb777fc55bc5ca1366 [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 Jones91fa4322024-03-29 14:50:50 -070044config_map_files := $(wildcard build/release/release_config_map.mk) \
LaMont Jones318dafe2024-03-07 17:08:05 -080045 $(wildcard vendor/google_shared/build/release/release_config_map.mk) \
Joe Onoratod6df20a2023-06-09 18:51:00 -070046 $(if $(wildcard vendor/google/release/release_config_map.mk), \
47 vendor/google/release/release_config_map.mk, \
48 $(sort \
49 $(wildcard device/*/release/release_config_map.mk) \
50 $(wildcard device/*/*/release/release_config_map.mk) \
51 $(wildcard vendor/*/release/release_config_map.mk) \
52 $(wildcard vendor/*/*/release/release_config_map.mk) \
53 ) \
54 )
55
LaMont Jones38b195e2023-11-06 22:14:51 +000056# PRODUCT_RELEASE_CONFIG_MAPS is set by Soong using an initial run of product
57# config to capture only the list of config maps needed by the build.
58# Keep them in the order provided, but remove duplicates.
59$(foreach map,$(PRODUCT_RELEASE_CONFIG_MAPS), \
60 $(if $(filter $(map),$(config_map_files)),,$(eval config_map_files += $(map))) \
61)
62
LaMont Jones61b0f792024-01-30 23:04:02 +000063# Declare an alias release-config
64#
65# This should be used to declare a release as an alias of another, meaning no
66# release config files should be present.
67#
68# $1 config name
69# $2 release config for which it is an alias
70define alias-release-config
71 $(call _declare-release-config,$(1),,$(2),true)
72endef
73
LaMont Jones38b195e2023-11-06 22:14:51 +000074# Declare or extend a release-config.
75#
LaMont Jones55d5fc52024-01-23 19:15:49 +000076# The order of processing is:
77# 1. Recursively apply any overridden release configs. Only apply each config
78# the first time we reach it.
79# 2. Apply any files for this release config, in the order they were added to
80# the declaration.
81#
82# Example:
83# With these declarations:
84# $(declare-release-config foo, foo.scl)
85# $(declare-release-config bar, bar.scl, foo)
86# $(declare-release-config baz, baz.scl, bar)
87# $(declare-release-config bif, bif.scl, foo baz)
88# $(declare-release-config bop, bop.scl, bar baz)
89#
90# TARGET_RELEASE:
91# - bar will use: foo.scl bar.scl
92# - baz will use: foo.scl bar.scl baz.scl
93# - bif will use: foo.scl bar.scl baz.scl bif.scl
94# - bop will use: foo.scl bar.scl baz.scl bop.scl
95#
Joe Onoratod6df20a2023-06-09 18:51:00 -070096# $1 config name
97# $2 release config files
LaMont Jones55d5fc52024-01-23 19:15:49 +000098# $3 overridden release config
Joe Onoratod6df20a2023-06-09 18:51:00 -070099define declare-release-config
LaMont Jones61b0f792024-01-30 23:04:02 +0000100 $(call _declare-release-config,$(1),$(2),$(3),)
101endef
102
103define _declare-release-config
LaMont Jones55d5fc52024-01-23 19:15:49 +0000104 $(if $(strip $(2)$(3)),, \
105 $(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 -0700106 )
LaMont Jones61b0f792024-01-30 23:04:02 +0000107 $(if $(strip $(4)),$(eval _all_release_configs.$(strip $(1)).ALIAS := true))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700108 $(eval _all_release_configs := $(sort $(_all_release_configs) $(strip $(1))))
LaMont Jonesa6b6e672023-11-27 20:12:36 +0000109 $(if $(strip $(3)), \
110 $(if $(filter $(_all_release_configs), $(strip $(3))),
111 $(if $(filter $(_all_release_configs.$(strip $(1)).OVERRIDES),$(strip $(3))),,
112 $(eval _all_release_configs.$(strip $(1)).OVERRIDES := $(_all_release_configs.$(strip $(1)).OVERRIDES) $(strip $(3)))), \
113 $(error No release config $(strip $(3))) \
114 ) \
115 )
Joe Onorato1f655512023-06-12 23:29:25 -0700116 $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included) $(_all_release_configs.$(strip $(1)).DECLARED_IN))
117 $(eval _all_release_configs.$(strip $(1)).FILES := $(_all_release_configs.$(strip $(1)).FILES) $(strip $(2)))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700118endef
119
LaMont Jones38b195e2023-11-06 22:14:51 +0000120# Include the config map files and populate _flag_declaration_files.
LaMont Jones451abd62024-03-14 10:48:55 -0700121# If the file is found more than once, only include it the first time.
LaMont Jones38b195e2023-11-06 22:14:51 +0000122_flag_declaration_files :=
LaMont Jones451abd62024-03-14 10:48:55 -0700123_included_config_map_files :=
Joe Onoratod6df20a2023-06-09 18:51:00 -0700124$(foreach f, $(config_map_files), \
LaMont Jones38b195e2023-11-06 22:14:51 +0000125 $(eval FLAG_DECLARATION_FILES:= ) \
LaMont Jones451abd62024-03-14 10:48:55 -0700126 $(if $(filter $(_included_config_map_files),$(f)),,\
127 $(eval _included := $(f)) \
128 $(eval include $(f)) \
129 $(eval _flag_declaration_files += $(FLAG_DECLARATION_FILES)) \
130 $(eval _included_config_map_files += $(f)) \
131 ) \
LaMont Jones38b195e2023-11-06 22:14:51 +0000132)
133FLAG_DECLARATION_FILES :=
134
LaMont Jones76452d82024-03-20 12:24:21 -0700135# Verify that all inherited/overridden release configs are declared.
136$(foreach config,$(_all_release_configs),\
137 $(foreach r,$(all_release_configs.$(r).OVERRIDES),\
138 $(if $(strip $(_all_release_configs.$(r).FILES)$(_all_release_configs.$(r).OVERRIDES)),,\
139 $(error Release config $(config) [declared in: $(_all_release_configs.$(r).DECLARED_IN)] inherits from non-existent $(r).)\
140)))
141# Verify that alias configs do not have config files.
142$(foreach r,$(_all_release_configs),\
143 $(if $(_all_release_configs.$(r).ALIAS),$(if $(_all_release_configs.$(r).FILES),\
144 $(error Alias release config "$(r)" may not specify release config files $(_all_release_configs.$(r).FILES))\
145)))
146
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000147ifeq ($(TARGET_RELEASE),)
148 # We allow some internal paths to explicitly set TARGET_RELEASE to the
149 # empty string. For the most part, 'make' treats unset and empty string as
150 # the same. But the following line differentiates, and will only assign
151 # if the variable was completely unset.
152 TARGET_RELEASE ?= was_unset
153 ifeq ($(TARGET_RELEASE),was_unset)
154 $(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))
155 endif
156 # Instead of leaving this string empty, we want to default to a valid
157 # setting. Full builds coming through this path is a bug, but in case
158 # of such a bug, we want to at least get consistent, valid results.
159 TARGET_RELEASE = trunk_staging
160endif
161
LaMont Jones76452d82024-03-20 12:24:21 -0700162# During pass 1 of product config, using a non-existent release config is not an error.
163# We can safely assume that we are doing pass 1 if DUMP_MANY_VARS=="PRODUCT_RELEASE_CONFIG_MAPS".
164ifneq (PRODUCT_RELEASE_CONFIG_MAPS,$(DUMP_MANY_VARS))
165 ifeq ($(filter $(_all_release_configs), $(TARGET_RELEASE)),)
166 $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(_all_release_configs))
167 endif
Joe Onoratod6df20a2023-06-09 18:51:00 -0700168endif
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000169
170# Choose flag files
171# Don't sort this, use it in the order they gave us.
172# Do allow duplicate entries, retaining only the first usage.
Joe Onoratod6df20a2023-06-09 18:51:00 -0700173flag_value_files :=
LaMont Jones55d5fc52024-01-23 19:15:49 +0000174
175# Apply overrides recursively
176#
177# $1 release config that we override
178applied_releases :=
179define _apply-release-config-overrides
180$(foreach r,$(1), \
181 $(if $(filter $(r),$(applied_releases)),, \
182 $(foreach o,$(_all_release_configs.$(r).OVERRIDES),$(call _apply-release-config-overrides,$(o)))\
183 $(eval applied_releases += $(r))\
LaMont Jonesa6b6e672023-11-27 20:12:36 +0000184 $(foreach f,$(_all_release_configs.$(r).FILES), \
185 $(if $(filter $(f),$(flag_value_files)),,$(eval flag_value_files += $(f)))\
186 )\
LaMont Jones55d5fc52024-01-23 19:15:49 +0000187 )\
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000188)
LaMont Jones55d5fc52024-01-23 19:15:49 +0000189endef
190$(call _apply-release-config-overrides,$(TARGET_RELEASE))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700191# Unset variables so they can't use them
192define declare-release-config
193$(error declare-release-config can only be called from inside release_config_map.mk files)
194endef
LaMont Jones76452d82024-03-20 12:24:21 -0700195define _apply-release-config-overrides
LaMont Jones55d5fc52024-01-23 19:15:49 +0000196$(error invalid use of apply-release-config-overrides)
197endef
Joe Onoratod6df20a2023-06-09 18:51:00 -0700198
199# TODO: Remove this check after enough people have sourced lunch that we don't
200# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
201ifneq ($(CALLED_FROM_SETUP),true)
202define TARGET_RELEASE
203$(error TARGET_RELEASE may not be accessed directly. Use individual flags.)
204endef
205else
206TARGET_RELEASE:=
207endif
208.KATI_READONLY := TARGET_RELEASE
209
Joe Onoratod6df20a2023-06-09 18:51:00 -0700210$(foreach config, $(_all_release_configs), \
211 $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
212 $(eval _all_release_configs.$(config).FILES:= ) \
213)
214_all_release_configs:=
215config_map_files:=
LaMont Jones55d5fc52024-01-23 19:15:49 +0000216applied_releases:=
Joe Onoratod6df20a2023-06-09 18:51:00 -0700217
218
219# -----------------------------------------------------------------
220# Flag declarations and values
221# -----------------------------------------------------------------
222# This part is in starlark. We generate a root starlark file that loads
223# all of the flags declaration files that we found, and the flag_value_files
224# that we chose from the config map above. Then we run that, and load the
225# results of that into the make environment.
226
LaMont Jones38b195e2023-11-06 22:14:51 +0000227# _flag_declaration_files is the combined list of FLAG_DECLARATION_FILES set by
228# release_config_map.mk files above.
Joe Onorato964f4012023-05-06 12:29:01 -0700229
Cole Faust386b3742023-06-06 16:55:58 -0700230# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
231# contains the result of the above wildcards for the starlark code to use.
232filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
Cole Faust9a106f32023-11-08 09:51:04 -0800233_c:=load("//build/make/core/release_config.scl", "release_config")
Joe Onoratod6df20a2023-06-09 18:51:00 -0700234_c+=$(newline)def add(d, k, v):
235_c+=$(newline)$(space)d = dict(d)
236_c+=$(newline)$(space)d[k] = v
237_c+=$(newline)$(space)return d
LaMont Jones38b195e2023-11-06 22:14:51 +0000238_c+=$(foreach f,$(_flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
239_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 -0700240_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
241_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
242_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
Cole Faust9a106f32023-11-08 09:51:04 -0800243$(file >$(OUT_DIR)/release_config_entrypoint.scl,$(_c))
Cole Faust386b3742023-06-06 16:55:58 -0700244_c:=
245filename_to_starlark:=
Joe Onorato964f4012023-05-06 12:29:01 -0700246
Cole Faust386b3742023-06-06 16:55:58 -0700247# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
248# rerun kati every build. Kati will replay the $(file) command that generates it every build,
249# updating its timestamp.
250#
251# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
252# outside of the source tree.
Cole Faust9a106f32023-11-08 09:51:04 -0800253$(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 -0700254