blob: 9d14434fa447c95778bcba5c4bc213473356ae7e [file] [log] [blame]
Andrew Grievee6c6bb382021-04-27 21:47:081#!/usr/bin/env python3
Nate Fischerac07b2622020-10-01 20:20:142#
Avi Drissman73a09d12022-09-08 20:33:383# Copyright 2020 The Chromium Authors
Nate Fischerac07b2622020-10-01 20:20:144# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import re
10import sys
11import zipfile
12
13from util import build_utils
14from util import java_cpp_utils
Andrew Grieve4bc50e2d2023-03-31 02:25:5715import action_helpers # build_utils adds //build to sys.path.
Nate Fischerac07b2622020-10-01 20:20:1416
17
18class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
Daniel Chengdc644a12022-09-19 23:21:3719 # 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 Fischerac07b2622020-10-01 20:20:1424 VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,')
25
26 def ExtractConstantName(self, line):
27 match = FeatureParserDelegate.FEATURE_RE.match(line)
Nate Fischerac07b2622020-10-01 20:20:1428 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
38def _GenerateOutput(template, source_paths, template_path, features):
39 description_template = """
40 // This following string constants were inserted by
41 // {SCRIPT_NAME}
42 // From
43 // {SOURCE_PATHS}
44 // Into
45 // {TEMPLATE_PATH}
46
47"""
48 values = {
49 'SCRIPT_NAME': java_cpp_utils.GetScriptName(),
50 'SOURCE_PATHS': ',\n // '.join(source_paths),
51 'TEMPLATE_PATH': template_path,
52 }
53 description = description_template.format(**values)
54 native_features = '\n\n'.join(x.Format() for x in features)
55
56 values = {
57 'NATIVE_FEATURES': description + native_features,
58 }
59 return template.format(**values)
60
61
62def _ParseFeatureFile(path):
63 with open(path) as f:
64 feature_file_parser = java_cpp_utils.CppConstantParser(
65 FeatureParserDelegate(), f.readlines())
66 return feature_file_parser.Parse()
67
68
69def _Generate(source_paths, template_path):
70 with open(template_path) as f:
71 lines = f.readlines()
72
73 template = ''.join(lines)
74 package, class_name = java_cpp_utils.ParseTemplateFile(lines)
75 output_path = java_cpp_utils.GetJavaFilePath(package, class_name)
76
77 features = []
78 for source_path in source_paths:
79 features.extend(_ParseFeatureFile(source_path))
80
81 output = _GenerateOutput(template, source_paths, template_path, features)
82 return output, output_path
83
84
85def _Main(argv):
86 parser = argparse.ArgumentParser()
87
88 parser.add_argument('--srcjar',
89 required=True,
90 help='The path at which to generate the .srcjar file')
91
92 parser.add_argument('--template',
93 required=True,
94 help='The template file with which to generate the Java '
95 'class. Must have "{NATIVE_FEATURES}" somewhere in '
96 'the template.')
97
98 parser.add_argument('inputs',
99 nargs='+',
100 help='Input file(s)',
101 metavar='INPUTFILE')
102 args = parser.parse_args(argv)
103
Andrew Grieve4bc50e2d2023-03-31 02:25:57104 with action_helpers.atomic_output(args.srcjar) as f:
Nate Fischerac07b2622020-10-01 20:20:14105 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar:
106 data, path = _Generate(args.inputs, args.template)
107 build_utils.AddToZipHermetic(srcjar, path, data=data)
108
109
110if __name__ == '__main__':
111 _Main(sys.argv[1:])