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 |
| 8 | import os |
| 9 | import re |
| 10 | import sys |
| 11 | import zipfile |
| 12 | |
| 13 | from util import build_utils |
| 14 | from util import java_cpp_utils |
| 15 | |
| 16 | |
| 17 | class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate): |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame^] | 18 | # Ex. 'BASE_FEATURE(kConstantName, "StringNameOfTheFeature", ...);' |
| 19 | # would parse as: |
| 20 | # ExtractConstantName() -> 'ConstantName' |
| 21 | # ExtractValue() -> '"StringNameOfTheFeature"' |
| 22 | FEATURE_RE = re.compile(r'BASE_FEATURE\(k([^,]+),') |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 23 | # Ex. 'const base::Feature kConstantName{"StringNameOfTheFeature", ...};' |
| 24 | # would parse as: |
| 25 | # ExtractConstantName() -> 'ConstantName' |
| 26 | # ExtractValue() -> '"StringNameOfTheFeature"' |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame^] | 27 | LEGACY_FEATURE_RE = re.compile( |
Alex Attar | 43a3ef5 | 2022-06-01 14:20:04 | [diff] [blame] | 28 | r'\s*const(?:\s+\w+_EXPORT)? (?:base::)?Feature\s+k(\w+)\s*(?:=\s*)?') |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 29 | VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,') |
| 30 | |
| 31 | def ExtractConstantName(self, line): |
| 32 | match = FeatureParserDelegate.FEATURE_RE.match(line) |
Daniel Cheng | dc644a1 | 2022-09-19 23:21:37 | [diff] [blame^] | 33 | if match: |
| 34 | return match.group(1) |
| 35 | match = FeatureParserDelegate.LEGACY_FEATURE_RE.match(line) |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 36 | return match.group(1) if match else None |
| 37 | |
| 38 | def ExtractValue(self, line): |
| 39 | match = FeatureParserDelegate.VALUE_RE.search(line) |
| 40 | return match.group(1) if match else None |
| 41 | |
| 42 | def CreateJavaConstant(self, name, value, comments): |
| 43 | return java_cpp_utils.JavaString(name, value, comments) |
| 44 | |
| 45 | |
| 46 | def _GenerateOutput(template, source_paths, template_path, features): |
| 47 | description_template = """ |
| 48 | // This following string constants were inserted by |
| 49 | // {SCRIPT_NAME} |
| 50 | // From |
| 51 | // {SOURCE_PATHS} |
| 52 | // Into |
| 53 | // {TEMPLATE_PATH} |
| 54 | |
| 55 | """ |
| 56 | values = { |
| 57 | 'SCRIPT_NAME': java_cpp_utils.GetScriptName(), |
| 58 | 'SOURCE_PATHS': ',\n // '.join(source_paths), |
| 59 | 'TEMPLATE_PATH': template_path, |
| 60 | } |
| 61 | description = description_template.format(**values) |
| 62 | native_features = '\n\n'.join(x.Format() for x in features) |
| 63 | |
| 64 | values = { |
| 65 | 'NATIVE_FEATURES': description + native_features, |
| 66 | } |
| 67 | return template.format(**values) |
| 68 | |
| 69 | |
| 70 | def _ParseFeatureFile(path): |
| 71 | with open(path) as f: |
| 72 | feature_file_parser = java_cpp_utils.CppConstantParser( |
| 73 | FeatureParserDelegate(), f.readlines()) |
| 74 | return feature_file_parser.Parse() |
| 75 | |
| 76 | |
| 77 | def _Generate(source_paths, template_path): |
| 78 | with open(template_path) as f: |
| 79 | lines = f.readlines() |
| 80 | |
| 81 | template = ''.join(lines) |
| 82 | package, class_name = java_cpp_utils.ParseTemplateFile(lines) |
| 83 | output_path = java_cpp_utils.GetJavaFilePath(package, class_name) |
| 84 | |
| 85 | features = [] |
| 86 | for source_path in source_paths: |
| 87 | features.extend(_ParseFeatureFile(source_path)) |
| 88 | |
| 89 | output = _GenerateOutput(template, source_paths, template_path, features) |
| 90 | return output, output_path |
| 91 | |
| 92 | |
| 93 | def _Main(argv): |
| 94 | parser = argparse.ArgumentParser() |
| 95 | |
| 96 | parser.add_argument('--srcjar', |
| 97 | required=True, |
| 98 | help='The path at which to generate the .srcjar file') |
| 99 | |
| 100 | parser.add_argument('--template', |
| 101 | required=True, |
| 102 | help='The template file with which to generate the Java ' |
| 103 | 'class. Must have "{NATIVE_FEATURES}" somewhere in ' |
| 104 | 'the template.') |
| 105 | |
| 106 | parser.add_argument('inputs', |
| 107 | nargs='+', |
| 108 | help='Input file(s)', |
| 109 | metavar='INPUTFILE') |
| 110 | args = parser.parse_args(argv) |
| 111 | |
| 112 | with build_utils.AtomicOutput(args.srcjar) as f: |
| 113 | with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar: |
| 114 | data, path = _Generate(args.inputs, args.template) |
| 115 | build_utils.AddToZipHermetic(srcjar, path, data=data) |
| 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | _Main(sys.argv[1:]) |