Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 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. |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 14 | """ |
| 15 | Export build flags (with values) to make. |
| 16 | """ |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 17 | |
Cole Faust | a44c7bd | 2023-11-07 15:12:45 -0800 | [diff] [blame] | 18 | load("//build/bazel/utils:schema_validation.scl", "validate") |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 19 | |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 20 | # Partitions that get build system flag summaries |
| 21 | _flag_partitions = [ |
| 22 | "product", |
| 23 | "system", |
| 24 | "system_ext", |
| 25 | "vendor", |
| 26 | ] |
| 27 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 28 | ALL = ["all"] |
| 29 | PRODUCT = ["product"] |
| 30 | SYSTEM = ["system"] |
| 31 | SYSTEM_EXT = ["system_ext"] |
| 32 | VENDOR = ["vendor"] |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 33 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 34 | _valid_types = ["NoneType", "bool", "list", "string", "int"] |
| 35 | |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 36 | _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 | }, |
LaMont Jones | 9ee098e | 2024-03-20 13:57:50 -0700 | [diff] [blame] | 56 | "origin": {"type": "string"}, |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 57 | "declared_in": {"type": "string"}, |
| 58 | }, |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 59 | "optional_keys": { |
| 60 | "appends": { |
| 61 | "type": "bool", |
| 62 | }, |
| 63 | }, |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 64 | }, |
| 65 | } |
| 66 | |
| 67 | _all_values_schema = { |
| 68 | "type": "list", |
| 69 | "of": { |
| 70 | "type": "dict", |
| 71 | "required_keys": { |
| 72 | "name": {"type": "string"}, |
| 73 | "value": { |
| 74 | "or": [ |
| 75 | {"type": t} |
| 76 | for t in _valid_types |
| 77 | ], |
| 78 | }, |
| 79 | "set_in": {"type": "string"}, |
| 80 | }, |
| 81 | }, |
| 82 | } |
| 83 | |
LaMont Jones | 9ee098e | 2024-03-20 13:57:50 -0700 | [diff] [blame] | 84 | def flag(name, partitions, default, *, origin = "Unknown", appends = False): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 85 | """Declare a flag. |
| 86 | |
| 87 | Args: |
| 88 | name: name of the flag |
| 89 | partitions: the partitions where this should be recorded. |
| 90 | default: the default value of the flag. |
LaMont Jones | 9ee098e | 2024-03-20 13:57:50 -0700 | [diff] [blame] | 91 | origin: The origin of this flag. |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 92 | appends: Whether new values should be append (not replace) the old. |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 93 | |
| 94 | Returns: |
| 95 | A dictionary containing the flag declaration. |
| 96 | """ |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 97 | if not partitions: |
| 98 | fail("At least 1 partition is required") |
| 99 | if not name.startswith("RELEASE_"): |
| 100 | fail("Release flag names must start with RELEASE_") |
| 101 | if " " in name or "\t" in name or "\n" in name: |
| 102 | fail("Flag names must not contain whitespace: \"" + name + "\"") |
| 103 | for partition in partitions: |
| 104 | if partition == "all": |
| 105 | if len(partitions) > 1: |
| 106 | fail("\"all\" can't be combined with other partitions: " + str(partitions)) |
| 107 | elif partition not in _flag_partitions: |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 108 | fail("Invalid partition: " + partition + ", allowed partitions: " + |
| 109 | str(_flag_partitions)) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 110 | if type(default) not in _valid_types: |
| 111 | fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")") |
| 112 | return { |
| 113 | "name": name, |
| 114 | "partitions": partitions, |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 115 | "default": default, |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 116 | "appends": appends, |
LaMont Jones | 9ee098e | 2024-03-20 13:57:50 -0700 | [diff] [blame] | 117 | "origin": origin, |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 118 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 119 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 120 | def value(name, value): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 121 | """Define the flag value for a particular configuration. |
| 122 | |
| 123 | Args: |
| 124 | name: The name of the flag. |
| 125 | value: The value for the flag. |
| 126 | |
| 127 | Returns: |
| 128 | A dictionary containing the name and value to be used. |
| 129 | """ |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 130 | return { |
| 131 | "name": name, |
| 132 | "value": value, |
| 133 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 134 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 135 | def _format_value(val): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 136 | """Format the starlark type correctly for make. |
| 137 | |
| 138 | Args: |
| 139 | val: The value to format |
| 140 | |
| 141 | Returns: |
| 142 | The value, formatted correctly for make. |
| 143 | """ |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 144 | if type(val) == "NoneType": |
| 145 | return "" |
| 146 | elif type(val) == "bool": |
| 147 | return "true" if val else "" |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 148 | else: |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 149 | return val |
| 150 | |
LaMont Jones | 451abd6 | 2024-03-14 10:48:55 -0700 | [diff] [blame] | 151 | def equal_flag_declaration(flag, other): |
| 152 | """Return true if the flag declarations are equal. |
| 153 | |
| 154 | Args: |
| 155 | flag: This flag declaration. |
| 156 | other: Another flag declaration. |
| 157 | |
| 158 | Returns: |
| 159 | Whether the declarations are the same. |
| 160 | """ |
| 161 | for key in "name", "partitions", "default", "appends": |
| 162 | if flag[key] != other[key]: |
| 163 | return False |
LaMont Jones | 9ee098e | 2024-03-20 13:57:50 -0700 | [diff] [blame] | 164 | # For now, allow Unknown to match any other origin. |
| 165 | if flag["origin"] == "Unknown" or other["origin"] == "Unknown": |
| 166 | return True |
| 167 | return flag["origin"] == other["origin"] |
LaMont Jones | 451abd6 | 2024-03-14 10:48:55 -0700 | [diff] [blame] | 168 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 169 | def release_config(all_flags, all_values): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 170 | """Return the make variables that should be set for this release config. |
| 171 | |
| 172 | Args: |
| 173 | all_flags: A list of flag objects (from flag() calls). |
| 174 | all_values: A list of value objects (from value() calls). |
| 175 | |
| 176 | Returns: |
| 177 | A dictionary of {name: value} variables for make. |
| 178 | """ |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 179 | validate(all_flags, _all_flags_schema) |
| 180 | validate(all_values, _all_values_schema) |
| 181 | |
LaMont Jones | d6be720 | 2024-03-27 17:37:32 -0700 | [diff] [blame] | 182 | # Final values. |
| 183 | values = {} |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 184 | # Validate flags |
| 185 | flag_names = [] |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 186 | flags_dict = {} |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 187 | for flag in all_flags: |
LaMont Jones | d6be720 | 2024-03-27 17:37:32 -0700 | [diff] [blame] | 188 | name = flag["name"] |
| 189 | if name in flag_names: |
| 190 | if equal_flag_declaration(flag, flags_dict[name]): |
LaMont Jones | 451abd6 | 2024-03-14 10:48:55 -0700 | [diff] [blame] | 191 | continue |
| 192 | else: |
LaMont Jones | d6be720 | 2024-03-27 17:37:32 -0700 | [diff] [blame] | 193 | fail(flag["declared_in"] + ": Duplicate declaration of flag " + name + |
| 194 | " (declared first in " + flags_dict[name]["declared_in"] + ")") |
| 195 | flag_names.append(name) |
| 196 | flags_dict[name] = flag |
| 197 | # Set the flag value to the default value. |
| 198 | values[name] = {"name": name, "value": _format_value(flag["default"]), "set_in": flag["declared_in"]} |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 199 | |
| 200 | # Record which flags go on which partition |
| 201 | partitions = {} |
| 202 | for flag in all_flags: |
| 203 | for partition in flag["partitions"]: |
| 204 | if partition == "all": |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 205 | if len(flag["partitions"]) > 1: |
| 206 | fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"])) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 207 | for partition in _flag_partitions: |
| 208 | partitions.setdefault(partition, []).append(flag["name"]) |
| 209 | else: |
| 210 | partitions.setdefault(partition, []).append(flag["name"]) |
| 211 | |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 212 | # Generate final values. |
| 213 | # Only declared flags may have a value. |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 214 | for value in all_values: |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 215 | name = value["name"] |
| 216 | if name not in flag_names: |
| 217 | fail(value["set_in"] + ": Value set for undeclared build flag: " + name) |
| 218 | if flags_dict[name]["appends"]: |
| 219 | if name in values: |
| 220 | values[name]["value"] += " " + value["value"] |
| 221 | values[name]["set_in"] += " " + value["set_in"] |
| 222 | else: |
| 223 | values[name] = value |
| 224 | else: |
| 225 | values[name] = value |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 226 | |
| 227 | # Collect values |
| 228 | result = { |
| 229 | "_ALL_RELEASE_FLAGS": sorted(flag_names), |
| 230 | } |
| 231 | for partition, names in partitions.items(): |
| 232 | result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names |
| 233 | for flag in all_flags: |
LaMont Jones | d6be720 | 2024-03-27 17:37:32 -0700 | [diff] [blame] | 234 | val = _format_value(values[flag["name"]]["value"]) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 235 | result[flag["name"]] = val |
| 236 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"] |
| 237 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"]) |
| 238 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val |
| 239 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"] |
LaMont Jones | d6be720 | 2024-03-27 17:37:32 -0700 | [diff] [blame] | 240 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = values[flag["name"]]["set_in"] |
LaMont Jones | 9ee098e | 2024-03-20 13:57:50 -0700 | [diff] [blame] | 241 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".ORIGIN"] = flag["origin"] |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 242 | |
| 243 | return result |