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. |
| 14 | |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame^] | 15 | load("//build/bazel/utils:schema_validation.bzl", "validate") |
| 16 | |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 17 | # Partitions that get build system flag summaries |
| 18 | _flag_partitions = [ |
| 19 | "product", |
| 20 | "system", |
| 21 | "system_ext", |
| 22 | "vendor", |
| 23 | ] |
| 24 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 25 | ALL = ["all"] |
| 26 | PRODUCT = ["product"] |
| 27 | SYSTEM = ["system"] |
| 28 | SYSTEM_EXT = ["system_ext"] |
| 29 | VENDOR = ["vendor"] |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 30 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 31 | _valid_types = ["NoneType", "bool", "list", "string", "int"] |
| 32 | |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame^] | 33 | _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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 75 | def 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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame^] | 88 | fail("Invalid partition: " + partition + ", allowed partitions: " + |
| 89 | str(_flag_partitions)) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 90 | 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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame^] | 95 | "default": default, |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 96 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 97 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 98 | def value(name, value): |
| 99 | "Define the flag value for a particular configuration." |
| 100 | return { |
| 101 | "name": name, |
| 102 | "value": value, |
| 103 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 104 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 105 | def _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 Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 111 | else: |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 112 | return val |
| 113 | |
| 114 | def release_config(all_flags, all_values): |
| 115 | "Return the make variables that should be set for this release config." |
Cole Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame^] | 116 | validate(all_flags, _all_flags_schema) |
| 117 | validate(all_values, _all_values_schema) |
| 118 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 119 | # 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 Faust | 8a7efaf | 2023-08-15 17:12:01 -0700 | [diff] [blame^] | 131 | if len(flag["partitions"]) > 1: |
| 132 | fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"])) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 133 | 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 Onorato | 1f65551 | 2023-06-12 23:29:25 -0700 | [diff] [blame] | 139 | # 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] | 140 | 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 Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 156 | 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 Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 166 | |
| 167 | return result |