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