1#!/usr/bin/env python3
2#
3# Copyright 2014 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
7"""Invokes Android's aidl
8"""
9
10import optparse
11import os
12import re
13import sys
14import zipfile
15
16from util import build_utils
17
18
19def main(argv):
20  option_parser = optparse.OptionParser()
21  option_parser.add_option('--aidl-path', help='Path to the aidl binary.')
22  option_parser.add_option('--imports', help='Files to import.')
23  option_parser.add_option('--includes',
24                           help='Directories to add as import search paths.')
25  option_parser.add_option('--srcjar', help='Path for srcjar output.')
26  build_utils.AddDepfileOption(option_parser)
27  options, args = option_parser.parse_args(argv[1:])
28
29  options.includes = build_utils.ParseGnList(options.includes)
30
31  with build_utils.TempDir() as temp_dir:
32    for f in args:
33      classname = os.path.splitext(os.path.basename(f))[0]
34      output = os.path.join(temp_dir, classname + '.java')
35      aidl_cmd = [options.aidl_path]
36      aidl_cmd += [
37        '-p' + s for s in build_utils.ParseGnList(options.imports)
38      ]
39      aidl_cmd += ['-I' + s for s in options.includes]
40      aidl_cmd += [
41        f,
42        output
43      ]
44      build_utils.CheckOutput(aidl_cmd)
45
46    with build_utils.AtomicOutput(options.srcjar) as f:
47      with zipfile.ZipFile(f, 'w') as srcjar:
48        for path in build_utils.FindInDirectory(temp_dir, '*.java'):
49          with open(path) as fileobj:
50            data = fileobj.read()
51          pkg_name = re.search(r'^\s*package\s+(.*?)\s*;', data, re.M).group(1)
52          arcname = '%s/%s' % (
53              pkg_name.replace('.', '/'), os.path.basename(path))
54          build_utils.AddToZipHermetic(srcjar, arcname, data=data)
55
56  if options.depfile:
57    include_files = []
58    for include_dir in options.includes:
59      include_files += build_utils.FindInDirectory(include_dir, '*.java')
60    build_utils.WriteDepfile(options.depfile, options.srcjar, include_files)
61
62
63if __name__ == '__main__':
64  sys.exit(main(sys.argv))
65