blob: ca7927e52fffa36f3b64ac20e44e11a9617262d6 [file] [log] [blame]
Cole Faust386b3742023-06-06 16:55:58 -07001# 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 Onoratod6df20a2023-06-09 18:51:00 -070023ALL = ["all"]
24PRODUCT = ["product"]
25SYSTEM = ["system"]
26SYSTEM_EXT = ["system_ext"]
27VENDOR = ["vendor"]
Cole Faust386b3742023-06-06 16:55:58 -070028
Joe Onoratod6df20a2023-06-09 18:51:00 -070029_valid_types = ["NoneType", "bool", "list", "string", "int"]
30
31def 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 Faust0c869ed2023-09-07 12:29:21 -070044 fail("Invalid partition: " + partition + ", allowed partitions: "
45 + str(_flag_partitions))
Joe Onoratod6df20a2023-06-09 18:51:00 -070046 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 Faust0c869ed2023-09-07 12:29:21 -070051 "default": default
Cole Faust386b3742023-06-06 16:55:58 -070052 }
Cole Faust386b3742023-06-06 16:55:58 -070053
Cole Faust0c869ed2023-09-07 12:29:21 -070054
Joe Onoratod6df20a2023-06-09 18:51:00 -070055def value(name, value):
56 "Define the flag value for a particular configuration."
57 return {
58 "name": name,
59 "value": value,
60 }
Cole Faust386b3742023-06-06 16:55:58 -070061
Cole Faust0c869ed2023-09-07 12:29:21 -070062
Joe Onoratod6df20a2023-06-09 18:51:00 -070063def _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 Faust386b3742023-06-06 16:55:58 -070069 else:
Joe Onoratod6df20a2023-06-09 18:51:00 -070070 return val
71
Cole Faust0c869ed2023-09-07 12:29:21 -070072
Joe Onoratod6df20a2023-06-09 18:51:00 -070073def release_config(all_flags, all_values):
74 "Return the make variables that should be set for this release config."
Joe Onoratod6df20a2023-06-09 18:51:00 -070075 # 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 Onorato1f655512023-06-12 23:29:25 -070093 # TODO(joeo): Disallow duplicate values after we've split AOSP and vendor flags.
Joe Onoratod6df20a2023-06-09 18:51:00 -070094 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 Faust386b3742023-06-06 16:55:58 -0700122
123 return result
Cole Faust0c869ed2023-09-07 12:29:21 -0700124