blob: 134650851c6ace86462d024ee3745b4f73c0d2cd [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:
44 fail("Invalid partition: " + partition + ", allowed partitions: " +
45 str(_flag_partitions))
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,
51 "default": default,
Cole Faust386b3742023-06-06 16:55:58 -070052 }
Cole Faust386b3742023-06-06 16:55:58 -070053
Joe Onoratod6df20a2023-06-09 18:51:00 -070054def value(name, value):
55 "Define the flag value for a particular configuration."
56 return {
57 "name": name,
58 "value": value,
59 }
Cole Faust386b3742023-06-06 16:55:58 -070060
Joe Onoratod6df20a2023-06-09 18:51:00 -070061def _format_value(val):
62 "Format the starlark type correctly for make"
63 if type(val) == "NoneType":
64 return ""
65 elif type(val) == "bool":
66 return "true" if val else ""
Cole Faust386b3742023-06-06 16:55:58 -070067 else:
Joe Onoratod6df20a2023-06-09 18:51:00 -070068 return val
69
70def release_config(all_flags, all_values):
71 "Return the make variables that should be set for this release config."
72
73 # Validate flags
74 flag_names = []
75 for flag in all_flags:
76 if flag["name"] in flag_names:
77 fail(flag["declared_in"] + ": Duplicate declaration of flag " + flag["name"])
78 flag_names.append(flag["name"])
79
80 # Record which flags go on which partition
81 partitions = {}
82 for flag in all_flags:
83 for partition in flag["partitions"]:
84 if partition == "all":
85 for partition in _flag_partitions:
86 partitions.setdefault(partition, []).append(flag["name"])
87 else:
88 partitions.setdefault(partition, []).append(flag["name"])
89
90 # Validate values
91 values = {}
92 for value in all_values:
93 if value["name"] not in flag_names:
94 fail(value["set_in"] + ": Value set for undeclared build flag: " + value["name"])
95 values[value["name"]] = value
96
97 # Collect values
98 result = {
99 "_ALL_RELEASE_FLAGS": sorted(flag_names),
100 }
101 for partition, names in partitions.items():
102 result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names
103 for flag in all_flags:
104 if flag["name"] in values:
105 val = values[flag["name"]]["value"]
106 set_in = values[flag["name"]]["set_in"]
107 if type(val) not in _valid_types:
108 fail("Invalid type of value for flag \"" + flag["name"] + "\" (" + type(val) + ")")
109 else:
110 val = flag["default"]
111 set_in = flag["declared_in"]
112 val = _format_value(val)
113 result[flag["name"]] = val
114 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"]
115 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"])
116 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val
117 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"]
118 result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in
Cole Faust386b3742023-06-06 16:55:58 -0700119
120 return result