blob: 5aeb219bd69da0466db14acda7fea1cfca72040f [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
15
16
17class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
Daniel Chengdc644a12022-09-19 23:21:3718 # 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 Fischerac07b2622020-10-01 20:20:1423 # Ex. 'const base::Feature kConstantName{"StringNameOfTheFeature", ...};'
24 # would parse as:
25 # ExtractConstantName() -> 'ConstantName'
26 # ExtractValue() -> '"StringNameOfTheFeature"'
Daniel Chengdc644a12022-09-19 23:21:3727 LEGACY_FEATURE_RE = re.compile(
Alex Attar43a3ef52022-06-01 14:20:0428 r'\s*const(?:\s+\w+_EXPORT)? (?:base::)?Feature\s+k(\w+)\s*(?:=\s*)?')
Nate Fischerac07b2622020-10-01 20:20:1429 VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,')
30
31 def ExtractConstantName(self, line):
32 match = FeatureParserDelegate.FEATURE_RE.match(line)
Daniel Chengdc644a12022-09-19 23:21:3733 if match:
34 return match.group(1)
35 match = FeatureParserDelegate.LEGACY_FEATURE_RE.match(line)
Nate Fischerac07b2622020-10-01 20:20:1436 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
46def _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
70def _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
77def _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
93def _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
118if __name__ == '__main__':
119 _Main(sys.argv[1:])