• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# -----------------------------------------------------------------
17# Determine which pass this is.
18# -----------------------------------------------------------------
19# On the first pass, we are asked for only PRODUCT_RELEASE_CONFIG_MAPS,
20# on the second pass, we are asked for whatever else is wanted.
21_final_product_config_pass:=
22ifneq (PRODUCT_RELEASE_CONFIG_MAPS,$(DUMP_MANY_VARS))
23    _final_product_config_pass:=true
24endif
25
26# -----------------------------------------------------------------
27# Choose the flag files
28# -----------------------------------------------------------------
29# Release configs are defined in reflease_config_map files, which map
30# the short name (e.g. -next) used in lunch to the starlark files
31# defining the build flag values.
32#
33# (If you're thinking about aconfig flags, there is one build flag,
34# RELEASE_ACONFIG_VALUE_SETS, that sets which aconfig_value_set
35# module to use to set the aconfig flag values.)
36#
37# The short release config names *can* appear multiple times, to allow
38# for AOSP and vendor specific flags under the same name, but the
39# individual flag values must appear in exactly one config.  Vendor
40# does not override AOSP, or anything like that.  This is because
41# vendor code usually includes prebuilts, and having vendor compile
42# with different flags from AOSP increases the likelihood of flag
43# mismatch.
44
45# Do this first, because we're going to unset TARGET_RELEASE before
46# including anyone, so they don't start making conditionals based on it.
47# This logic is in make because starlark doesn't understand optional
48# vendor files.
49
50# If this is a google source tree, restrict it to only the one file
51# which has OWNERS control.  If it isn't let others define their own.
52config_map_files := $(wildcard build/release/release_config_map.mk) \
53    $(wildcard vendor/google_shared/build/release/release_config_map.mk) \
54    $(if $(wildcard vendor/google/release/release_config_map.mk), \
55        vendor/google/release/release_config_map.mk, \
56        $(sort \
57            $(wildcard device/*/release/release_config_map.mk) \
58            $(wildcard device/*/*/release/release_config_map.mk) \
59            $(wildcard vendor/*/release/release_config_map.mk) \
60            $(wildcard vendor/*/*/release/release_config_map.mk) \
61        ) \
62    )
63
64protobuf_map_files := build/release/release_config_map.textproto \
65    $(wildcard vendor/google_shared/build/release/release_config_map.textproto) \
66    $(if $(wildcard vendor/google/release/release_config_map.textproto), \
67        vendor/google/release/release_config_map.textproto, \
68        $(sort \
69            $(wildcard device/*/release/release_config_map.textproto) \
70            $(wildcard device/*/*/release/release_config_map.textproto) \
71            $(wildcard vendor/*/release/release_config_map.textproto) \
72            $(wildcard vendor/*/*/release/release_config_map.textproto) \
73        ) \
74    )
75
76# Remove support for the legacy approach.
77_must_protobuf := true
78
79# PRODUCT_RELEASE_CONFIG_MAPS is set by Soong using an initial run of product
80# config to capture only the list of config maps needed by the build.
81# Keep them in the order provided, but remove duplicates.
82# Treat .mk and .textproto as equal for duplicate elimination, but force
83# protobuf if any PRODUCT_RELEASE_CONFIG_MAPS specify .textproto.
84$(foreach map,$(PRODUCT_RELEASE_CONFIG_MAPS), \
85    $(if $(filter $(basename $(map)),$(basename $(config_map_files))),, \
86        $(eval config_map_files += $(map))) \
87    $(if $(filter $(basename $(map)).textproto,$(map)),$(eval _must_protobuf := true)) \
88)
89
90
91# If we are missing the textproto version of any of $(config_map_files), we cannot use protobuf.
92_can_protobuf := true
93$(foreach map,$(config_map_files), \
94    $(if $(wildcard $(basename $(map)).textproto),,$(eval _can_protobuf :=)) \
95)
96# If we are missing the mk version of any of $(protobuf_map_files), we must use protobuf.
97$(foreach map,$(protobuf_map_files), \
98    $(if $(wildcard $(basename $(map)).mk),,$(eval _must_protobuf := true)) \
99)
100
101ifneq (,$(_must_protobuf))
102    ifeq (,$(_can_protobuf))
103        # We must use protobuf, but we cannot use protobuf.
104        $(error release config is a mixture of .scl and .textproto)
105    endif
106endif
107
108_use_protobuf :=
109ifneq (,$(_must_protobuf))
110    _use_protobuf := true
111else
112    ifneq ($(_can_protobuf),)
113        # Determine the default
114        $(foreach map,$(config_map_files), \
115            $(if $(wildcard $(dir $(map))/build_config/DEFAULT=proto),$(eval _use_protobuf := true)) \
116            $(if $(wildcard $(dir $(map))/build_config/DEFAULT=make),$(eval _use_protobuf := )) \
117        )
118        # Update for this specific release config only (no inheritance).
119        $(foreach map,$(config_map_files), \
120            $(if $(wildcard $(dir $(map))/build_config/$(TARGET_RELEASE)=proto),$(eval _use_protobuf := true)) \
121            $(if $(wildcard $(dir $(map))/build_config/$(TARGET_RELEASE)=make),$(eval _use_protobuf := )) \
122        )
123    endif
124endif
125
126ifneq (,$(_use_protobuf))
127    # The .textproto files are the canonical source of truth.
128    _args := $(foreach map,$(config_map_files), --map $(map) )
129    ifneq (,$(_must_protobuf))
130        # Disable the build flag in release-config.
131        _args += --guard=false
132    endif
133    _args += --allow-missing=true
134    ifneq (,$(TARGET_PRODUCT))
135        _args += --product $(TARGET_PRODUCT)
136    endif
137    _flags_dir:=$(OUT_DIR)/soong/release-config
138    _flags_file:=$(_flags_dir)/release_config-$(TARGET_PRODUCT)-$(TARGET_RELEASE).vars
139    # release-config generates $(_flags_varmk)
140    _flags_varmk:=$(_flags_file:.vars=.varmk)
141    $(shell $(OUT_DIR)/release-config $(_args) >$(OUT_DIR)/release-config.out && touch -t 200001010000 $(_flags_varmk))
142    $(if $(filter-out 0,$(.SHELLSTATUS)),$(error release-config failed to run))
143    ifneq (,$(_final_product_config_pass))
144        # Save the final version of the config.
145        $(shell if ! cmp --quiet $(_flags_varmk) $(_flags_file); then cp $(_flags_varmk) $(_flags_file); fi)
146        # This will also set ALL_RELEASE_CONFIGS_FOR_PRODUCT and _used_files for us.
147        $(eval include $(_flags_file))
148        $(KATI_extra_file_deps $(OUT_DIR)/release-config $(protobuf_map_files) $(_flags_file))
149        ifneq (,$(_disallow_lunch_use))
150            $(error Release config ${TARGET_RELEASE} is disallowed for build.  Please use one of: $(ALL_RELEASE_CONFIGS_FOR_PRODUCT))
151        endif
152    else
153        # This is the first pass of product config.
154        $(eval include $(_flags_varmk))
155    endif
156    _used_files :=
157    ifeq (,$(_must_protobuf)$(RELEASE_BUILD_FLAGS_IN_PROTOBUF))
158        _use_protobuf :=
159    endif
160    _flags_dir:=
161    _flags_file:=
162    _flags_varmk:=
163endif
164ifeq (,$(_use_protobuf))
165    # The .mk files are the canonical source of truth.
166
167
168# Declare an alias release-config
169#
170# This should be used to declare a release as an alias of another, meaning no
171# release config files should be present.
172#
173# $1 config name
174# $2 release config for which it is an alias
175define alias-release-config
176    $(call _declare-release-config,$(1),,$(2),true)
177endef
178
179# Declare or extend a release-config.
180#
181# The order of processing is:
182# 1. Recursively apply any overridden release configs.  Only apply each config
183#    the first time we reach it.
184# 2. Apply any files for this release config, in the order they were added to
185#    the declaration.
186#
187# Example:
188#   With these declarations:
189#     $(declare-release-config foo, foo.scl)
190#     $(declare-release-config bar, bar.scl, foo)
191#     $(declare-release-config baz, baz.scl, bar)
192#     $(declare-release-config bif, bif.scl, foo baz)
193#     $(declare-release-config bop, bop.scl, bar baz)
194#
195#   TARGET_RELEASE:
196#     - bar will use: foo.scl bar.scl
197#     - baz will use: foo.scl bar.scl baz.scl
198#     - bif will use: foo.scl bar.scl baz.scl bif.scl
199#     - bop will use: foo.scl bar.scl baz.scl bop.scl
200#
201# $1 config name
202# $2 release config files
203# $3 overridden release config
204define declare-release-config
205    $(call _declare-release-config,$(1),$(2),$(3),)
206endef
207
208define _declare-release-config
209    $(if $(strip $(2)$(3)),,  \
210        $(error declare-release-config: config $(strip $(1)) must have release config files, override another release config, or both) \
211    )
212    $(if $(strip $(4)),$(eval _all_release_configs.$(strip $(1)).ALIAS := true))
213    $(eval ALL_RELEASE_CONFIGS_FOR_PRODUCT := $(sort $(ALL_RELEASE_CONFIGS_FOR_PRODUCT) $(strip $(1))))
214    $(if $(strip $(3)), \
215      $(if $(filter $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), $(strip $(3))),
216        $(if $(filter $(_all_release_configs.$(strip $(1)).OVERRIDES),$(strip $(3))),,
217          $(eval _all_release_configs.$(strip $(1)).OVERRIDES := $(_all_release_configs.$(strip $(1)).OVERRIDES) $(strip $(3)))), \
218        $(error No release config $(strip $(3))) \
219      ) \
220    )
221    $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included) $(_all_release_configs.$(strip $(1)).DECLARED_IN))
222    $(eval _all_release_configs.$(strip $(1)).FILES := $(_all_release_configs.$(strip $(1)).FILES) $(strip $(2)))
223endef
224
225# Include the config map files and populate _flag_declaration_files.
226# If the file is found more than once, only include it the first time.
227_flag_declaration_files :=
228_included_config_map_files :=
229$(foreach f, $(config_map_files), \
230    $(eval FLAG_DECLARATION_FILES:= ) \
231    $(if $(filter $(_included_config_map_files),$(f)),,\
232        $(eval _included := $(f)) \
233        $(eval include $(f)) \
234        $(eval _flag_declaration_files += $(FLAG_DECLARATION_FILES)) \
235        $(eval _included_config_map_files += $(f)) \
236    ) \
237)
238FLAG_DECLARATION_FILES :=
239
240# Verify that all inherited/overridden release configs are declared.
241$(foreach config,$(ALL_RELEASE_CONFIGS_FOR_PRODUCT),\
242  $(foreach r,$(all_release_configs.$(r).OVERRIDES),\
243    $(if $(strip $(_all_release_configs.$(r).FILES)$(_all_release_configs.$(r).OVERRIDES)),,\
244    $(error Release config $(config) [declared in: $(_all_release_configs.$(r).DECLARED_IN)] inherits from non-existent $(r).)\
245)))
246# Verify that alias configs do not have config files.
247$(foreach r,$(ALL_RELEASE_CONFIGS_FOR_PRODUCT),\
248  $(if $(_all_release_configs.$(r).ALIAS),$(if $(_all_release_configs.$(r).FILES),\
249    $(error Alias release config "$(r)" may not specify release config files $(_all_release_configs.$(r).FILES))\
250)))
251
252# Use makefiles
253endif
254
255ifeq ($(TARGET_RELEASE),)
256    # We allow some internal paths to explicitly set TARGET_RELEASE to the
257    # empty string.  For the most part, 'make' treats unset and empty string as
258    # the same.  But the following line differentiates, and will only assign
259    # if the variable was completely unset.
260    TARGET_RELEASE ?= was_unset
261    ifeq ($(TARGET_RELEASE),was_unset)
262        $(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_FOR_PRODUCT))
263    endif
264    # Instead of leaving this string empty, we want to default to a valid
265    # setting.  Full builds coming through this path is a bug, but in case
266    # of such a bug, we want to at least get consistent, valid results.
267    TARGET_RELEASE = trunk_staging
268endif
269
270# During pass 1 of product config, using a non-existent release config is not an error.
271# We can safely assume that we are doing pass 1 if DUMP_MANY_VARS=="PRODUCT_RELEASE_CONFIG_MAPS".
272ifneq (,$(_final_product_config_pass))
273    ifeq ($(filter $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), $(TARGET_RELEASE)),)
274        $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(ALL_RELEASE_CONFIGS_FOR_PRODUCT))
275    endif
276endif
277
278ifeq (,$(_use_protobuf))
279# Choose flag files
280# Don't sort this, use it in the order they gave us.
281# Do allow duplicate entries, retaining only the first usage.
282flag_value_files :=
283
284# Apply overrides recursively
285#
286# $1 release config that we override
287applied_releases :=
288define _apply-release-config-overrides
289$(foreach r,$(1), \
290  $(if $(filter $(r),$(applied_releases)),, \
291    $(foreach o,$(_all_release_configs.$(r).OVERRIDES),$(call _apply-release-config-overrides,$(o)))\
292    $(eval applied_releases += $(r))\
293    $(foreach f,$(_all_release_configs.$(r).FILES), \
294      $(if $(filter $(f),$(flag_value_files)),,$(eval flag_value_files += $(f)))\
295    )\
296  )\
297)
298endef
299$(call _apply-release-config-overrides,$(TARGET_RELEASE))
300# Unset variables so they can't use them
301define declare-release-config
302$(error declare-release-config can only be called from inside release_config_map.mk files)
303endef
304define _apply-release-config-overrides
305$(error invalid use of apply-release-config-overrides)
306endef
307
308# use makefiles
309endif
310
311# TODO: Remove this check after enough people have sourced lunch that we don't
312# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
313ifneq ($(CALLED_FROM_SETUP),true)
314define TARGET_RELEASE
315$(error TARGET_RELEASE may not be accessed directly. Use individual flags.)
316endef
317else
318TARGET_RELEASE:=
319endif
320.KATI_READONLY := TARGET_RELEASE
321
322ifeq (,$(_use_protobuf))
323$(foreach config, $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), \
324    $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
325    $(eval _all_release_configs.$(config).FILES:= ) \
326)
327applied_releases:=
328# use makefiles
329endif
330config_map_files:=
331protobuf_map_files:=
332
333
334ifeq (,$(_use_protobuf))
335# -----------------------------------------------------------------
336# Flag declarations and values
337# -----------------------------------------------------------------
338# This part is in starlark.  We generate a root starlark file that loads
339# all of the flags declaration files that we found, and the flag_value_files
340# that we chose from the config map above.  Then we run that, and load the
341# results of that into the make environment.
342
343# _flag_declaration_files is the combined list of FLAG_DECLARATION_FILES set by
344# release_config_map.mk files above.
345
346# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
347# contains the result of the above wildcards for the starlark code to use.
348filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
349_c:=load("//build/make/core/release_config.scl", "release_config")
350_c+=$(newline)def add(d, k, v):
351_c+=$(newline)$(space)d = dict(d)
352_c+=$(newline)$(space)d[k] = v
353_c+=$(newline)$(space)return d
354_c+=$(foreach f,$(_flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
355_c+=$(newline)all_flags = [] $(foreach f,$(_flag_declaration_files),+ [add(x, "declared_in", "$(f)") for x in flags_$(call filename_to_starlark,$(f))])
356_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
357_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
358_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
359$(file >$(OUT_DIR)/release_config_entrypoint.scl,$(_c))
360_c:=
361filename_to_starlark:=
362
363# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
364# rerun kati every build. Kati will replay the $(file) command that generates it every build,
365# updating its timestamp.
366#
367# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
368# outside of the source tree.
369$(call run-starlark,$(OUT_DIR)/release_config_entrypoint.scl,$(OUT_DIR)/release_config_entrypoint.scl,--allow_external_entrypoint)
370
371# use makefiles
372endif
373_can_protobuf :=
374_must_protobuf :=
375_use_protobuf :=
376
377