-
Notifications
You must be signed in to change notification settings - Fork 12
/
protoc_java.py
executable file
·90 lines (74 loc) · 3.2 KB
/
protoc_java.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generate java source files from protobuf files.
This is a helper file for the genproto_java action in protoc_java.gypi.
It performs the following steps:
1. Deletes all old sources (ensures deleted classes are not part of new jars).
2. Creates source directory.
3. Generates Java files using protoc (output into either --java-out-dir or
--srcjar).
4. Creates a new stamp file.
"""
from __future__ import print_function
import os
import optparse
import shutil
import subprocess
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp"))
from util import build_utils
def main(argv):
parser = optparse.OptionParser()
build_utils.AddDepfileOption(parser)
parser.add_option("--protoc", help="Path to protoc binary.")
parser.add_option("--proto-path", help="Path to proto directory.")
parser.add_option("--java-out-dir",
help="Path to output directory for java files.")
parser.add_option("--srcjar", help="Path to output srcjar.")
parser.add_option("--stamp", help="File to touch on success.")
parser.add_option("--nano",
help="Use to generate nano protos.", action='store_true')
parser.add_option("--protoc-javalite-plugin-dir",
help="Path to protoc java lite plugin directory.")
options, args = parser.parse_args(argv)
build_utils.CheckOptions(options, parser, ['protoc', 'proto_path'])
if not options.java_out_dir and not options.srcjar:
print('One of --java-out-dir or --srcjar must be specified.')
return 1
if not options.nano and not options.protoc_javalite_plugin_dir:
print('One of --nano or --protoc-javalite-plugin-dir must be specified.')
return 1
with build_utils.TempDir() as temp_dir:
if options.nano:
# Specify arguments to the generator.
generator_args = ['optional_field_style=reftypes',
'store_unknown_fields=true']
out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
else:
out_arg = '--javalite_out=' + temp_dir
custom_env = os.environ.copy()
if options.protoc_javalite_plugin_dir:
# if we are generating lite protos, then the lite plugin needs to be in the path when protoc
# is called. See https://github.com/protocolbuffers/protobuf/blob/master/java/lite.md
custom_env['PATH'] = '{}:{}'.format(
os.path.abspath(options.protoc_javalite_plugin_dir), custom_env['PATH'])
# Generate Java files using protoc.
build_utils.CheckOutput(
[options.protoc, '--proto_path', options.proto_path, out_arg]
+ args, env=custom_env)
if options.java_out_dir:
build_utils.DeleteDirectory(options.java_out_dir)
shutil.copytree(temp_dir, options.java_out_dir)
else:
build_utils.ZipDir(options.srcjar, temp_dir)
if options.depfile:
assert options.srcjar
deps = args + [options.protoc]
build_utils.WriteDepfile(options.depfile, options.srcjar, deps,
add_pydeps=False)
if options.stamp:
build_utils.Touch(options.stamp)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))