nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 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 | |
nyquist | b3d2794 | 2014-09-04 09:28:38 +0900 | [diff] [blame] | 6 | """Generate java source files from protobuf files. |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 7 | |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 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. |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 13 | 3. Generates Java files using protoc (output into either --java-out-dir or |
| 14 | --srcjar). |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 15 | 4. Creates a new stamp file. |
| 16 | """ |
| 17 | |
| 18 | import os |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 19 | import optparse |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 20 | import shutil |
| 21 | import subprocess |
| 22 | import sys |
| 23 | |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 24 | sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp")) |
| 25 | from util import build_utils |
| 26 | |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 27 | def main(argv): |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 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 | 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.' |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 41 | return 1 |
| 42 | |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 43 | 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) |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 52 | |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 53 | 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) |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 58 | |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 59 | if options.depfile: |
agrieve | 92aed19 | 2016-09-14 11:04:21 +0900 | [diff] [blame] | 60 | assert options.srcjar |
| 61 | deps = args + [options.protoc] |
| 62 | build_utils.WriteDepfile(options.depfile, options.srcjar, deps) |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 63 | |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 64 | if options.stamp: |
| 65 | build_utils.Touch(options.stamp) |
nyquist@chromium.org | 67c61ad | 2012-11-15 06:20:47 +0900 | [diff] [blame] | 66 | |
| 67 | if __name__ == '__main__': |
cjhopman | dcbc7ba | 2014-10-02 08:48:58 +0900 | [diff] [blame] | 68 | sys.exit(main(sys.argv[1:])) |