blob: a2f59e63a841e3f08ce5e3620ee180389228cf55 [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.
14
Cole Faust8a7efaf2023-08-15 17:12:01 -070015load("//build/bazel/utils:schema_validation.bzl", "validate")
16
Cole Faust386b3742023-06-06 16:55:58 -070017# Partitions that get build system flag summaries
18_flag_partitions = [
19 "product",
20 "system",
21 "system_ext",
22 "vendor",
23]
24
Joe Onoratod6df20a2023-06-09 18:51:00 -070025ALL = ["all"]
26PRODUCT = ["product"]
27SYSTEM = ["system"]
28SYSTEM_EXT = ["system_ext"]
29VENDOR = ["vendor"]
Cole Faust386b3742023-06-06 16:55:58 -070030
Joe Onoratod6df20a2023-06-09 18:51:00 -070031_valid_types = ["NoneType", "bool", "list", "string", "int"]
32
Cole Faust8a7efaf2023-08-15 17:12:01 -070033_all_flags_schema = {
34 "type": "list",
35 "of": {
36 "type": "dict",
37 "required_keys": {
38 "name": {"type": "string"},
39 "partitions": {
40 "type": "list",
41 "of": {
42 "type": "string",
43 "choices": _flag_partitions + ["all"],
44 },
45 "unique": True,
46 },
47 "default": {
48 "or": [
49 {"type": t}
50 for t in _valid_types
51 ],
52 },
53 "declared_in": {"type": "string"},
54 },
55 },
56}
57
58_all_values_schema = {
59 "type": "list",
60 "of": {
61 "type": "dict",
62 "required_keys": {
63 "name": {"type": "string"},
64 "value": {
65 "or": [
66 {"type": t}
67 for t in _valid_types
68 ],
69 },
70 "set_in": {"type": "string"},
71 },
72 },
73}
74
Joe Onoratod6df20a2023-06-09 18:51:00 -070075def flag(name, partitions, default):
76 "Declare a flag."
77 if not partitions:
78 fail("At least 1 partition is required")
79 if not name.startswith("RELEASE_"):
80 fail("Release flag names must start with RELEASE_")
81 if " " in name or "\t" in name or "\n" in name:
82 fail("Flag names must not contain whitespace: \"" + name + "\"")
83 for partition in partitions:
84 if partition == "all":
85 if len(partitions) > 1:
86 fail("\"all\" can't be combined with other partitions: " + str(partitions))
87 elif partition not in _flag_partitions:
Cole Faust8a7efaf2023-08-15 17:12:01 -070088 fail("Invalid partition: " + partition + ", allowed partitions: " +
89 str(_flag_partitions))
Joe Onoratod6df20a2023-06-09 18:51:00 -070090 if type(default) not in _valid_types:
91 fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")")
92 return {
93 "name": name,
94 "partitions": partitions,
Cole Faust8a7efaf2023-08-15 17:12:01 -070095 "default": default,
Cole Faust386b3742023-06-06 16:55:58 -070096 }
Cole Faust386b3742023-06-06 16:55:58 -070097
Joe Onoratod6df20a2023-06-09 18:51:00 -070098def value(name, value):
99 "Define the flag value for a particular configuration."
100 return {
101 "name": name,
102 "value": value,
103 }
Cole Faust386b3742023-06-06 16:55:58 -0700104
Joe Onoratod6df20a2023-06-09 18:51:00 -0700105def _format_value(val):
106 "Format the starlark type correctly for make"
107 if type(val) == "NoneType":
108 return ""
109 elif type(val) == "bool":
110 return "true" if val else ""
Cole Faust386b3742023-06-06 16:55:58 -0700111 else:
Joe Onoratod6df20a2023-06-09 18:51:00 -0700112 return val
113
114def release_config(all_flags, all_values):
115 "Return the make variables that should be set for this release config."
Cole Faust8a7efaf2023-08-15 17:12:01 -0700116 validate(all_flags, _all_flags_schema)
117 validate(all_values, _all_values_schema)
118
Joe Onoratod6df20a2023-06-09 18:51:00 -0700119 # Validate flags
120 flag_names = []
121 for flag in all_flags:
122 if flag["name"] in flag_names:
123 fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"])
124 flag_names.append(flag["name"])
125
126 # Record which flags go on which partition
127 partitions = {}
128 for flag in all_flags:
129 for partition in flag["partitions"]:
130 if partition == "all":
Cole Faust8a7efaf2023-08-15 17:12:01 -0700131 if len(flag["partitions"]) > 1:
132 fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"]))
Joe Onoratod6df20a2023-06-09 18:51:00 -0700133 for partition in _flag_partitions:
134 partitions.setdefault(partition, []).append(flag["name"])
135 else:
136 partitions.setdefault(partition, []).append(flag["name"])
137
138 # Validate values
Joe Onorato1f655512023-06-12 23:29:25 -0700139 # TODO(joeo): Disallow duplicate values after we've split AOSP and vendor flags.
Joe Onoratod6df20a2023-06-09 18:51:00 -0700140 values = {}
141 for value in all_values:
142 if value["name"] not in flag_names:
143 fail(value["set_in"] + ": Value set for undeclared build flag: " + value["name"])
144 values[value["name"]] = value
145
146 # Collect values
147 result = {
148 "_ALL_RELEASE_FLAGS": sorted(flag_names),
149 }
150 for partition, names in partitions.items():
151 result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names
152 for flag in all_flags:
153 if flag["name"] in values:
154 val = values[flag["name"]]["value"]
155 set_in = values[flag["name"]]["set_in"]
Joe Onoratod6df20a2023-06-09 18:51:00 -0700156 else:
157 val = flag["default"]
158 set_in = flag["declared_in"]
159 val = _format_value(val)
160 result[flag["name"]] = val
161 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"]
162 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"])
163 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val
164 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"]
165 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in
Cole Faust386b3742023-06-06 16:55:58 -0700166
167 return result