blob: 1fb574756fdee1141b82065c7599296b72d110fd [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.
44config_map_files := $(wildcard build/release/release_config_map.mk) \
45 $(if $(wildcard vendor/google/release/release_config_map.mk), \
46 vendor/google/release/release_config_map.mk, \
47 $(sort \
48 $(wildcard device/*/release/release_config_map.mk) \
49 $(wildcard device/*/*/release/release_config_map.mk) \
50 $(wildcard vendor/*/release/release_config_map.mk) \
51 $(wildcard vendor/*/*/release/release_config_map.mk) \
52 ) \
53 )
54
LaMont Jones38b195e2023-11-06 22:14:51 +000055# PRODUCT_RELEASE_CONFIG_MAPS is set by Soong using an initial run of product
56# config to capture only the list of config maps needed by the build.
57# Keep them in the order provided, but remove duplicates.
58$(foreach map,$(PRODUCT_RELEASE_CONFIG_MAPS), \
59 $(if $(filter $(map),$(config_map_files)),,$(eval config_map_files += $(map))) \
60)
61
62# Declare or extend a release-config.
63#
Joe Onoratod6df20a2023-06-09 18:51:00 -070064# $1 config name
65# $2 release config files
LaMont Jonesa6b6e672023-11-27 20:12:36 +000066# $3 overridden release config. Only applied for $(TARGET_RELEASE), not in depth.
Joe Onoratod6df20a2023-06-09 18:51:00 -070067define declare-release-config
Joe Onoratod6df20a2023-06-09 18:51:00 -070068 $(if $(strip $(2)),, \
69 $(error declare-release-config: config $(strip $(1)) must have release config files) \
70 )
71 $(eval _all_release_configs := $(sort $(_all_release_configs) $(strip $(1))))
LaMont Jonesa6b6e672023-11-27 20:12:36 +000072 $(if $(strip $(3)), \
73 $(if $(filter $(_all_release_configs), $(strip $(3))),
74 $(if $(filter $(_all_release_configs.$(strip $(1)).OVERRIDES),$(strip $(3))),,
75 $(eval _all_release_configs.$(strip $(1)).OVERRIDES := $(_all_release_configs.$(strip $(1)).OVERRIDES) $(strip $(3)))), \
76 $(error No release config $(strip $(3))) \
77 ) \
78 )
Joe Onorato1f655512023-06-12 23:29:25 -070079 $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included) $(_all_release_configs.$(strip $(1)).DECLARED_IN))
80 $(eval _all_release_configs.$(strip $(1)).FILES := $(_all_release_configs.$(strip $(1)).FILES) $(strip $(2)))
Joe Onoratod6df20a2023-06-09 18:51:00 -070081endef
82
LaMont Jones38b195e2023-11-06 22:14:51 +000083# Include the config map files and populate _flag_declaration_files.
84_flag_declaration_files :=
Joe Onoratod6df20a2023-06-09 18:51:00 -070085$(foreach f, $(config_map_files), \
LaMont Jones38b195e2023-11-06 22:14:51 +000086 $(eval FLAG_DECLARATION_FILES:= ) \
Joe Onoratod6df20a2023-06-09 18:51:00 -070087 $(eval _included := $(f)) \
88 $(eval include $(f)) \
LaMont Jones38b195e2023-11-06 22:14:51 +000089 $(eval _flag_declaration_files += $(FLAG_DECLARATION_FILES)) \
90)
91FLAG_DECLARATION_FILES :=
92
Greg Kaiser0229ecf2023-11-09 20:28:55 +000093ifeq ($(TARGET_RELEASE),)
94 # We allow some internal paths to explicitly set TARGET_RELEASE to the
95 # empty string. For the most part, 'make' treats unset and empty string as
96 # the same. But the following line differentiates, and will only assign
97 # if the variable was completely unset.
98 TARGET_RELEASE ?= was_unset
99 ifeq ($(TARGET_RELEASE),was_unset)
100 $(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))
101 endif
102 # Instead of leaving this string empty, we want to default to a valid
103 # setting. Full builds coming through this path is a bug, but in case
104 # of such a bug, we want to at least get consistent, valid results.
105 TARGET_RELEASE = trunk_staging
106endif
107
Joe Onoratod6df20a2023-06-09 18:51:00 -0700108ifeq ($(filter $(_all_release_configs), $(TARGET_RELEASE)),)
109 $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(_all_release_configs))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700110endif
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000111
112# Choose flag files
113# Don't sort this, use it in the order they gave us.
114# Do allow duplicate entries, retaining only the first usage.
Joe Onoratod6df20a2023-06-09 18:51:00 -0700115flag_value_files :=
LaMont Jonesa6b6e672023-11-27 20:12:36 +0000116$(foreach r,$(_all_release_configs.$(TARGET_RELEASE).OVERRIDES) $(TARGET_RELEASE), \
117 $(foreach f,$(_all_release_configs.$(r).FILES), \
118 $(if $(filter $(f),$(flag_value_files)),,$(eval flag_value_files += $(f)))\
119 )\
Greg Kaiser0229ecf2023-11-09 20:28:55 +0000120)
Joe Onoratod6df20a2023-06-09 18:51:00 -0700121
122# Unset variables so they can't use them
123define declare-release-config
124$(error declare-release-config can only be called from inside release_config_map.mk files)
125endef
126
127# TODO: Remove this check after enough people have sourced lunch that we don't
128# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
129ifneq ($(CALLED_FROM_SETUP),true)
130define TARGET_RELEASE
131$(error TARGET_RELEASE may not be accessed directly. Use individual flags.)
132endef
133else
134TARGET_RELEASE:=
135endif
136.KATI_READONLY := TARGET_RELEASE
137
138
139$(foreach config, $(_all_release_configs), \
140 $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
141 $(eval _all_release_configs.$(config).FILES:= ) \
142)
143_all_release_configs:=
144config_map_files:=
145
146
147# -----------------------------------------------------------------
148# Flag declarations and values
149# -----------------------------------------------------------------
150# This part is in starlark. We generate a root starlark file that loads
151# all of the flags declaration files that we found, and the flag_value_files
152# that we chose from the config map above. Then we run that, and load the
153# results of that into the make environment.
154
LaMont Jones38b195e2023-11-06 22:14:51 +0000155# _flag_declaration_files is the combined list of FLAG_DECLARATION_FILES set by
156# release_config_map.mk files above.
Joe Onorato964f4012023-05-06 12:29:01 -0700157
Cole Faust386b3742023-06-06 16:55:58 -0700158# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
159# contains the result of the above wildcards for the starlark code to use.
160filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
Cole Faust9a106f32023-11-08 09:51:04 -0800161_c:=load("//build/make/core/release_config.scl", "release_config")
Joe Onoratod6df20a2023-06-09 18:51:00 -0700162_c+=$(newline)def add(d, k, v):
163_c+=$(newline)$(space)d = dict(d)
164_c+=$(newline)$(space)d[k] = v
165_c+=$(newline)$(space)return d
LaMont Jones38b195e2023-11-06 22:14:51 +0000166_c+=$(foreach f,$(_flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
167_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 -0700168_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
169_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
170_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
Cole Faust9a106f32023-11-08 09:51:04 -0800171$(file >$(OUT_DIR)/release_config_entrypoint.scl,$(_c))
Cole Faust386b3742023-06-06 16:55:58 -0700172_c:=
173filename_to_starlark:=
Joe Onorato964f4012023-05-06 12:29:01 -0700174
Cole Faust386b3742023-06-06 16:55:58 -0700175# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
176# rerun kati every build. Kati will replay the $(file) command that generates it every build,
177# updating its timestamp.
178#
179# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
180# outside of the source tree.
Cole Faust9a106f32023-11-08 09:51:04 -0800181$(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 -0700182