blob: f93bd990ea6a8bd911a2304df56d099a68d22d6d [file] [log] [blame]
Andrew Grievee6c6bb382021-04-27 21:47:081#!/usr/bin/env python3
Nate Fischerac07b2622020-10-01 20:20:142#
3# Copyright 2020 The Chromium Authors. All rights reserved.
4# 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
15
16
17class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
18 # Ex. 'const base::Feature kConstantName{"StringNameOfTheFeature", ...};'
19 # would parse as:
20 # ExtractConstantName() -> 'ConstantName'
21 # ExtractValue() -> '"StringNameOfTheFeature"'
Alex Attar43a3ef52022-06-01 14:20:0422 FEATURE_RE = re.compile(
23 r'\s*const(?:\s+\w+_EXPORT)? (?:base::)?Feature\s+k(\w+)\s*(?:=\s*)?')
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)
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
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
104 with build_utils.AtomicOutput(args.srcjar) as f:
105 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:])