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_CHROMIUM_SRC = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 19 os.pardir) 20_LLVM_CLANG_PATH = os.path.join(_CHROMIUM_SRC, 'third_party', 'llvm-build', 21 'Release+Asserts', 'bin', 'clang') 22 23def _ParsePackageName(data): 24 m = re.search(r'^\s*package\s+(.*?)\s*;', data, re.MULTILINE) 25 return m.group(1) if m else '' 26 27 28def ProcessJavaFile(template, defines, include_dirs): 29 clang_cmd = [ 30 _LLVM_CLANG_PATH, 31 '-E', # stop after preprocessing. 32 '-CC', # Keep comments 33 '-DANDROID', # Specify ANDROID define for pre-processor. 34 '-x', 35 'c-header', # treat sources as C header files 36 '-P', # disable line markers, i.e. '#line 309' 37 ] 38 clang_cmd.extend('-D' + x for x in defines) 39 clang_cmd.extend('-I' + x for x in include_dirs) 40 data = build_utils.CheckOutput(clang_cmd + [template]) 41 package_name = _ParsePackageName(data) 42 if not package_name: 43 raise Exception('Could not find java package of ' + template) 44 return package_name, data 45 46 47def main(args): 48 args = build_utils.ExpandFileArgs(args) 49 50 parser = argparse.ArgumentParser() 51 parser.add_argument('--include-dirs', help='GN list of include directories.') 52 parser.add_argument('--output', help='Path for .srcjar.') 53 parser.add_argument('--define', 54 action='append', 55 dest='defines', 56 help='List of -D args') 57 parser.add_argument('templates', nargs='+', help='Template files.') 58 options = parser.parse_args(args) 59 60 options.defines = action_helpers.parse_gn_list(options.defines) 61 options.include_dirs = action_helpers.parse_gn_list(options.include_dirs) 62 with action_helpers.atomic_output(options.output) as f: 63 with zipfile.ZipFile(f, 'w') as z: 64 for template in options.templates: 65 package_name, data = ProcessJavaFile(template, options.defines, 66 options.include_dirs) 67 zip_path = posixpath.join( 68 package_name.replace('.', '/'), 69 os.path.splitext(os.path.basename(template))[0]) + '.java' 70 zip_helpers.add_to_zip_hermetic(z, zip_path, data=data) 71 72 73if __name__ == '__main__': 74 main(sys.argv[1:]) 75