• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2013 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 posixpath
10import re
11import sys
12import zipfile
13
14from util import build_utils
15import action_helpers  # build_utils adds //build to sys.path.
16import zip_helpers
17
18
19def _ParsePackageName(data):
20  m = re.match(r'^\s*package\s+(.*?)\s*;', data, re.MULTILINE)
21  return m.group(1) if m else ''
22
23
24def main(args):
25  args = build_utils.ExpandFileArgs(args)
26
27  parser = argparse.ArgumentParser()
28  parser.add_argument('--include-dirs', help='GN list of include directories.')
29  parser.add_argument('--output', help='Path for .srcjar.')
30  parser.add_argument('--define',
31                      action='append',
32                      dest='defines',
33                      help='List of -D args')
34  parser.add_argument('templates', nargs='+', help='Template files.')
35  options = parser.parse_args(args)
36
37  options.defines = action_helpers.parse_gn_list(options.defines)
38  options.include_dirs = action_helpers.parse_gn_list(options.include_dirs)
39
40  gcc_cmd = [
41      'gcc',
42      '-E',  # stop after preprocessing.
43      '-DANDROID',  # Specify ANDROID define for pre-processor.
44      '-x',
45      'c-header',  # treat sources as C header files
46      '-P',  # disable line markers, i.e. '#line 309'
47  ]
48  gcc_cmd.extend('-D' + x for x in options.defines)
49  gcc_cmd.extend('-I' + x for x in options.include_dirs)
50
51  with action_helpers.atomic_output(options.output) as f:
52    with zipfile.ZipFile(f, 'w') as z:
53      for template in options.templates:
54        data = build_utils.CheckOutput(gcc_cmd + [template])
55        package_name = _ParsePackageName(data)
56        if not package_name:
57          raise Exception('Could not find java package of ' + template)
58        zip_path = posixpath.join(
59            package_name.replace('.', '/'),
60            os.path.splitext(os.path.basename(template))[0]) + '.java'
61        zip_helpers.add_to_zip_hermetic(z, zip_path, data=data)
62
63
64if __name__ == '__main__':
65  main(sys.argv[1:])
66