blob: 480af08d8e5507442f9d97c33f62421ac342ad12 [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 = """
31VALUE_LIST_LIST = [{}]
32"""
33
34_ACONFIG_VALUES_TEMPLATE: str = """
35aconfig_values {{
36 name: "aconfig-local-{}",
37 package: "{}",
38 srcs: [
39 "{}",
40 ]
41}}
42"""
43
44_PACKAGE_REGEX = re.compile(r"^package\:\s*\"([\w\d\.]+)\"")
45_ANDROID_BP_FILE_NAME = r"Android.bp"
46
47
48def _parse_packages(file: pathlib.Path) -> set[str]:
49 packages = set()
50 with open(file) as f:
51 for line in f:
52 line = line.strip()
53 package_match = _PACKAGE_REGEX.match(line)
54 if package_match is None:
55 continue
56 package_name = package_match.group(1)
57 packages.add(package_name)
58
59 return packages
60
61
62def _create_android_bp(packages: set[str], file_name: str) -> str:
63 android_bp = ""
64 value_list = ",\n ".join(map(lambda n: "aconfig-local-" + n, packages))
65 if value_list:
66 value_list = "\n " + value_list + "\n"
67 android_bp += _VALUE_LIST_TEMPLATE.format(value_list) + "\n"
68
69 for package in packages:
70 android_bp += _ACONFIG_VALUES_TEMPLATE.format(package, package, file_name)
71 android_bp += "\n"
72
73 return android_bp
74
75
76def _write_android_bp(new_android_bp: str, out: pathlib.Path) -> None:
77 if not out.is_dir():
78 out.mkdir(parents=True, exist_ok=True)
79
80 output = out.joinpath(_ANDROID_BP_FILE_NAME)
81 with open(output, "w+") as f:
82 f.write(new_android_bp)
83
84
85def main(args):
86 """Program entry point."""
87 args_parser = argparse.ArgumentParser()
88 args_parser.add_argument(
89 "--overrides",
90 required=True,
91 help="The path to override file.",
92 )
93 args_parser.add_argument(
94 "--out",
95 required=True,
96 help="The path to output directory.",
97 )
98
99 args = args_parser.parse_args(args)
100 file = pathlib.Path(args.overrides)
101 out = pathlib.Path(args.out)
102 if not file.is_file():
103 raise FileNotFoundError(f"File '{file}' is not found")
104
105 packages = _parse_packages(file)
106 new_android_bp = _create_android_bp(packages, file.name)
107 _write_android_bp(new_android_bp, out)
108
109
110if __name__ == "__main__":
111 main(sys.argv[1:])