xref: /aosp_15_r20/external/cronet/build/android/gyp/java_cpp_features.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python3
2#
3# Copyright 2020 The Chromium Authors
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
15import action_helpers  # build_utils adds //build to sys.path.
16import zip_helpers
17
18
19class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
20  # Ex. 'BASE_FEATURE(kConstantName, "StringNameOfTheFeature", ...);'
21  # would parse as:
22  #   ExtractConstantName() -> 'ConstantName'
23  #   ExtractValue() -> '"StringNameOfTheFeature"'
24  FEATURE_RE = re.compile(r'BASE_FEATURE\(k([^,]+),')
25  VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,')
26
27  def ExtractConstantName(self, line):
28    match = FeatureParserDelegate.FEATURE_RE.match(line)
29    return match.group(1) if match else None
30
31  def ExtractValue(self, line):
32    match = FeatureParserDelegate.VALUE_RE.search(line)
33    return match.group(1) if match else None
34
35  def CreateJavaConstant(self, name, value, comments):
36    return java_cpp_utils.JavaString(name, value, comments)
37
38
39def _GenerateOutput(template, source_paths, template_path, features):
40  description_template = """
41    // This following string constants were inserted by
42    //     {SCRIPT_NAME}
43    // From
44    //     {SOURCE_PATHS}
45    // Into
46    //     {TEMPLATE_PATH}
47
48"""
49  values = {
50      'SCRIPT_NAME': java_cpp_utils.GetScriptName(),
51      'SOURCE_PATHS': ',\n    //     '.join(source_paths),
52      'TEMPLATE_PATH': template_path,
53  }
54  description = description_template.format(**values)
55  native_features = '\n\n'.join(x.Format() for x in features)
56
57  values = {
58      'NATIVE_FEATURES': description + native_features,
59  }
60  return template.format(**values)
61
62
63def _ParseFeatureFile(path):
64  with open(path) as f:
65    feature_file_parser = java_cpp_utils.CppConstantParser(
66        FeatureParserDelegate(), f.readlines())
67  return feature_file_parser.Parse()
68
69
70def _Generate(source_paths, template_path):
71  with open(template_path) as f:
72    lines = f.readlines()
73
74  template = ''.join(lines)
75  package, class_name = java_cpp_utils.ParseTemplateFile(lines)
76  output_path = java_cpp_utils.GetJavaFilePath(package, class_name)
77
78  features = []
79  for source_path in source_paths:
80    features.extend(_ParseFeatureFile(source_path))
81
82  output = _GenerateOutput(template, source_paths, template_path, features)
83  return output, output_path
84
85
86def _Main(argv):
87  parser = argparse.ArgumentParser()
88
89  parser.add_argument('--srcjar',
90                      required=True,
91                      help='The path at which to generate the .srcjar file')
92
93  parser.add_argument('--template',
94                      required=True,
95                      help='The template file with which to generate the Java '
96                      'class. Must have "{NATIVE_FEATURES}" somewhere in '
97                      'the template.')
98
99  parser.add_argument('inputs',
100                      nargs='+',
101                      help='Input file(s)',
102                      metavar='INPUTFILE')
103  args = parser.parse_args(argv)
104
105  with action_helpers.atomic_output(args.srcjar) as f:
106    with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar:
107      data, path = _Generate(args.inputs, args.template)
108      zip_helpers.add_to_zip_hermetic(srcjar, path, data=data)
109
110
111if __name__ == '__main__':
112  _Main(sys.argv[1:])
113