blob: ecb9ec6022e6303b352d86d78e72e7dc70890dbc [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
23def _combine_dicts_no_duplicate_keys(dicts):
24 result = {}
25 for d in dicts:
26 for k, v in d.items():
27 if k in result:
28 fail("Duplicate key: " + k)
29 result[k] = v
30 return result
31
32def release_config(target_release, flag_definitions, config_maps, fail_if_no_release_config = True):
33 result = {
34 "_ALL_RELEASE_FLAGS": [flag.name for flag in flag_definitions],
35 }
36 all_flags = {}
37 for flag in flag_definitions:
38 if sorted(dir(flag)) != ["default", "name", "partitions"]:
39 fail("Flag structs must contain 3 fields: name, partitions, and default")
40 if not flag.partitions:
41 fail("At least 1 partition is required")
42 for partition in flag.partitions:
43 if partition == "all":
44 if len(flag.partitions) > 1:
45 fail("\"all\" can't be combined with other partitions: " + str(flag.partitions))
46 elif partition not in _flag_partitions:
47 fail("Invalid partition: " + flag.partition + ", allowed partitions: " + str(_flag_partitions))
48 if not flag.name.startswith("RELEASE_"):
49 fail("Release flag names must start with RELEASE_")
50 if " " in flag.name or "\t" in flag.name or "\n" in flag.name:
51 fail("Flag names must not contain whitespace.")
52 if flag.name in all_flags:
53 fail("Duplicate declaration of flag " + flag.name)
54 all_flags[flag.name] = True
55
56 default = flag.default
57 if type(default) == "bool":
58 default = "true" if default else ""
59
60 result["_ALL_RELEASE_FLAGS." + flag.name + ".PARTITIONS"] = flag.partitions
61 result["_ALL_RELEASE_FLAGS." + flag.name + ".DEFAULT"] = default
62 result["_ALL_RELEASE_FLAGS." + flag.name + ".VALUE"] = default
63
64 # If TARGET_RELEASE is set, fail if there is no matching release config
65 # If it isn't set, no release config files will be included and all flags
66 # will get their default values.
67 if target_release:
68 config_map = _combine_dicts_no_duplicate_keys(config_maps)
69 if target_release not in config_map:
70 fail("No release config found for TARGET_RELEASE: " + target_release + ". Available releases are: " + str(config_map.keys()))
71 release_config = config_map[target_release]
72 if sorted(dir(release_config)) != ["flags", "release_version"]:
73 fail("A release config must be a struct with a flags and release_version fields")
74 result["_RELEASE_VERSION"] = release_config.release_version
75 for flag in release_config.flags:
76 if sorted(dir(flag)) != ["name", "value"]:
77 fail("A flag must be a struct with name and value fields, got: " + str(sorted(dir(flag))))
78 if flag.name not in all_flags:
79 fail("Undeclared build flag: " + flag.name)
80 value = flag.value
81 if type(value) == "bool":
82 value = "true" if value else ""
83 result["_ALL_RELEASE_FLAGS." + flag.name + ".VALUE"] = value
84 elif fail_if_no_release_config:
85 fail("FAIL_IF_NO_RELEASE_CONFIG was set and TARGET_RELEASE was not")
86 else:
87 # No TARGET_RELEASE means release version 0
88 result["_RELEASE_VERSION"] = 0
89
90 return result