Andrew Grieve | e6c6bb38 | 2021-04-27 21:47:08 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 2 | # |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 3 | # Copyright 2020 The Chromium Authors |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import argparse |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 8 | import re |
| 9 | import sys |
| 10 | import zipfile |
| 11 | |
Andrew Grieve | 74ce100 | 2025-06-23 15:29:14 | [diff] [blame] | 12 | from util import build_utils # pylint: disable=unused-import |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 13 | from util import java_cpp_utils |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 14 | import action_helpers # build_utils adds //build to sys.path. |
Andrew Grieve | 3dec346 | 2023-03-31 20:31:29 | [diff] [blame] | 15 | import zip_helpers |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate): |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame] | 19 | # Ex. 'BASE_FEATURE(kConstantName, "StringNameOfTheFeature", ...);' |
| 20 | # would parse as: |
| 21 | # ExtractConstantName() -> 'ConstantName' |
| 22 | # ExtractValue() -> '"StringNameOfTheFeature"' |
| 23 | FEATURE_RE = re.compile(r'BASE_FEATURE\(k([^,]+),') |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 24 | VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,') |
| 25 | |
| 26 | def ExtractConstantName(self, line): |
| 27 | match = FeatureParserDelegate.FEATURE_RE.match(line) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 28 | return match.group(1) if match else None |
| 29 | |
| 30 | def ExtractValue(self, line): |
| 31 | match = FeatureParserDelegate.VALUE_RE.search(line) |
| 32 | return match.group(1) if match else None |
| 33 | |
| 34 | def CreateJavaConstant(self, name, value, comments): |
| 35 | return java_cpp_utils.JavaString(name, value, comments) |
| 36 | |
| 37 | |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 38 | def _GenerateOutput(template, source_paths, features): |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 39 | description_template = """ |
| 40 | // This following string constants were inserted by |
| 41 | // {SCRIPT_NAME} |
| 42 | // From |
| 43 | // {SOURCE_PATHS} |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 44 | |
| 45 | """ |
| 46 | values = { |
| 47 | 'SCRIPT_NAME': java_cpp_utils.GetScriptName(), |
| 48 | 'SOURCE_PATHS': ',\n // '.join(source_paths), |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 49 | } |
| 50 | description = description_template.format(**values) |
| 51 | native_features = '\n\n'.join(x.Format() for x in features) |
| 52 | |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 53 | # TODO(agrieve): Remove {{ and }} from input templates. |
| 54 | template = template.replace('{{', '{').replace('}}', '}') |
| 55 | return template.replace('{NATIVE_FEATURES}', description + native_features) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 56 | |
| 57 | |
| 58 | def _ParseFeatureFile(path): |
Andrew Grieve | 74ce100 | 2025-06-23 15:29:14 | [diff] [blame] | 59 | with open(path, encoding='utf-8') as f: |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 60 | feature_file_parser = java_cpp_utils.CppConstantParser( |
| 61 | FeatureParserDelegate(), f.readlines()) |
| 62 | return feature_file_parser.Parse() |
| 63 | |
| 64 | |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 65 | def _Generate(source_paths, template): |
| 66 | package, class_name = java_cpp_utils.ParseTemplateFile(template) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 67 | output_path = java_cpp_utils.GetJavaFilePath(package, class_name) |
| 68 | |
| 69 | features = [] |
| 70 | for source_path in source_paths: |
| 71 | features.extend(_ParseFeatureFile(source_path)) |
| 72 | |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 73 | output = _GenerateOutput(template, source_paths, features) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 74 | return output, output_path |
| 75 | |
| 76 | |
| 77 | def _Main(argv): |
| 78 | parser = argparse.ArgumentParser() |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 79 | parser.add_argument('--srcjar', |
| 80 | required=True, |
| 81 | help='The path at which to generate the .srcjar file') |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 82 | parser.add_argument('--template', |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 83 | help='The template file with which to generate the Java ' |
| 84 | 'class. Must have "{NATIVE_FEATURES}" somewhere in ' |
| 85 | 'the template.') |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 86 | parser.add_argument('--class-name', help='FQN of Java class to generate') |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 87 | parser.add_argument('inputs', |
| 88 | nargs='+', |
| 89 | help='Input file(s)', |
| 90 | metavar='INPUTFILE') |
| 91 | args = parser.parse_args(argv) |
| 92 | |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 93 | if args.template: |
| 94 | with open(args.template, encoding='utf-8') as f: |
| 95 | template = f.read() |
| 96 | else: |
| 97 | template = java_cpp_utils.CreateDefaultTemplate(args.class_name, |
| 98 | '{NATIVE_FEATURES}') |
| 99 | |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 100 | with action_helpers.atomic_output(args.srcjar) as f: |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 101 | with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar: |
Andrew Grieve | 6262d38 | 2025-06-23 18:37:06 | [diff] [blame] | 102 | data, path = _Generate(args.inputs, template) |
Andrew Grieve | 3dec346 | 2023-03-31 20:31:29 | [diff] [blame] | 103 | zip_helpers.add_to_zip_hermetic(srcjar, path, data=data) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 104 | |
| 105 | |
| 106 | if __name__ == '__main__': |
| 107 | _Main(sys.argv[1:]) |