blob: 0c088581340625e4b3b906e68333daaa2466c05a [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 Faust8a7efaf2023-08-15 17:12:01 -070018load("//build/bazel/utils:schema_validation.bzl", "validate")
19
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 },
58 },
59}
60
61_all_values_schema = {
62 "type": "list",
63 "of": {
64 "type": "dict",
65 "required_keys": {
66 "name": {"type": "string"},
67 "value": {
68 "or": [
69 {"type": t}
70 for t in _valid_types
71 ],
72 },
73 "set_in": {"type": "string"},
74 },
75 },
76}
77
Colin Cross93b43ef2023-11-06 21:32:03 +000078def flag(name, partitions, default):
LaMont Jonesfff3ee02023-10-12 20:00:32 +000079 """Declare a flag.
80
81 Args:
82 name: name of the flag
83 partitions: the partitions where this should be recorded.
84 default: the default value of the flag.
85
86 Returns:
87 A dictionary containing the flag declaration.
88 """
Joe Onoratod6df20a2023-06-09 18:51:00 -070089 if not partitions:
90 fail("At least 1 partition is required")
91 if not name.startswith("RELEASE_"):
92 fail("Release flag names must start with RELEASE_")
93 if " " in name or "\t" in name or "\n" in name:
94 fail("Flag names must not contain whitespace: \"" + name + "\"")
95 for partition in partitions:
96 if partition == "all":
97 if len(partitions) > 1:
98 fail("\"all\" can't be combined with other partitions: " + str(partitions))
99 elif partition not in _flag_partitions:
Cole Faust8a7efaf2023-08-15 17:12:01 -0700100 fail("Invalid partition: " + partition + ", allowed partitions: " +
101 str(_flag_partitions))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700102 if type(default) not in _valid_types:
103 fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")")
104 return {
105 "name": name,
106 "partitions": partitions,
Cole Faust8a7efaf2023-08-15 17:12:01 -0700107 "default": default,
Cole Faust386b3742023-06-06 16:55:58 -0700108 }
Cole Faust386b3742023-06-06 16:55:58 -0700109
Joe Onoratod6df20a2023-06-09 18:51:00 -0700110def value(name, value):
LaMont Jonesfff3ee02023-10-12 20:00:32 +0000111 """Define the flag value for a particular configuration.
112
113 Args:
114 name: The name of the flag.
115 value: The value for the flag.
116
117 Returns:
118 A dictionary containing the name and value to be used.
119 """
Joe Onoratod6df20a2023-06-09 18:51:00 -0700120 return {
121 "name": name,
122 "value": value,
123 }
Cole Faust386b3742023-06-06 16:55:58 -0700124
Joe Onoratod6df20a2023-06-09 18:51:00 -0700125def _format_value(val):
LaMont Jonesfff3ee02023-10-12 20:00:32 +0000126 """Format the starlark type correctly for make.
127
128 Args:
129 val: The value to format
130
131 Returns:
132 The value, formatted correctly for make.
133 """
Joe Onoratod6df20a2023-06-09 18:51:00 -0700134 if type(val) == "NoneType":
135 return ""
136 elif type(val) == "bool":
137 return "true" if val else ""
Cole Faust386b3742023-06-06 16:55:58 -0700138 else:
Joe Onoratod6df20a2023-06-09 18:51:00 -0700139 return val
140
141def release_config(all_flags, all_values):
LaMont Jonesfff3ee02023-10-12 20:00:32 +0000142 """Return the make variables that should be set for this release config.
143
144 Args:
145 all_flags: A list of flag objects (from flag() calls).
146 all_values: A list of value objects (from value() calls).
147
148 Returns:
149 A dictionary of {name: value} variables for make.
150 """
Cole Faust8a7efaf2023-08-15 17:12:01 -0700151 validate(all_flags, _all_flags_schema)
152 validate(all_values, _all_values_schema)
153
Joe Onoratod6df20a2023-06-09 18:51:00 -0700154 # Validate flags
155 flag_names = []
156 for flag in all_flags:
157 if flag["name"] in flag_names:
158 fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"])
159 flag_names.append(flag["name"])
160
161 # Record which flags go on which partition
162 partitions = {}
163 for flag in all_flags:
164 for partition in flag["partitions"]:
165 if partition == "all":
Cole Faust8a7efaf2023-08-15 17:12:01 -0700166 if len(flag["partitions"]) > 1:
167 fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"]))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700168 for partition in _flag_partitions:
169 partitions.setdefault(partition, []).append(flag["name"])
170 else:
171 partitions.setdefault(partition, []).append(flag["name"])
172
Colin Cross93b43ef2023-11-06 21:32:03 +0000173 # Validate values
174 # TODO(joeo): Disallow duplicate values after we've split AOSP and vendor flags.
Joe Onoratod6df20a2023-06-09 18:51:00 -0700175 values = {}
176 for value in all_values:
Colin Cross93b43ef2023-11-06 21:32:03 +0000177 if value["name"] not in flag_names:
178 fail(value["set_in"] + ": Value set for undeclared build flag: " + value["name"])
179 values[value["name"]] = value
Joe Onoratod6df20a2023-06-09 18:51:00 -0700180
181 # Collect values
182 result = {
183 "_ALL_RELEASE_FLAGS": sorted(flag_names),
184 }
185 for partition, names in partitions.items():
186 result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names
187 for flag in all_flags:
188 if flag["name"] in values:
189 val = values[flag["name"]]["value"]
190 set_in = values[flag["name"]]["set_in"]
Joe Onoratod6df20a2023-06-09 18:51:00 -0700191 else:
192 val = flag["default"]
193 set_in = flag["declared_in"]
194 val = _format_value(val)
195 result[flag["name"]] = val
196 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"]
197 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"])
198 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val
199 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"]
200 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in
Cole Faust386b3742023-06-06 16:55:58 -0700201
202 return result