blob: 3cd8b4103baa82f81076943c71a54e5894a3f4cd [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# -----------------------------------------------------------------
19# Do this first, because we're going to unset TARGET_RELEASE before
20# including anyone, so they don't start making conditionals based on it.
21# This logic is in make because starlark doesn't understand optional
22# vendor files.
23
24# If this is a google source tree, restrict it to only the one file
25# which has OWNERS control. If it isn't let others define their own.
26# TODO: Remove wildcard for build/release one when all branch manifests
27# have updated.
28config_map_files := $(wildcard build/release/release_config_map.mk) \
29 $(if $(wildcard vendor/google/release/release_config_map.mk), \
30 vendor/google/release/release_config_map.mk, \
31 $(sort \
32 $(wildcard device/*/release/release_config_map.mk) \
33 $(wildcard device/*/*/release/release_config_map.mk) \
34 $(wildcard vendor/*/release/release_config_map.mk) \
35 $(wildcard vendor/*/*/release/release_config_map.mk) \
36 ) \
37 )
38
39# $1 config name
40# $2 release config files
41define declare-release-config
42 $(eval # No duplicates)
43 $(if $(filter $(_all_release_configs), $(strip $(1))), \
44 $(error declare-release-config: config $(strip $(1)) declared in: $(_included) Previously declared here: $(_all_release_configs.$(strip $(1)).DECLARED_IN)) \
45 )
46 $(eval # Must have release config files)
47 $(if $(strip $(2)),, \
48 $(error declare-release-config: config $(strip $(1)) must have release config files) \
49 )
50 $(eval _all_release_configs := $(sort $(_all_release_configs) $(strip $(1))))
51 $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included))
52 $(eval _all_release_configs.$(strip $(1)).FILES := $(strip $(2)))
53endef
54
55# Include the config map files
56$(foreach f, $(config_map_files), \
57 $(eval _included := $(f)) \
58 $(eval include $(f)) \
59)
60
61# If TARGET_RELEASE is set, fail if there is no matching release config
62# If it isn't set, no release config files will be included and all flags
63# will get their default values.
64ifneq ($(TARGET_RELEASE),)
65ifeq ($(filter $(_all_release_configs), $(TARGET_RELEASE)),)
66 $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(_all_release_configs))
67else
68 # Choose flag files
69 # Don't sort this, use it in the order they gave us.
70 flag_value_files := $(_all_release_configs.$(TARGET_RELEASE).FILES)
71endif
72else
73# Useful for finding scripts etc that aren't passing or setting TARGET_RELEASE
74ifneq ($(FAIL_IF_NO_RELEASE_CONFIG),)
75 $(error FAIL_IF_NO_RELEASE_CONFIG was set and TARGET_RELEASE was not)
76endif
77flag_value_files :=
78endif
79
80# Unset variables so they can't use them
81define declare-release-config
82$(error declare-release-config can only be called from inside release_config_map.mk files)
83endef
84
85# TODO: Remove this check after enough people have sourced lunch that we don't
86# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
87ifneq ($(CALLED_FROM_SETUP),true)
88define TARGET_RELEASE
89$(error TARGET_RELEASE may not be accessed directly. Use individual flags.)
90endef
91else
92TARGET_RELEASE:=
93endif
94.KATI_READONLY := TARGET_RELEASE
95
96
97$(foreach config, $(_all_release_configs), \
98 $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
99 $(eval _all_release_configs.$(config).FILES:= ) \
100)
101_all_release_configs:=
102config_map_files:=
103
104
105# -----------------------------------------------------------------
106# Flag declarations and values
107# -----------------------------------------------------------------
108# This part is in starlark. We generate a root starlark file that loads
109# all of the flags declaration files that we found, and the flag_value_files
110# that we chose from the config map above. Then we run that, and load the
111# results of that into the make environment.
112
Joe Onorato964f4012023-05-06 12:29:01 -0700113# If this is a google source tree, restrict it to only the one file
114# which has OWNERS control. If it isn't let others define their own.
Joe Onorato0d1a9812023-05-07 13:40:25 -0700115# TODO: Remove wildcard for build/release one when all branch manifests
116# have updated.
Cole Faust386b3742023-06-06 16:55:58 -0700117flag_declaration_files := $(wildcard build/release/build_flags.bzl) \
118 $(if $(wildcard vendor/google/release/build_flags.bzl), \
119 vendor/google/release/build_flags.bzl, \
Joe Onorato964f4012023-05-06 12:29:01 -0700120 $(sort \
Cole Faust386b3742023-06-06 16:55:58 -0700121 $(wildcard device/*/release/build_flags.bzl) \
122 $(wildcard device/*/*/release/build_flags.bzl) \
123 $(wildcard vendor/*/release/build_flags.bzl) \
124 $(wildcard vendor/*/*/release/build_flags.bzl) \
125 ) \
126 )
Joe Onoratod6df20a2023-06-09 18:51:00 -0700127
Joe Onorato964f4012023-05-06 12:29:01 -0700128
Cole Faust386b3742023-06-06 16:55:58 -0700129# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
130# contains the result of the above wildcards for the starlark code to use.
131filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
132_c:=load("//build/make/core/release_config.bzl", "release_config")
Joe Onoratod6df20a2023-06-09 18:51:00 -0700133_c+=$(newline)def add(d, k, v):
134_c+=$(newline)$(space)d = dict(d)
135_c+=$(newline)$(space)d[k] = v
136_c+=$(newline)$(space)return d
137_c+=$(foreach f,$(flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
138_c+=$(newline)all_flags = [] $(foreach f,$(flag_declaration_files),+ [add(x, "declared_in", "$(f)") for x in flags_$(call filename_to_starlark,$(f))])
139_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
140_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
141_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
Cole Faust386b3742023-06-06 16:55:58 -0700142$(file >$(OUT_DIR)/release_config_entrypoint.bzl,$(_c))
143_c:=
144filename_to_starlark:=
Joe Onorato964f4012023-05-06 12:29:01 -0700145
Cole Faust386b3742023-06-06 16:55:58 -0700146# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
147# rerun kati every build. Kati will replay the $(file) command that generates it every build,
148# updating its timestamp.
149#
150# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
151# outside of the source tree.
152$(call run-starlark,$(OUT_DIR)/release_config_entrypoint.bzl,$(OUT_DIR)/release_config_entrypoint.bzl,--allow_external_entrypoint)
Joe Onorato7b578d32023-05-19 09:13:36 -0700153