blob: 8f25e3a5e6c757b564745504a3e8912474771940 [file] [log] [blame]
Takuto Ikuta3dab32e02023-01-12 18:52:001#!/usr/bin/env python3
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2012 The Chromium Authors
[email protected]d339e3c2012-11-14 21:20:473# 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
Mohamed Heikal8e87c012020-09-29 13:13:088This is the action script for the proto_java_library template.
[email protected]d339e3c2012-11-14 21:20:479
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:0718
Mohamed Heikal8e87c012020-09-29 13:13:0819import argparse
[email protected]d339e3c2012-11-14 21:20:4720import os
21import shutil
22import subprocess
23import sys
24
Andrew Grieve4bc50e2d2023-03-31 02:25:5725import action_helpers
Andrew Grieve3dec3462023-03-31 20:31:2926import zip_helpers
Andrew Grieve4bc50e2d2023-03-31 02:25:5727
Mohamed Heikal8e87c012020-09-29 13:13:0828sys.path.append(os.path.join(os.path.dirname(__file__), 'android', 'gyp'))
cjhopmana3f2d3f62014-10-01 23:48:5829from util import build_utils
30
Mohamed Heikal8e87c012020-09-29 13:13:0831
32def _HasJavaPackage(proto_lines):
33 return any(line.strip().startswith('option java_package')
34 for line in proto_lines)
35
36
37def _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]d339e3c2012-11-14 21:20:4745def main(argv):
Mohamed Heikal8e87c012020-09-29 13:13:0846 parser = argparse.ArgumentParser()
Andrew Grieve4bc50e2d2023-03-31 02:25:5747 action_helpers.add_depfile_arg(parser)
Mohamed Heikal8e87c012020-09-29 13:13:0848 parser.add_argument('--protoc', required=True, help='Path to protoc binary.')
David Bengoab8463502022-10-07 15:50:1349 parser.add_argument('--plugin', help='Path to plugin executable')
Mohamed Heikal8e87c012020-09-29 13:13:0850 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)
cjhopmana3f2d3f62014-10-01 23:48:5864
cjhopmana3f2d3f62014-10-01 23:48:5865 if not options.java_out_dir and not options.srcjar:
Mohamed Heikal8e87c012020-09-29 13:13:0866 raise Exception('One of --java-out-dir or --srcjar must be specified.')
[email protected]d339e3c2012-11-14 21:20:4767
Mohamed Heikal8e87c012020-09-29 13:13:0868 _EnforceJavaPackage(options.protos)
Sky Malice43955522020-01-07 19:55:5669
cjhopmana3f2d3f62014-10-01 23:48:5870 with build_utils.TempDir() as temp_dir:
David Bengoab8463502022-10-07 15:50:1371 protoc_args = []
Mohamed Heikal8e87c012020-09-29 13:13:0872
David Bengoab8463502022-10-07 15:50:1373 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 Heikal8e87c012020-09-29 13:13:0879 for path in options.import_dir:
David Bengoab8463502022-10-07 15:50:1380 protoc_args += ['--proto_path', path]
81
82 protoc_args += ['--' + generator + '_out=lite:' + temp_dir]
oysteinecabc7f42018-12-01 00:08:5283
cjhopmana3f2d3f62014-10-01 23:48:5884 # Generate Java files using protoc.
85 build_utils.CheckOutput(
David Bengoab8463502022-10-07 15:50:1386 [options.protoc] + protoc_args + options.protos,
Mohamed Heikal0221b0c2020-03-06 02:48:5087 # protoc generates superfluous warnings about LITE_RUNTIME deprecation
88 # even though we are using the new non-deprecated method.
Mohamed Heikal8a3e6a22020-06-11 21:24:1889 stderr_filter=lambda output: build_utils.FilterLines(
90 output, '|'.join([r'optimize_for = LITE_RUNTIME', r'java/lite\.md'])
91 ))
[email protected]d339e3c2012-11-14 21:20:4792
cjhopmana3f2d3f62014-10-01 23:48:5893 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 Grieve3dec3462023-03-31 20:31:2997 with action_helpers.atomic_output(options.srcjar) as f:
98 zip_helpers.zip_directory(f, temp_dir)
[email protected]d339e3c2012-11-14 21:20:4799
cjhopmana3f2d3f62014-10-01 23:48:58100 if options.depfile:
agrieve4eb18a552016-09-14 02:04:21101 assert options.srcjar
Mohamed Heikal8e87c012020-09-29 13:13:08102 deps = options.protos + [options.protoc]
Andrew Grieve4bc50e2d2023-03-31 02:25:57103 action_helpers.write_depfile(options.depfile, options.srcjar, deps)
[email protected]d339e3c2012-11-14 21:20:47104
cjhopmana3f2d3f62014-10-01 23:48:58105 if options.stamp:
106 build_utils.Touch(options.stamp)
[email protected]d339e3c2012-11-14 21:20:47107
108if __name__ == '__main__':
cjhopmana3f2d3f62014-10-01 23:48:58109 sys.exit(main(sys.argv[1:]))