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    else
150        # This is the first pass of product config.
151        $(eval include $(_flags_varmk))
152    endif
153    _used_files :=
154    ifeq (,$(_must_protobuf)$(RELEASE_BUILD_FLAGS_IN_PROTOBUF))
155        _use_protobuf :=
156    else
157        _base_all_release := all_release_configs-$(TARGET_PRODUCT)
158        $(call dist-for-goals,droid,\
159            $(_flags_dir)/$(_base_all_release).pb:build_flags/all_release_configs.pb \
160            $(_flags_dir)/$(_base_all_release).textproto:build_flags/all_release_configs.textproto \
161            $(_flags_dir)/$(_base_all_release).json:build_flags/all_release_configs.json \
162            $(_flags_dir)/inheritance_graph-$(TARGET_PRODUCT).dot:build_flags/inheritance_graph-$(TARGET_PRODUCT).dot \
163        )
164# These are always created, add an empty rule for them to keep ninja happy.
165$(_flags_dir)/inheritance_graph-$(TARGET_PRODUCT).dot:
166	: created by $(OUT_DIR)/release-config
167$(_flags_dir)/$(_base_all_release).pb $(_flags_dir)/$(_base_all_release).textproto $(_flags_dir)/$(_base_all_release).json:
168	: created by $(OUT_DIR)/release-config
169        _base_all_release :=
170    endif
171    _flags_dir:=
172    _flags_file:=
173    _flags_varmk:=
174endif
175ifeq (,$(_use_protobuf))
176    # The .mk files are the canonical source of truth.
177
178
179# Declare an alias release-config
180#
181# This should be used to declare a release as an alias of another, meaning no
182# release config files should be present.
183#
184# $1 config name
185# $2 release config for which it is an alias
186define alias-release-config
187    $(call _declare-release-config,$(1),,$(2),true)
188endef
189
190# Declare or extend a release-config.
191#
192# The order of processing is:
193# 1. Recursively apply any overridden release configs.  Only apply each config
194#    the first time we reach it.
195# 2. Apply any files for this release config, in the order they were added to
196#    the declaration.
197#
198# Example:
199#   With these declarations:
200#     $(declare-release-config foo, foo.scl)
201#     $(declare-release-config bar, bar.scl, foo)
202#     $(declare-release-config baz, baz.scl, bar)
203#     $(declare-release-config bif, bif.scl, foo baz)
204#     $(declare-release-config bop, bop.scl, bar baz)
205#
206#   TARGET_RELEASE:
207#     - bar will use: foo.scl bar.scl
208#     - baz will use: foo.scl bar.scl baz.scl
209#     - bif will use: foo.scl bar.scl baz.scl bif.scl
210#     - bop will use: foo.scl bar.scl baz.scl bop.scl
211#
212# $1 config name
213# $2 release config files
214# $3 overridden release config
215define declare-release-config
216    $(call _declare-release-config,$(1),$(2),$(3),)
217endef
218
219define _declare-release-config
220    $(if $(strip $(2)$(3)),,  \
221        $(error declare-release-config: config $(strip $(1)) must have release config files, override another release config, or both) \
222    )
223    $(if $(strip $(4)),$(eval _all_release_configs.$(strip $(1)).ALIAS := true))
224    $(eval ALL_RELEASE_CONFIGS_FOR_PRODUCT := $(sort $(ALL_RELEASE_CONFIGS_FOR_PRODUCT) $(strip $(1))))
225    $(if $(strip $(3)), \
226      $(if $(filter $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), $(strip $(3))),
227        $(if $(filter $(_all_release_configs.$(strip $(1)).OVERRIDES),$(strip $(3))),,
228          $(eval _all_release_configs.$(strip $(1)).OVERRIDES := $(_all_release_configs.$(strip $(1)).OVERRIDES) $(strip $(3)))), \
229        $(error No release config $(strip $(3))) \
230      ) \
231    )
232    $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included) $(_all_release_configs.$(strip $(1)).DECLARED_IN))
233    $(eval _all_release_configs.$(strip $(1)).FILES := $(_all_release_configs.$(strip $(1)).FILES) $(strip $(2)))
234endef
235
236# Include the config map files and populate _flag_declaration_files.
237# If the file is found more than once, only include it the first time.
238_flag_declaration_files :=
239_included_config_map_files :=
240$(foreach f, $(config_map_files), \
241    $(eval FLAG_DECLARATION_FILES:= ) \
242    $(if $(filter $(_included_config_map_files),$(f)),,\
243        $(eval _included := $(f)) \
244        $(eval include $(f)) \
245        $(eval _flag_declaration_files += $(FLAG_DECLARATION_FILES)) \
246        $(eval _included_config_map_files += $(f)) \
247    ) \
248)
249FLAG_DECLARATION_FILES :=
250
251# Verify that all inherited/overridden release configs are declared.
252$(foreach config,$(ALL_RELEASE_CONFIGS_FOR_PRODUCT),\
253  $(foreach r,$(all_release_configs.$(r).OVERRIDES),\
254    $(if $(strip $(_all_release_configs.$(r).FILES)$(_all_release_configs.$(r).OVERRIDES)),,\
255    $(error Release config $(config) [declared in: $(_all_release_configs.$(r).DECLARED_IN)] inherits from non-existent $(r).)\
256)))
257# Verify that alias configs do not have config files.
258$(foreach r,$(ALL_RELEASE_CONFIGS_FOR_PRODUCT),\
259  $(if $(_all_release_configs.$(r).ALIAS),$(if $(_all_release_configs.$(r).FILES),\
260    $(error Alias release config "$(r)" may not specify release config files $(_all_release_configs.$(r).FILES))\
261)))
262
263# Use makefiles
264endif
265
266ifeq ($(TARGET_RELEASE),)
267    # We allow some internal paths to explicitly set TARGET_RELEASE to the
268    # empty string.  For the most part, 'make' treats unset and empty string as
269    # the same.  But the following line differentiates, and will only assign
270    # if the variable was completely unset.
271    TARGET_RELEASE ?= was_unset
272    ifeq ($(TARGET_RELEASE),was_unset)
273        $(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))
274    endif
275    # Instead of leaving this string empty, we want to default to a valid
276    # setting.  Full builds coming through this path is a bug, but in case
277    # of such a bug, we want to at least get consistent, valid results.
278    TARGET_RELEASE = trunk_staging
279endif
280
281# During pass 1 of product config, using a non-existent release config is not an error.
282# We can safely assume that we are doing pass 1 if DUMP_MANY_VARS=="PRODUCT_RELEASE_CONFIG_MAPS".
283ifneq (,$(_final_product_config_pass))
284    ifeq ($(filter $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), $(TARGET_RELEASE)),)
285        $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(ALL_RELEASE_CONFIGS_FOR_PRODUCT))
286    endif
287endif
288
289ifeq (,$(_use_protobuf))
290# Choose flag files
291# Don't sort this, use it in the order they gave us.
292# Do allow duplicate entries, retaining only the first usage.
293flag_value_files :=
294
295# Apply overrides recursively
296#
297# $1 release config that we override
298applied_releases :=
299define _apply-release-config-overrides
300$(foreach r,$(1), \
301  $(if $(filter $(r),$(applied_releases)),, \
302    $(foreach o,$(_all_release_configs.$(r).OVERRIDES),$(call _apply-release-config-overrides,$(o)))\
303    $(eval applied_releases += $(r))\
304    $(foreach f,$(_all_release_configs.$(r).FILES), \
305      $(if $(filter $(f),$(flag_value_files)),,$(eval flag_value_files += $(f)))\
306    )\
307  )\
308)
309endef
310$(call _apply-release-config-overrides,$(TARGET_RELEASE))
311# Unset variables so they can't use them
312define declare-release-config
313$(error declare-release-config can only be called from inside release_config_map.mk files)
314endef
315define _apply-release-config-overrides
316$(error invalid use of apply-release-config-overrides)
317endef
318
319# use makefiles
320endif
321
322# TODO: Remove this check after enough people have sourced lunch that we don't
323# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
324ifneq ($(CALLED_FROM_SETUP),true)
325define TARGET_RELEASE
326$(error TARGET_RELEASE may not be accessed directly. Use individual flags.)
327endef
328else
329TARGET_RELEASE:=
330endif
331.KATI_READONLY := TARGET_RELEASE
332
333ifeq (,$(_use_protobuf))
334$(foreach config, $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), \
335    $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
336    $(eval _all_release_configs.$(config).FILES:= ) \
337)
338applied_releases:=
339# use makefiles
340endif
341config_map_files:=
342protobuf_map_files:=
343
344
345ifeq (,$(_use_protobuf))
346# -----------------------------------------------------------------
347# Flag declarations and values
348# -----------------------------------------------------------------
349# This part is in starlark.  We generate a root starlark file that loads
350# all of the flags declaration files that we found, and the flag_value_files
351# that we chose from the config map above.  Then we run that, and load the
352# results of that into the make environment.
353
354# _flag_declaration_files is the combined list of FLAG_DECLARATION_FILES set by
355# release_config_map.mk files above.
356
357# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
358# contains the result of the above wildcards for the starlark code to use.
359filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
360_c:=load("//build/make/core/release_config.scl", "release_config")
361_c+=$(newline)def add(d, k, v):
362_c+=$(newline)$(space)d = dict(d)
363_c+=$(newline)$(space)d[k] = v
364_c+=$(newline)$(space)return d
365_c+=$(foreach f,$(_flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
366_c+=$(newline)all_flags = [] $(foreach f,$(_flag_declaration_files),+ [add(x, "declared_in", "$(f)") for x in flags_$(call filename_to_starlark,$(f))])
367_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
368_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
369_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
370$(file >$(OUT_DIR)/release_config_entrypoint.scl,$(_c))
371_c:=
372filename_to_starlark:=
373
374# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
375# rerun kati every build. Kati will replay the $(file) command that generates it every build,
376# updating its timestamp.
377#
378# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
379# outside of the source tree.
380$(call run-starlark,$(OUT_DIR)/release_config_entrypoint.scl,$(OUT_DIR)/release_config_entrypoint.scl,--allow_external_entrypoint)
381
382# use makefiles
383endif
384_can_protobuf :=
385_must_protobuf :=
386_use_protobuf :=
387
388