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 | }, |
| 56 | "declared_in": {"type": "string"}, |
| 57 | }, |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 58 | "optional_keys": { |
| 59 | "appends": { |
| 60 | "type": "bool", |
| 61 | }, |
| 62 | }, |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 63 | }, |
| 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 | |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 83 | def flag(name, partitions, default, _kwmarker = (), appends = False): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 84 | """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 Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 90 | _kwmarker: Used to detect argument misuse. |
| 91 | appends: Whether new values should be append (not replace) the old. |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 92 | |
| 93 | Returns: |
| 94 | A dictionary containing the flag declaration. |
| 95 | """ |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 96 | |
| 97 | # If specified, appends must be a keyword value. |
| 98 | if _kwmarker != (): |
| 99 | fail("Too many positional parameters") |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 100 | if not partitions: |
| 101 | fail("At least 1 partition is required") |
| 102 | if not name.startswith("RELEASE_"): |
| 103 | fail("Release flag names must start with RELEASE_") |
| 104 | if " " in name or "\t" in name or "\n" in name: |
| 105 | fail("Flag names must not contain whitespace: \"" + name + "\"") |
| 106 | for partition in partitions: |
| 107 | if partition == "all": |
| 108 | if len(partitions) > 1: |
| 109 | fail("\"all\" can't be combined with other partitions: " + str(partitions)) |
| 110 | elif partition not in _flag_partitions: |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 111 | fail("Invalid partition: " + partition + ", allowed partitions: " + |
| 112 | str(_flag_partitions)) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 113 | if type(default) not in _valid_types: |
| 114 | fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")") |
| 115 | return { |
| 116 | "name": name, |
| 117 | "partitions": partitions, |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 118 | "default": default, |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 119 | "appends": appends, |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 120 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 121 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 122 | def value(name, value): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 123 | """Define the flag value for a particular configuration. |
| 124 | |
| 125 | Args: |
| 126 | name: The name of the flag. |
| 127 | value: The value for the flag. |
| 128 | |
| 129 | Returns: |
| 130 | A dictionary containing the name and value to be used. |
| 131 | """ |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 132 | return { |
| 133 | "name": name, |
| 134 | "value": value, |
| 135 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 136 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 137 | def _format_value(val): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 138 | """Format the starlark type correctly for make. |
| 139 | |
| 140 | Args: |
| 141 | val: The value to format |
| 142 | |
| 143 | Returns: |
| 144 | The value, formatted correctly for make. |
| 145 | """ |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 146 | if type(val) == "NoneType": |
| 147 | return "" |
| 148 | elif type(val) == "bool": |
| 149 | return "true" if val else "" |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 150 | else: |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 151 | return val |
| 152 | |
| 153 | def release_config(all_flags, all_values): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 154 | """Return the make variables that should be set for this release config. |
| 155 | |
| 156 | Args: |
| 157 | all_flags: A list of flag objects (from flag() calls). |
| 158 | all_values: A list of value objects (from value() calls). |
| 159 | |
| 160 | Returns: |
| 161 | A dictionary of {name: value} variables for make. |
| 162 | """ |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 163 | validate(all_flags, _all_flags_schema) |
| 164 | validate(all_values, _all_values_schema) |
| 165 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 166 | # Validate flags |
| 167 | flag_names = [] |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 168 | flags_dict = {} |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 169 | for flag in all_flags: |
| 170 | if flag["name"] in flag_names: |
| 171 | fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"]) |
| 172 | flag_names.append(flag["name"]) |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 173 | flags_dict[flag["name"]] = flag |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 174 | |
| 175 | # Record which flags go on which partition |
| 176 | partitions = {} |
| 177 | for flag in all_flags: |
| 178 | for partition in flag["partitions"]: |
| 179 | if partition == "all": |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 180 | if len(flag["partitions"]) > 1: |
| 181 | fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"])) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 182 | for partition in _flag_partitions: |
| 183 | partitions.setdefault(partition, []).append(flag["name"]) |
| 184 | else: |
| 185 | partitions.setdefault(partition, []).append(flag["name"]) |
| 186 | |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 187 | # Generate final values. |
| 188 | # Only declared flags may have a value. |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 189 | values = {} |
| 190 | for value in all_values: |
LaMont Jones | 2dae3d6 | 2023-11-06 22:15:06 +0000 | [diff] [blame] | 191 | name = value["name"] |
| 192 | if name not in flag_names: |
| 193 | fail(value["set_in"] + ": Value set for undeclared build flag: " + name) |
| 194 | if flags_dict[name]["appends"]: |
| 195 | if name in values: |
| 196 | values[name]["value"] += " " + value["value"] |
| 197 | values[name]["set_in"] += " " + value["set_in"] |
| 198 | else: |
| 199 | values[name] = value |
| 200 | else: |
| 201 | values[name] = value |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 202 | |
| 203 | # Collect values |
| 204 | result = { |
| 205 | "_ALL_RELEASE_FLAGS": sorted(flag_names), |
| 206 | } |
| 207 | for partition, names in partitions.items(): |
| 208 | result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names |
| 209 | for flag in all_flags: |
| 210 | if flag["name"] in values: |
| 211 | val = values[flag["name"]]["value"] |
| 212 | set_in = values[flag["name"]]["set_in"] |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 213 | else: |
| 214 | val = flag["default"] |
| 215 | set_in = flag["declared_in"] |
| 216 | val = _format_value(val) |
| 217 | result[flag["name"]] = val |
| 218 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"] |
| 219 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"]) |
| 220 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val |
| 221 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"] |
| 222 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 223 | |
| 224 | return result |