blob: 46fa8200ebd596cd2c26b4f1f2c8c2868597b361 [file] [log] [blame]
[email protected]d339e3c2012-11-14 21:20:471#!/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
nyquist38f28ba2014-09-04 00:28:386"""Generate java source files from protobuf files.
[email protected]d339e3c2012-11-14 21:20:477
[email protected]d339e3c2012-11-14 21:20:478This 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.
cjhopmana3f2d3f62014-10-01 23:48:58133. Generates Java files using protoc (output into either --java-out-dir or
14 --srcjar).
[email protected]d339e3c2012-11-14 21:20:47154. Creates a new stamp file.
16"""
17
18import os
cjhopmana3f2d3f62014-10-01 23:48:5819import optparse
[email protected]d339e3c2012-11-14 21:20:4720import shutil
21import subprocess
22import sys
23
cjhopmana3f2d3f62014-10-01 23:48:5824sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp"))
25from util import build_utils
26
[email protected]d339e3c2012-11-14 21:20:4727def main(argv):
cjhopmana3f2d3f62014-10-01 23:48:5828 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 options, args = parser.parse_args(argv)
37
38 build_utils.CheckOptions(options, parser, ['protoc', 'proto_path'])
39 if not options.java_out_dir and not options.srcjar:
40 print 'One of --java-out-dir or --srcjar must be specified.'
[email protected]d339e3c2012-11-14 21:20:4741 return 1
42
cjhopmana3f2d3f62014-10-01 23:48:5843 with build_utils.TempDir() as temp_dir:
44 # Specify arguments to the generator.
45 generator_args = ['optional_field_style=reftypes',
46 'store_unknown_fields=true']
47 out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
48 # Generate Java files using protoc.
49 build_utils.CheckOutput(
50 [options.protoc, '--proto_path', options.proto_path, out_arg]
51 + args)
[email protected]d339e3c2012-11-14 21:20:4752
cjhopmana3f2d3f62014-10-01 23:48:5853 if options.java_out_dir:
54 build_utils.DeleteDirectory(options.java_out_dir)
55 shutil.copytree(temp_dir, options.java_out_dir)
56 else:
57 build_utils.ZipDir(options.srcjar, temp_dir)
[email protected]d339e3c2012-11-14 21:20:4758
cjhopmana3f2d3f62014-10-01 23:48:5859 if options.depfile:
agrieve4eb18a552016-09-14 02:04:2160 assert options.srcjar
61 deps = args + [options.protoc]
62 build_utils.WriteDepfile(options.depfile, options.srcjar, deps)
[email protected]d339e3c2012-11-14 21:20:4763
cjhopmana3f2d3f62014-10-01 23:48:5864 if options.stamp:
65 build_utils.Touch(options.stamp)
[email protected]d339e3c2012-11-14 21:20:4766
67if __name__ == '__main__':
cjhopmana3f2d3f62014-10-01 23:48:5868 sys.exit(main(sys.argv[1:]))