blob: 662d15507ac520dffe51825e9e8bda53cbbb8734 [file] [log] [blame]
Cole Faust386b3742023-06-06 16:55:58 -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.
LaMont Jonesfff3ee02023-10-12 20:00:32 +000014"""
15Export build flags (with values) to make.
16"""
Cole Faust386b3742023-06-06 16:55:58 -070017
Cole Fausta44c7bd2023-11-07 15:12:45 -080018load("//build/bazel/utils:schema_validation.scl", "validate")
Cole Faust8a7efaf2023-08-15 17:12:01 -070019
Cole Faust386b3742023-06-06 16:55:58 -070020# Partitions that get build system flag summaries
21_flag_partitions = [
22 "product",
23 "system",
24 "system_ext",
25 "vendor",
26]
27
Joe Onoratod6df20a2023-06-09 18:51:00 -070028ALL = ["all"]
29PRODUCT = ["product"]
30SYSTEM = ["system"]
31SYSTEM_EXT = ["system_ext"]
32VENDOR = ["vendor"]
Cole Faust386b3742023-06-06 16:55:58 -070033
Joe Onoratod6df20a2023-06-09 18:51:00 -070034_valid_types = ["NoneType", "bool", "list", "string", "int"]
35
Cole Faust8a7efaf2023-08-15 17:12:01 -070036_all_flags_schema = {
37 "type": "list",
38 "of": {
39 "type": "dict",
40 "required_keys": {
41 "name": {"type": "string"},
42 "partitions": {
43 "type": "list",
44 "of": {
45 "type": "string",
46 "choices": _flag_partitions + ["all"],
47 },
48 "unique": True,
49 },
50 "default": {
51 "or": [
52 {"type": t}
53 for t in _valid_types
54 ],
55 },
56 "declared_in": {"type": "string"},
57 },
LaMont Jones2dae3d62023-11-06 22:15:06 +000058 "optional_keys": {
59 "appends": {
60 "type": "bool",
61 },
62 },
Cole Faust8a7efaf2023-08-15 17:12:01 -070063 },
64}
65
66_all_values_schema = {
67 "type": "list",
68 "of": {
69 "type": "dict",
70 "required_keys": {
71 "name": {"type": "string"},
72 "value": {
73 "or": [
74 {"type": t}
75 for t in _valid_types
76 ],
77 },
78 "set_in": {"type": "string"},
79 },
80 },
81}
82
Cole Faustb9b54392023-11-09 12:40:38 -080083def flag(name, partitions, default, *, appends = False):
LaMont Jonesfff3ee02023-10-12 20:00:32 +000084 """Declare a flag.
85
86 Args:
87 name: name of the flag
88 partitions: the partitions where this should be recorded.
89 default: the default value of the flag.
LaMont Jones2dae3d62023-11-06 22:15:06 +000090 appends: Whether new values should be append (not replace) the old.
LaMont Jonesfff3ee02023-10-12 20:00:32 +000091
92 Returns:
93 A dictionary containing the flag declaration.
94 """
Joe Onoratod6df20a2023-06-09 18:51:00 -070095 if not partitions:
96 fail("At least 1 partition is required")
97 if not name.startswith("RELEASE_"):
98 fail("Release flag names must start with RELEASE_")
99 if " " in name or "\t" in name or "\n" in name:
100 fail("Flag names must not contain whitespace: \"" + name + "\"")
101 for partition in partitions:
102 if partition == "all":
103 if len(partitions) > 1:
104 fail("\"all\" can't be combined with other partitions: " + str(partitions))
105 elif partition not in _flag_partitions:
Cole Faust8a7efaf2023-08-15 17:12:01 -0700106 fail("Invalid partition: " + partition + ", allowed partitions: " +
107 str(_flag_partitions))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700108 if type(default) not in _valid_types:
109 fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")")
110 return {
111 "name": name,
112 "partitions": partitions,
Cole Faust8a7efaf2023-08-15 17:12:01 -0700113 "default": default,
LaMont Jones2dae3d62023-11-06 22:15:06 +0000114 "appends": appends,
Cole Faust386b3742023-06-06 16:55:58 -0700115 }
Cole Faust386b3742023-06-06 16:55:58 -0700116
Joe Onoratod6df20a2023-06-09 18:51:00 -0700117def value(name, value):
LaMont Jonesfff3ee02023-10-12 20:00:32 +0000118 """Define the flag value for a particular configuration.
119
120 Args:
121 name: The name of the flag.
122 value: The value for the flag.
123
124 Returns:
125 A dictionary containing the name and value to be used.
126 """
Joe Onoratod6df20a2023-06-09 18:51:00 -0700127 return {
128 "name": name,
129 "value": value,
130 }
Cole Faust386b3742023-06-06 16:55:58 -0700131
Joe Onoratod6df20a2023-06-09 18:51:00 -0700132def _format_value(val):
LaMont Jonesfff3ee02023-10-12 20:00:32 +0000133 """Format the starlark type correctly for make.
134
135 Args:
136 val: The value to format
137
138 Returns:
139 The value, formatted correctly for make.
140 """
Joe Onoratod6df20a2023-06-09 18:51:00 -0700141 if type(val) == "NoneType":
142 return ""
143 elif type(val) == "bool":
144 return "true" if val else ""
Cole Faust386b3742023-06-06 16:55:58 -0700145 else:
Joe Onoratod6df20a2023-06-09 18:51:00 -0700146 return val
147
148def release_config(all_flags, all_values):
LaMont Jonesfff3ee02023-10-12 20:00:32 +0000149 """Return the make variables that should be set for this release config.
150
151 Args:
152 all_flags: A list of flag objects (from flag() calls).
153 all_values: A list of value objects (from value() calls).
154
155 Returns:
156 A dictionary of {name: value} variables for make.
157 """
Cole Faust8a7efaf2023-08-15 17:12:01 -0700158 validate(all_flags, _all_flags_schema)
159 validate(all_values, _all_values_schema)
160
Joe Onoratod6df20a2023-06-09 18:51:00 -0700161 # Validate flags
162 flag_names = []
LaMont Jones2dae3d62023-11-06 22:15:06 +0000163 flags_dict = {}
Joe Onoratod6df20a2023-06-09 18:51:00 -0700164 for flag in all_flags:
165 if flag["name"] in flag_names:
166 fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"])
167 flag_names.append(flag["name"])
LaMont Jones2dae3d62023-11-06 22:15:06 +0000168 flags_dict[flag["name"]] = flag
Joe Onoratod6df20a2023-06-09 18:51:00 -0700169
170 # Record which flags go on which partition
171 partitions = {}
172 for flag in all_flags:
173 for partition in flag["partitions"]:
174 if partition == "all":
Cole Faust8a7efaf2023-08-15 17:12:01 -0700175 if len(flag["partitions"]) > 1:
176 fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"]))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700177 for partition in _flag_partitions:
178 partitions.setdefault(partition, []).append(flag["name"])
179 else:
180 partitions.setdefault(partition, []).append(flag["name"])
181
LaMont Jones2dae3d62023-11-06 22:15:06 +0000182 # Generate final values.
183 # Only declared flags may have a value.
Joe Onoratod6df20a2023-06-09 18:51:00 -0700184 values = {}
185 for value in all_values:
LaMont Jones2dae3d62023-11-06 22:15:06 +0000186 name = value["name"]
187 if name not in flag_names:
188 fail(value["set_in"] + ": Value set for undeclared build flag: " + name)
189 if flags_dict[name]["appends"]:
190 if name in values:
191 values[name]["value"] += " " + value["value"]
192 values[name]["set_in"] += " " + value["set_in"]
193 else:
194 values[name] = value
195 else:
196 values[name] = value
Joe Onoratod6df20a2023-06-09 18:51:00 -0700197
198 # Collect values
199 result = {
200 "_ALL_RELEASE_FLAGS": sorted(flag_names),
201 }
202 for partition, names in partitions.items():
203 result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names
204 for flag in all_flags:
205 if flag["name"] in values:
206 val = values[flag["name"]]["value"]
207 set_in = values[flag["name"]]["set_in"]
Joe Onoratod6df20a2023-06-09 18:51:00 -0700208 else:
209 val = flag["default"]
210 set_in = flag["declared_in"]
211 val = _format_value(val)
212 result[flag["name"]] = val
213 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"]
214 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"])
215 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val
216 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"]
217 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in
Cole Faust386b3742023-06-06 16:55:58 -0700218
219 return result