blob: e355c21cc77ae52e63d7581ee79780bedfa10422 [file] [log] [blame]
Zhi Dou77c9f0c2023-10-11 19:49:56 +00001#!/usr/bin/env python3
2#
3# Copyright (C) 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""Create Aconfig value building rules.
17
18This script will help to create Aconfig flag value building rules. It will
19parse necessary information in the value file to create the building rules, but
20it will not validate the value file. The validation will defer to the building
21system.
22"""
23
24import argparse
25import pathlib
26import re
27import sys
28
29
30_VALUE_LIST_TEMPLATE: str = """
Zhi Doub6697362023-10-23 18:19:29 +000031ACONFIG_VALUES_LIST_LOCAL = [{}]
Zhi Dou77c9f0c2023-10-11 19:49:56 +000032"""
33
34_ACONFIG_VALUES_TEMPLATE: str = """
35aconfig_values {{
Zhi Doub6697362023-10-23 18:19:29 +000036 name: "{}",
Zhi Dou77c9f0c2023-10-11 19:49:56 +000037 package: "{}",
38 srcs: [
39 "{}",
40 ]
41}}
42"""
43
Zhi Doub6697362023-10-23 18:19:29 +000044_ACONFIG_VALUES_NAME_SUFFIX: str = "aconfig-local-override-{}"
45
Zhi Dou77c9f0c2023-10-11 19:49:56 +000046_PACKAGE_REGEX = re.compile(r"^package\:\s*\"([\w\d\.]+)\"")
47_ANDROID_BP_FILE_NAME = r"Android.bp"
48
49
50def _parse_packages(file: pathlib.Path) -> set[str]:
51 packages = set()
52 with open(file) as f:
53 for line in f:
54 line = line.strip()
55 package_match = _PACKAGE_REGEX.match(line)
56 if package_match is None:
57 continue
58 package_name = package_match.group(1)
59 packages.add(package_name)
60
61 return packages
62
63
64def _create_android_bp(packages: set[str], file_name: str) -> str:
65 android_bp = ""
Zhi Doub6697362023-10-23 18:19:29 +000066 value_list = ",\n ".join(
67 map(f'"{_ACONFIG_VALUES_NAME_SUFFIX}"'.format, packages)
68 )
Zhi Dou77c9f0c2023-10-11 19:49:56 +000069 if value_list:
70 value_list = "\n " + value_list + "\n"
71 android_bp += _VALUE_LIST_TEMPLATE.format(value_list) + "\n"
72
73 for package in packages:
Zhi Doub6697362023-10-23 18:19:29 +000074 android_bp += _ACONFIG_VALUES_TEMPLATE.format(
75 _ACONFIG_VALUES_NAME_SUFFIX.format(package), package, file_name
76 )
Zhi Dou77c9f0c2023-10-11 19:49:56 +000077 android_bp += "\n"
78
79 return android_bp
80
81
82def _write_android_bp(new_android_bp: str, out: pathlib.Path) -> None:
83 if not out.is_dir():
84 out.mkdir(parents=True, exist_ok=True)
85
86 output = out.joinpath(_ANDROID_BP_FILE_NAME)
Zhi Doub6697362023-10-23 18:19:29 +000087 with open(output, "r+", encoding="utf8") as file:
88 lines = []
89 for line in file:
90 line = line.rstrip("\n")
91 if line.startswith("ACONFIG_VALUES_LIST_LOCAL"):
92 break
93 lines.append(line)
94 # Overwrite the file with the updated contents.
95 file.seek(0)
96 file.truncate()
97 file.write("\n".join(lines))
98 file.write(new_android_bp)
Zhi Dou77c9f0c2023-10-11 19:49:56 +000099
100
101def main(args):
102 """Program entry point."""
103 args_parser = argparse.ArgumentParser()
104 args_parser.add_argument(
105 "--overrides",
106 required=True,
107 help="The path to override file.",
108 )
109 args_parser.add_argument(
110 "--out",
111 required=True,
112 help="The path to output directory.",
113 )
114
115 args = args_parser.parse_args(args)
116 file = pathlib.Path(args.overrides)
117 out = pathlib.Path(args.out)
118 if not file.is_file():
119 raise FileNotFoundError(f"File '{file}' is not found")
120
121 packages = _parse_packages(file)
122 new_android_bp = _create_android_bp(packages, file.name)
123 _write_android_bp(new_android_bp, out)
124
125
126if __name__ == "__main__":
127 main(sys.argv[1:])