blob: 4eb8eaecbca66464f331035622f463ad35150c40 [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
Raul Tambre9e24293b2019-05-12 06:11:0718from __future__ import print_function
19
[email protected]d339e3c2012-11-14 21:20:4720import os
cjhopmana3f2d3f62014-10-01 23:48:5821import optparse
[email protected]d339e3c2012-11-14 21:20:4722import shutil
23import subprocess
24import sys
25
cjhopmana3f2d3f62014-10-01 23:48:5826sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp"))
27from util import build_utils
28
[email protected]d339e3c2012-11-14 21:20:4729def main(argv):
cjhopmana3f2d3f62014-10-01 23:48:5830 parser = optparse.OptionParser()
31 build_utils.AddDepfileOption(parser)
32 parser.add_option("--protoc", help="Path to protoc binary.")
33 parser.add_option("--proto-path", help="Path to proto directory.")
34 parser.add_option("--java-out-dir",
35 help="Path to output directory for java files.")
36 parser.add_option("--srcjar", help="Path to output srcjar.")
37 parser.add_option("--stamp", help="File to touch on success.")
Jan Krcaldc072552018-04-09 08:52:1638 parser.add_option("--nano",
39 help="Use to generate nano protos.", action='store_true')
oysteinecabc7f42018-12-01 00:08:5240 parser.add_option("--protoc-javalite-plugin-dir",
41 help="Path to protoc java lite plugin directory.")
cjhopmana3f2d3f62014-10-01 23:48:5842 options, args = parser.parse_args(argv)
43
44 build_utils.CheckOptions(options, parser, ['protoc', 'proto_path'])
45 if not options.java_out_dir and not options.srcjar:
Raul Tambre9e24293b2019-05-12 06:11:0746 print('One of --java-out-dir or --srcjar must be specified.')
[email protected]d339e3c2012-11-14 21:20:4747 return 1
48
oysteinecabc7f42018-12-01 00:08:5249 if not options.nano and not options.protoc_javalite_plugin_dir:
Raul Tambre9e24293b2019-05-12 06:11:0750 print('One of --nano or --protoc-javalite-plugin-dir must be specified.')
oysteinecabc7f42018-12-01 00:08:5251 return 1
52
cjhopmana3f2d3f62014-10-01 23:48:5853 with build_utils.TempDir() as temp_dir:
Jan Krcaldc072552018-04-09 08:52:1654 if options.nano:
55 # Specify arguments to the generator.
56 generator_args = ['optional_field_style=reftypes',
57 'store_unknown_fields=true']
58 out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
59 else:
oysteinecabc7f42018-12-01 00:08:5260 out_arg = '--javalite_out=' + temp_dir
Florian Uunkcc12c7a2017-11-17 03:11:0761
oysteinecabc7f42018-12-01 00:08:5262 custom_env = os.environ.copy()
63 if options.protoc_javalite_plugin_dir:
64 # if we are generating lite protos, then the lite plugin needs to be in the path when protoc
65 # is called. See https://github.com/protocolbuffers/protobuf/blob/master/java/lite.md
66 custom_env['PATH'] = '{}:{}'.format(
67 os.path.abspath(options.protoc_javalite_plugin_dir), custom_env['PATH'])
68
cjhopmana3f2d3f62014-10-01 23:48:5869 # Generate Java files using protoc.
70 build_utils.CheckOutput(
71 [options.protoc, '--proto_path', options.proto_path, out_arg]
oysteinecabc7f42018-12-01 00:08:5272 + args, env=custom_env)
[email protected]d339e3c2012-11-14 21:20:4773
cjhopmana3f2d3f62014-10-01 23:48:5874 if options.java_out_dir:
75 build_utils.DeleteDirectory(options.java_out_dir)
76 shutil.copytree(temp_dir, options.java_out_dir)
77 else:
78 build_utils.ZipDir(options.srcjar, temp_dir)
[email protected]d339e3c2012-11-14 21:20:4779
cjhopmana3f2d3f62014-10-01 23:48:5880 if options.depfile:
agrieve4eb18a552016-09-14 02:04:2181 assert options.srcjar
82 deps = args + [options.protoc]
David 'Digit' Turner0006f4732018-08-07 07:12:3683 build_utils.WriteDepfile(options.depfile, options.srcjar, deps,
84 add_pydeps=False)
[email protected]d339e3c2012-11-14 21:20:4785
cjhopmana3f2d3f62014-10-01 23:48:5886 if options.stamp:
87 build_utils.Touch(options.stamp)
[email protected]d339e3c2012-11-14 21:20:4788
89if __name__ == '__main__':
cjhopmana3f2d3f62014-10-01 23:48:5890 sys.exit(main(sys.argv[1:]))