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 | |
| 15 | # Partitions that get build system flag summaries |
| 16 | _flag_partitions = [ |
| 17 | "product", |
| 18 | "system", |
| 19 | "system_ext", |
| 20 | "vendor", |
| 21 | ] |
| 22 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 23 | ALL = ["all"] |
| 24 | PRODUCT = ["product"] |
| 25 | SYSTEM = ["system"] |
| 26 | SYSTEM_EXT = ["system_ext"] |
| 27 | VENDOR = ["vendor"] |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 28 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 29 | _valid_types = ["NoneType", "bool", "list", "string", "int"] |
| 30 | |
| 31 | def flag(name, partitions, default): |
| 32 | "Declare a flag." |
| 33 | if not partitions: |
| 34 | fail("At least 1 partition is required") |
| 35 | if not name.startswith("RELEASE_"): |
| 36 | fail("Release flag names must start with RELEASE_") |
| 37 | if " " in name or "\t" in name or "\n" in name: |
| 38 | fail("Flag names must not contain whitespace: \"" + name + "\"") |
| 39 | for partition in partitions: |
| 40 | if partition == "all": |
| 41 | if len(partitions) > 1: |
| 42 | fail("\"all\" can't be combined with other partitions: " + str(partitions)) |
| 43 | elif partition not in _flag_partitions: |
Cole Faust | 0c869ed | 2023-09-07 12:29:21 -0700 | [diff] [blame^] | 44 | fail("Invalid partition: " + partition + ", allowed partitions: " |
| 45 | + str(_flag_partitions)) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 46 | if type(default) not in _valid_types: |
| 47 | fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")") |
| 48 | return { |
| 49 | "name": name, |
| 50 | "partitions": partitions, |
Cole Faust | 0c869ed | 2023-09-07 12:29:21 -0700 | [diff] [blame^] | 51 | "default": default |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 52 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 53 | |
Cole Faust | 0c869ed | 2023-09-07 12:29:21 -0700 | [diff] [blame^] | 54 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 55 | def value(name, value): |
| 56 | "Define the flag value for a particular configuration." |
| 57 | return { |
| 58 | "name": name, |
| 59 | "value": value, |
| 60 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 61 | |
Cole Faust | 0c869ed | 2023-09-07 12:29:21 -0700 | [diff] [blame^] | 62 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 63 | def _format_value(val): |
| 64 | "Format the starlark type correctly for make" |
| 65 | if type(val) == "NoneType": |
| 66 | return "" |
| 67 | elif type(val) == "bool": |
| 68 | return "true" if val else "" |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 69 | else: |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 70 | return val |
| 71 | |
Cole Faust | 0c869ed | 2023-09-07 12:29:21 -0700 | [diff] [blame^] | 72 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 73 | def release_config(all_flags, all_values): |
| 74 | "Return the make variables that should be set for this release config." |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame] | 75 | # Validate flags |
| 76 | flag_names = [] |
| 77 | for flag in all_flags: |
| 78 | if flag["name"] in flag_names: |
| 79 | fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"]) |
| 80 | flag_names.append(flag["name"]) |
| 81 | |
| 82 | # Record which flags go on which partition |
| 83 | partitions = {} |
| 84 | for flag in all_flags: |
| 85 | for partition in flag["partitions"]: |
| 86 | if partition == "all": |
| 87 | for partition in _flag_partitions: |
| 88 | partitions.setdefault(partition, []).append(flag["name"]) |
| 89 | else: |
| 90 | partitions.setdefault(partition, []).append(flag["name"]) |
| 91 | |
| 92 | # Validate values |
Joe Onorato | 1f65551 | 2023-06-12 23:29:25 -0700 | [diff] [blame] | 93 | # 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] | 94 | values = {} |
| 95 | for value in all_values: |
| 96 | if value["name"] not in flag_names: |
| 97 | fail(value["set_in"] + ": Value set for undeclared build flag: " + value["name"]) |
| 98 | values[value["name"]] = value |
| 99 | |
| 100 | # Collect values |
| 101 | result = { |
| 102 | "_ALL_RELEASE_FLAGS": sorted(flag_names), |
| 103 | } |
| 104 | for partition, names in partitions.items(): |
| 105 | result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names |
| 106 | for flag in all_flags: |
| 107 | if flag["name"] in values: |
| 108 | val = values[flag["name"]]["value"] |
| 109 | set_in = values[flag["name"]]["set_in"] |
| 110 | if type(val) not in _valid_types: |
| 111 | fail("Invalid type of value for flag \"" + flag["name"] + "\" (" + type(val) + ")") |
| 112 | else: |
| 113 | val = flag["default"] |
| 114 | set_in = flag["declared_in"] |
| 115 | val = _format_value(val) |
| 116 | result[flag["name"]] = val |
| 117 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"] |
| 118 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"]) |
| 119 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val |
| 120 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"] |
| 121 | result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 122 | |
| 123 | return result |
Cole Faust | 0c869ed | 2023-09-07 12:29:21 -0700 | [diff] [blame^] | 124 | |