Zhi Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 1 | #!/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 | |
| 18 | This script will help to create Aconfig flag value building rules. It will |
| 19 | parse necessary information in the value file to create the building rules, but |
| 20 | it will not validate the value file. The validation will defer to the building |
| 21 | system. |
| 22 | """ |
| 23 | |
| 24 | import argparse |
| 25 | import pathlib |
| 26 | import re |
| 27 | import sys |
| 28 | |
| 29 | |
| 30 | _VALUE_LIST_TEMPLATE: str = """ |
Zhi Dou | b669736 | 2023-10-23 18:19:29 +0000 | [diff] [blame] | 31 | ACONFIG_VALUES_LIST_LOCAL = [{}] |
Zhi Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 32 | """ |
| 33 | |
| 34 | _ACONFIG_VALUES_TEMPLATE: str = """ |
| 35 | aconfig_values {{ |
Zhi Dou | b669736 | 2023-10-23 18:19:29 +0000 | [diff] [blame] | 36 | name: "{}", |
Zhi Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 37 | package: "{}", |
| 38 | srcs: [ |
| 39 | "{}", |
| 40 | ] |
| 41 | }} |
| 42 | """ |
| 43 | |
Zhi Dou | b669736 | 2023-10-23 18:19:29 +0000 | [diff] [blame] | 44 | _ACONFIG_VALUES_NAME_SUFFIX: str = "aconfig-local-override-{}" |
| 45 | |
Zhi Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 46 | _PACKAGE_REGEX = re.compile(r"^package\:\s*\"([\w\d\.]+)\"") |
| 47 | _ANDROID_BP_FILE_NAME = r"Android.bp" |
| 48 | |
| 49 | |
| 50 | def _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 | |
| 64 | def _create_android_bp(packages: set[str], file_name: str) -> str: |
| 65 | android_bp = "" |
Zhi Dou | b669736 | 2023-10-23 18:19:29 +0000 | [diff] [blame] | 66 | value_list = ",\n ".join( |
| 67 | map(f'"{_ACONFIG_VALUES_NAME_SUFFIX}"'.format, packages) |
| 68 | ) |
Zhi Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 69 | 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 Dou | b669736 | 2023-10-23 18:19:29 +0000 | [diff] [blame] | 74 | android_bp += _ACONFIG_VALUES_TEMPLATE.format( |
| 75 | _ACONFIG_VALUES_NAME_SUFFIX.format(package), package, file_name |
| 76 | ) |
Zhi Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 77 | android_bp += "\n" |
| 78 | |
| 79 | return android_bp |
| 80 | |
| 81 | |
| 82 | def _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 Dou | b669736 | 2023-10-23 18:19:29 +0000 | [diff] [blame] | 87 | 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 Dou | 77c9f0c | 2023-10-11 19:49:56 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def 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 | |
| 126 | if __name__ == "__main__": |
| 127 | main(sys.argv[1:]) |