Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / build / protoc_java.py
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
8 This is a helper file for the genproto_java action in protoc_java.gypi.
9
10 It performs the following steps:
11 1. Deletes all old sources (ensures deleted classes are not part of new jars).
12 2. Creates source directory.
13 3. Generates Java files using protoc (output into either --java-out-dir or
14    --srcjar).
15 4. Creates a new stamp file.
16 """
17
18 from __future__ import print_function
19
20 import os
21 import optparse
22 import shutil
23 import subprocess
24 import sys
25
26 sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp"))
27 from util import build_utils
28
29 def main(argv):
30   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.")
38   parser.add_option("--nano",
39       help="Use to generate nano protos.", action='store_true')
40   parser.add_option("--protoc-javalite-plugin-dir",
41       help="Path to protoc java lite plugin directory.")
42   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:
46     print('One of --java-out-dir or --srcjar must be specified.')
47     return 1
48
49   if not options.nano and not options.protoc_javalite_plugin_dir:
50     print('One of --nano or --protoc-javalite-plugin-dir must be specified.')
51     return 1
52
53   with build_utils.TempDir() as temp_dir:
54     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:
60       out_arg = '--javalite_out=' + temp_dir
61
62     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
69     # Generate Java files using protoc.
70     build_utils.CheckOutput(
71         [options.protoc, '--proto_path', options.proto_path, out_arg]
72         + args, env=custom_env)
73
74     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)
79
80   if options.depfile:
81     assert options.srcjar
82     deps = args + [options.protoc]
83     build_utils.WriteDepfile(options.depfile, options.srcjar, deps,
84                              add_pydeps=False)
85
86   if options.stamp:
87     build_utils.Touch(options.stamp)
88
89 if __name__ == '__main__':
90   sys.exit(main(sys.argv[1:]))