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 | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 18 | load("//build/bazel/utils:schema_validation.bzl", "validate") |
| 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 | }, |
| 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 Cross | 93b43ef | 2023-11-06 21:32:03 +0000 | [diff] [blame^] | 78 | def flag(name, partitions, default): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 79 | """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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 89 | 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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 100 | fail("Invalid partition: " + partition + ", allowed partitions: " + |
| 101 | str(_flag_partitions)) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 102 | 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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 107 | "default": default, |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 108 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 109 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 110 | def value(name, value): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 111 | """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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 120 | return { |
| 121 | "name": name, |
| 122 | "value": value, |
| 123 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 124 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 125 | def _format_value(val): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 126 | """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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 134 | if type(val) == "NoneType": |
| 135 | return "" |
| 136 | elif type(val) == "bool": |
| 137 | return "true" if val else "" |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 138 | else: |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 139 | return val |
| 140 | |
| 141 | def release_config(all_flags, all_values): |
LaMont Jones | fff3ee0 | 2023-10-12 20:00:32 +0000 | [diff] [blame] | 142 | """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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 151 | validate(all_flags, _all_flags_schema) |
| 152 | validate(all_values, _all_values_schema) |
| 153 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 154 | # 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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame] | 166 | if len(flag["partitions"]) > 1: |
| 167 | fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"])) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 168 | for partition in _flag_partitions: |
| 169 | partitions.setdefault(partition, []).append(flag["name"]) |
| 170 | else: |
| 171 | partitions.setdefault(partition, []).append(flag["name"]) |
| 172 | |
Colin Cross | 93b43ef | 2023-11-06 21:32:03 +0000 | [diff] [blame^] | 173 | # Validate values |
| 174 | # TODO(joeo): Disallow duplicate values after we've split AOSP and vendor flags. |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 175 | values = {} |
| 176 | for value in all_values: |
Colin Cross | 93b43ef | 2023-11-06 21:32:03 +0000 | [diff] [blame^] | 177 | 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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 180 | |
| 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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 191 | 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 Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 201 | |
| 202 | return result |