Takuto Ikuta | 3dab32e0 | 2023-01-12 18:52:00 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 2 | # Copyright 2012 The Chromium Authors |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
nyquist | 38f28ba | 2014-09-04 00:28:38 | [diff] [blame] | 6 | """Generate java source files from protobuf files. |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 7 | |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 8 | This is the action script for the proto_java_library template. |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 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 | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 13 | 3. Generates Java files using protoc (output into either --java-out-dir or |
| 14 | --srcjar). |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 15 | 4. Creates a new stamp file. |
| 16 | """ |
| 17 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 18 | |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 19 | import argparse |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 20 | import os |
| 21 | import shutil |
| 22 | import subprocess |
| 23 | import sys |
| 24 | |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 25 | import action_helpers |
Andrew Grieve | 3dec346 | 2023-03-31 20:31:29 | [diff] [blame] | 26 | import zip_helpers |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 27 | |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 28 | sys.path.append(os.path.join(os.path.dirname(__file__), 'android', 'gyp')) |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 29 | from util import build_utils |
| 30 | |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 31 | |
| 32 | def _HasJavaPackage(proto_lines): |
| 33 | return any(line.strip().startswith('option java_package') |
| 34 | for line in proto_lines) |
| 35 | |
| 36 | |
| 37 | def _EnforceJavaPackage(proto_srcs): |
| 38 | for proto_path in proto_srcs: |
| 39 | with open(proto_path) as in_proto: |
| 40 | if not _HasJavaPackage(in_proto.readlines()): |
| 41 | raise Exception('Proto files for java must contain a "java_package" ' |
| 42 | 'line: {}'.format(proto_path)) |
| 43 | |
| 44 | |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 45 | def main(argv): |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 46 | parser = argparse.ArgumentParser() |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 47 | action_helpers.add_depfile_arg(parser) |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 48 | parser.add_argument('--protoc', required=True, help='Path to protoc binary.') |
David Bengoa | b846350 | 2022-10-07 15:50:13 | [diff] [blame] | 49 | parser.add_argument('--plugin', help='Path to plugin executable') |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 50 | parser.add_argument('--proto-path', |
| 51 | required=True, |
| 52 | help='Path to proto directory.') |
| 53 | parser.add_argument('--java-out-dir', |
| 54 | help='Path to output directory for java files.') |
| 55 | parser.add_argument('--srcjar', help='Path to output srcjar.') |
| 56 | parser.add_argument('--stamp', help='File to touch on success.') |
| 57 | parser.add_argument( |
| 58 | '--import-dir', |
| 59 | action='append', |
| 60 | default=[], |
| 61 | help='Extra import directory for protos, can be repeated.') |
| 62 | parser.add_argument('protos', nargs='+', help='proto source files') |
| 63 | options = parser.parse_args(argv) |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 64 | |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 65 | if not options.java_out_dir and not options.srcjar: |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 66 | raise Exception('One of --java-out-dir or --srcjar must be specified.') |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 67 | |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 68 | _EnforceJavaPackage(options.protos) |
Sky Malice | 4395552 | 2020-01-07 19:55:56 | [diff] [blame] | 69 | |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 70 | with build_utils.TempDir() as temp_dir: |
David Bengoa | b846350 | 2022-10-07 15:50:13 | [diff] [blame] | 71 | protoc_args = [] |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 72 | |
David Bengoa | b846350 | 2022-10-07 15:50:13 | [diff] [blame] | 73 | generator = 'java' |
| 74 | if options.plugin: |
| 75 | generator = 'plugin' |
| 76 | protoc_args += ['--plugin', 'protoc-gen-plugin=' + options.plugin] |
| 77 | |
| 78 | protoc_args += ['--proto_path', options.proto_path] |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 79 | for path in options.import_dir: |
David Bengoa | b846350 | 2022-10-07 15:50:13 | [diff] [blame] | 80 | protoc_args += ['--proto_path', path] |
| 81 | |
| 82 | protoc_args += ['--' + generator + '_out=lite:' + temp_dir] |
oysteine | cabc7f4 | 2018-12-01 00:08:52 | [diff] [blame] | 83 | |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 84 | # Generate Java files using protoc. |
| 85 | build_utils.CheckOutput( |
David Bengoa | b846350 | 2022-10-07 15:50:13 | [diff] [blame] | 86 | [options.protoc] + protoc_args + options.protos, |
Mohamed Heikal | 0221b0c | 2020-03-06 02:48:50 | [diff] [blame] | 87 | # protoc generates superfluous warnings about LITE_RUNTIME deprecation |
| 88 | # even though we are using the new non-deprecated method. |
Mohamed Heikal | 8a3e6a2 | 2020-06-11 21:24:18 | [diff] [blame] | 89 | stderr_filter=lambda output: build_utils.FilterLines( |
| 90 | output, '|'.join([r'optimize_for = LITE_RUNTIME', r'java/lite\.md']) |
| 91 | )) |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 92 | |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 93 | if options.java_out_dir: |
| 94 | build_utils.DeleteDirectory(options.java_out_dir) |
| 95 | shutil.copytree(temp_dir, options.java_out_dir) |
| 96 | else: |
Andrew Grieve | 3dec346 | 2023-03-31 20:31:29 | [diff] [blame] | 97 | with action_helpers.atomic_output(options.srcjar) as f: |
| 98 | zip_helpers.zip_directory(f, temp_dir) |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 99 | |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 100 | if options.depfile: |
agrieve | 4eb18a55 | 2016-09-14 02:04:21 | [diff] [blame] | 101 | assert options.srcjar |
Mohamed Heikal | 8e87c01 | 2020-09-29 13:13:08 | [diff] [blame] | 102 | deps = options.protos + [options.protoc] |
Andrew Grieve | 4bc50e2d | 2023-03-31 02:25:57 | [diff] [blame] | 103 | action_helpers.write_depfile(options.depfile, options.srcjar, deps) |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 104 | |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 105 | if options.stamp: |
| 106 | build_utils.Touch(options.stamp) |
[email protected] | d339e3c | 2012-11-14 21:20:47 | [diff] [blame] | 107 | |
| 108 | if __name__ == '__main__': |
cjhopman | a3f2d3f6 | 2014-10-01 23:48:58 | [diff] [blame] | 109 | sys.exit(main(sys.argv[1:])) |