• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Generate java source files from protobuf files.
7
8This is a helper file for the genproto_java action in protoc_java.gypi.
9
10It performs the following steps:
111. Deletes all old sources (ensures deleted classes are not part of new jars).
122. Creates source directory.
133. Generates Java files using protoc (output into either --java-out-dir or
14   --srcjar).
154. Creates a new stamp file.
16"""
17
18import os
19import optparse
20import shutil
21import subprocess
22import sys
23
24sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp"))
25from util import build_utils
26
27def main(argv):
28  parser = optparse.OptionParser()
29  build_utils.AddDepfileOption(parser)
30  parser.add_option("--protoc", help="Path to protoc binary.")
31  parser.add_option("--proto-path", help="Path to proto directory.")
32  parser.add_option("--java-out-dir",
33      help="Path to output directory for java files.")
34  parser.add_option("--srcjar", help="Path to output srcjar.")
35  parser.add_option("--stamp", help="File to touch on success.")
36  parser.add_option("--nano",
37      help="Use to generate nano protos.", action='store_true')
38  options, args = parser.parse_args(argv)
39
40  build_utils.CheckOptions(options, parser, ['protoc', 'proto_path'])
41  if not options.java_out_dir and not options.srcjar:
42    print 'One of --java-out-dir or --srcjar must be specified.'
43    return 1
44
45  with build_utils.TempDir() as temp_dir:
46    if options.nano:
47      # Specify arguments to the generator.
48      generator_args = ['optional_field_style=reftypes',
49                        'store_unknown_fields=true']
50      out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
51    else:
52      out_arg = '--java_out=' + temp_dir
53
54      # Check if all proto files (which are listed in the args) are opting to
55      # use the lite runtime, otherwise we'd have to include the much heavier
56      # regular proto runtime in Chrome.
57      # TODO(jkrcal): Replace this check by '--java_lite_out=' for the out_arg
58      # above once this works on the master branch of the protobuf library,
59      # expected in version 4.0 (see https://crbug.com/800281).
60      for proto_file in args:
61        if not 'LITE_RUNTIME' in open(proto_file).read():
62          raise Exception(
63              'Chrome only supports lite protos. Please add "optimize_for = '
64              'LITE_RUNTIME" to your proto file to enable the lite runtime.')
65    # Generate Java files using protoc.
66    build_utils.CheckOutput(
67        [options.protoc, '--proto_path', options.proto_path, out_arg]
68        + args)
69
70    if options.java_out_dir:
71      build_utils.DeleteDirectory(options.java_out_dir)
72      shutil.copytree(temp_dir, options.java_out_dir)
73    else:
74      build_utils.ZipDir(options.srcjar, temp_dir)
75
76  if options.depfile:
77    assert options.srcjar
78    deps = args + [options.protoc]
79    build_utils.WriteDepfile(options.depfile, options.srcjar, deps)
80
81  if options.stamp:
82    build_utils.Touch(options.stamp)
83
84if __name__ == '__main__':
85  sys.exit(main(sys.argv[1:]))
86