Skip to content

Commit

Permalink
Add proto_java_library to gn
Browse files Browse the repository at this point in the history
This adds a pretty straightforward gn template corresponding to
protoc_java.gypi.

protoc_java.py is updated to actually parse options instead of just
using positional arguments.

Adds cacheinvalidation targets.

TBR=brettw, zea

BUG=359249

Review URL: https://codereview.chromium.org/580343002

Cr-Commit-Position: refs/heads/master@{#297739}
  • Loading branch information
cjhopman authored and Commit bot committed Oct 1, 2014
1 parent e200cf7 commit a3f2d3f
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 37 deletions.
52 changes: 52 additions & 0 deletions build/config/android/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,55 @@ template("create_native_executable_dist") {
deps = final_deps
}
}


# Compile a protocol buffer to java.
#
# This generates java files from protocol buffers and creates an Android library
# containing the classes.
#
# Variables
# sources: Paths to .proto files to compile.
# proto_path: Root directory of .proto files.
#
# Example:
# proto_java_library("foo_proto_java") {
# proto_path = [ "src/foo" ]
# sources = [ "$proto_path/foo.proto" ]
# }
template("proto_java_library") {
_protoc_dep = "//third_party/android_protobuf:android_protoc($host_toolchain)"
_protoc_out_dir = get_label_info(_protoc_dep, "root_out_dir")
_protoc_bin = "$_protoc_out_dir/android_protoc"
_proto_path = invoker.proto_path

_template_name = target_name

action("${_template_name}__protoc_java") {
srcjar_path = "$target_gen_dir/$target_name.srcjar"
script = "//build/protoc_java.py"
deps = [
_protoc_dep
]
sources = invoker.sources
depfile = "$target_gen_dir/$target_name.d"
outputs = [
depfile,
srcjar_path,
]
args = [
"--depfile", rebase_path(depfile, root_build_dir),
"--protoc", rebase_path(_protoc_bin, root_build_dir),
"--proto-path", rebase_path(_proto_path, root_build_dir),
"--srcjar", rebase_path(srcjar_path, root_build_dir),
] + rebase_path(sources, root_build_dir)
}

android_library(target_name) {
java_files = []
srcjar_deps = [ ":${_template_name}__protoc_java" ]
deps = [
"//third_party/android_protobuf:protobuf_nano_javalib",
]
}
}
8 changes: 4 additions & 4 deletions build/protoc_java.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
],
'action': [
'<(script)',
'<(protoc)',
'<(proto_in_dir)',
'<(java_out_dir)',
'<(stamp_file)',
'--protoc=<(protoc)',
'--proto-path=<(proto_in_dir)',
'--java-out-dir=<(java_out_dir)',
'--stamp=<(stamp_file)',
'<@(_sources)',
],
'message': 'Generating Java code from protobuf files in <(proto_in_dir)',
Expand Down
72 changes: 40 additions & 32 deletions build/protoc_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,64 @@

"""Generate java source files from protobuf files.
Usage:
protoc_java.py {protoc} {proto_path} {java_out} {stamp_file} {proto_files}
This is a helper file for the genproto_java action in protoc_java.gypi.
It performs the following steps:
1. Deletes all old sources (ensures deleted classes are not part of new jars).
2. Creates source directory.
3. Generates Java files using protoc.
3. Generates Java files using protoc (output into either --java-out-dir or
--srcjar).
4. Creates a new stamp file.
"""

import os
import optparse
import shutil
import subprocess
import sys

def main(argv):
if len(argv) < 5:
usage()
return 1

protoc_path, proto_path, java_out, stamp_file = argv[1:5]
proto_files = argv[5:]
sys.path.append(os.path.join(os.path.dirname(__file__), "android", "gyp"))
from util import build_utils

# Delete all old sources.
if os.path.exists(java_out):
shutil.rmtree(java_out)

# Create source directory.
os.makedirs(java_out)
def main(argv):
parser = optparse.OptionParser()
build_utils.AddDepfileOption(parser)
parser.add_option("--protoc", help="Path to protoc binary.")
parser.add_option("--proto-path", help="Path to proto directory.")
parser.add_option("--java-out-dir",
help="Path to output directory for java files.")
parser.add_option("--srcjar", help="Path to output srcjar.")
parser.add_option("--stamp", help="File to touch on success.")
options, args = parser.parse_args(argv)

# Specify arguments to the generator.
generator_args = ['optional_field_style=reftypes',
'store_unknown_fields=true']
out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + java_out
build_utils.CheckOptions(options, parser, ['protoc', 'proto_path'])
if not options.java_out_dir and not options.srcjar:
print 'One of --java-out-dir or --srcjar must be specified.'
return 1

# Generate Java files using protoc.
ret = subprocess.call(
[protoc_path, '--proto_path', proto_path, out_arg] + proto_files)
with build_utils.TempDir() as temp_dir:
# Specify arguments to the generator.
generator_args = ['optional_field_style=reftypes',
'store_unknown_fields=true']
out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + temp_dir
# Generate Java files using protoc.
build_utils.CheckOutput(
[options.protoc, '--proto_path', options.proto_path, out_arg]
+ args)

if ret == 0:
# Create a new stamp file.
with file(stamp_file, 'a'):
os.utime(stamp_file, None)
if options.java_out_dir:
build_utils.DeleteDirectory(options.java_out_dir)
shutil.copytree(temp_dir, options.java_out_dir)
else:
build_utils.ZipDir(options.srcjar, temp_dir)

return ret
if options.depfile:
build_utils.WriteDepfile(
options.depfile,
args + [options.protoc] + build_utils.GetPythonDependencies())

def usage():
print(__doc__);
if options.stamp:
build_utils.Touch(options.stamp)

if __name__ == '__main__':
sys.exit(main(sys.argv))
sys.exit(main(sys.argv[1:]))
31 changes: 30 additions & 1 deletion build/secondary/third_party/cacheinvalidation/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,33 @@ test("cacheinvalidation_unittests") {
]
}

# TODO(GYP) A bunch of Android-specific stuff.
# TODO(GYP) Test isolation stuff.
if (is_android) {
import("//build/config/android/rules.gni")

# GYP: //third_party/cacheinvalidation/cacheinvalidation.gyp:cacheinvalidation_proto_java
proto_java_library("cacheinvalidation_proto_java") {
proto_path = "src/proto"
sources = [
"$proto_path/android_channel.proto",
"$proto_path/android_listener.proto",
"$proto_path/android_service.proto",
"$proto_path/channel_common.proto",
"$proto_path/client.proto",
"$proto_path/client_protocol.proto",
"$proto_path/java_client.proto",
"$proto_path/types.proto",
]
}

# GYP: //third_party/cacheinvalidation/cacheinvalidation.gyp:cacheinvalidation_javalib
android_library("cacheinvalidation_javalib") {
deps = [
":cacheinvalidation_proto_java",
"//third_party/android_protobuf:protobuf_nano_javalib",
"//third_party/android_tools:android_gcm_java",
]

DEPRECATED_java_in_dir = "src/java"
}
}
138 changes: 138 additions & 0 deletions third_party/android_protobuf/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

if (current_toolchain == host_toolchain) {
# GYP: //third_party/android_protobuf/android_protobuf.gyp:android_protoc
executable("android_protoc") {
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [ "//build/config/compiler:no_chromium_code" ]

deps = [
"//third_party/zlib"
]

sources = [
"src/src/google/protobuf/descriptor.cc",
"src/src/google/protobuf/descriptor.pb.cc",
"src/src/google/protobuf/descriptor_database.cc",
"src/src/google/protobuf/dynamic_message.cc",
"src/src/google/protobuf/extension_set.cc",
"src/src/google/protobuf/extension_set_heavy.cc",
"src/src/google/protobuf/generated_message_reflection.cc",
"src/src/google/protobuf/generated_message_util.cc",
"src/src/google/protobuf/message.cc",
"src/src/google/protobuf/message_lite.cc",
"src/src/google/protobuf/reflection_ops.cc",
"src/src/google/protobuf/repeated_field.cc",
"src/src/google/protobuf/service.cc",
"src/src/google/protobuf/text_format.cc",
"src/src/google/protobuf/unknown_field_set.cc",
"src/src/google/protobuf/wire_format.cc",
"src/src/google/protobuf/wire_format_lite.cc",
"src/src/google/protobuf/compiler/code_generator.cc",
"src/src/google/protobuf/compiler/command_line_interface.cc",
"src/src/google/protobuf/compiler/importer.cc",
"src/src/google/protobuf/compiler/main.cc",
"src/src/google/protobuf/compiler/parser.cc",
"src/src/google/protobuf/compiler/plugin.cc",
"src/src/google/protobuf/compiler/plugin.pb.cc",
"src/src/google/protobuf/compiler/subprocess.cc",
"src/src/google/protobuf/compiler/zip_writer.cc",
"src/src/google/protobuf/compiler/cpp/cpp_enum.cc",
"src/src/google/protobuf/compiler/cpp/cpp_enum_field.cc",
"src/src/google/protobuf/compiler/cpp/cpp_extension.cc",
"src/src/google/protobuf/compiler/cpp/cpp_field.cc",
"src/src/google/protobuf/compiler/cpp/cpp_file.cc",
"src/src/google/protobuf/compiler/cpp/cpp_generator.cc",
"src/src/google/protobuf/compiler/cpp/cpp_helpers.cc",
"src/src/google/protobuf/compiler/cpp/cpp_message.cc",
"src/src/google/protobuf/compiler/cpp/cpp_message_field.cc",
"src/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc",
"src/src/google/protobuf/compiler/cpp/cpp_service.cc",
"src/src/google/protobuf/compiler/cpp/cpp_string_field.cc",
"src/src/google/protobuf/compiler/java/java_enum.cc",
"src/src/google/protobuf/compiler/java/java_enum_field.cc",
"src/src/google/protobuf/compiler/java/java_extension.cc",
"src/src/google/protobuf/compiler/java/java_field.cc",
"src/src/google/protobuf/compiler/java/java_file.cc",
"src/src/google/protobuf/compiler/java/java_generator.cc",
"src/src/google/protobuf/compiler/java/java_helpers.cc",
"src/src/google/protobuf/compiler/java/java_message.cc",
"src/src/google/protobuf/compiler/java/java_message_field.cc",
"src/src/google/protobuf/compiler/java/java_primitive_field.cc",
"src/src/google/protobuf/compiler/java/java_service.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_enum.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_enum_field.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_field.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_file.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_generator.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_helpers.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_message.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_message_field.cc",
"src/src/google/protobuf/compiler/javamicro/javamicro_primitive_field.cc",
"src/src/google/protobuf/compiler/javanano/javanano_enum.cc",
"src/src/google/protobuf/compiler/javanano/javanano_enum_field.cc",
"src/src/google/protobuf/compiler/javanano/javanano_extension.cc",
"src/src/google/protobuf/compiler/javanano/javanano_field.cc",
"src/src/google/protobuf/compiler/javanano/javanano_file.cc",
"src/src/google/protobuf/compiler/javanano/javanano_generator.cc",
"src/src/google/protobuf/compiler/javanano/javanano_helpers.cc",
"src/src/google/protobuf/compiler/javanano/javanano_message.cc",
"src/src/google/protobuf/compiler/javanano/javanano_message_field.cc",
"src/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc",
"src/src/google/protobuf/compiler/python/python_generator.cc",
"src/src/google/protobuf/io/coded_stream.cc",
"src/src/google/protobuf/io/gzip_stream.cc",
"src/src/google/protobuf/io/printer.cc",
"src/src/google/protobuf/io/tokenizer.cc",
"src/src/google/protobuf/io/zero_copy_stream.cc",
"src/src/google/protobuf/io/zero_copy_stream_impl.cc",
"src/src/google/protobuf/io/zero_copy_stream_impl_lite.cc",
"src/src/google/protobuf/stubs/common.cc",
"src/src/google/protobuf/stubs/hash.cc",
"src/src/google/protobuf/stubs/once.cc",
"src/src/google/protobuf/stubs/structurally_valid.cc",
"src/src/google/protobuf/stubs/strutil.cc",
"src/src/google/protobuf/stubs/substitute.cc",
]

include_dirs = [
"src/android",
"src/src",
]

if (is_clang) {
cflags = [
"-Wno-null-conversion",
"-Wno-tautological-undefined-compare",
]
}
defines = [
# This macro must be defined to suppress the use
# of dynamic_cast<>, which requires RTTI.
"GOOGLE_PROTOBUF_NO_RTTI",
"GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER",
]
}
}

if (is_android) {
import("//build/config/android/rules.gni")

# GYP: //third_party/android_protobuf/android_protobuf.gyp:protobuf_nano_javalib
android_library("protobuf_nano_javalib") {
java_files = [
"src/java/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java",
"src/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java",
"src/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java",
"src/java/src/main/java/com/google/protobuf/nano/Extension.java",
"src/java/src/main/java/com/google/protobuf/nano/InternalNano.java",
"src/java/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java",
"src/java/src/main/java/com/google/protobuf/nano/MessageNano.java",
"src/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java",
"src/java/src/main/java/com/google/protobuf/nano/UnknownFieldData.java",
"src/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java",
]
}
}
6 changes: 6 additions & 0 deletions third_party/cacheinvalidation/cacheinvalidation.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'targets': [
# The C++ files generated from the cache invalidation protocol buffers.
{
# GN: //third_party/cacheinvalidation/src/google/cacheinvalidation:cacheinvalidation_proto_cpp (secondary)
'target_name': 'cacheinvalidation_proto_cpp',
'type': 'static_library',
'variables': {
Expand Down Expand Up @@ -55,6 +56,7 @@
# The main cache invalidation library. External clients should depend
# only on this.
{
# GN: //third_party/cacheinvalidation (secondary)
'target_name': 'cacheinvalidation',
'type': 'static_library',
'sources': [
Expand Down Expand Up @@ -147,6 +149,7 @@
# Unittests for the cache invalidation library.
# TODO(ghc): Write native tests and include them here.
{
# GN: //third_party/cacheinvalidation:cacheinvalidation_unittests (secondary)
'target_name': 'cacheinvalidation_unittests',
'type': 'executable',
'sources': [
Expand Down Expand Up @@ -175,6 +178,7 @@
['test_isolation_mode != "noop"', {
'targets': [
{
# TODO(GN)
'target_name': 'cacheinvalidation_unittests_run',
'type': 'none',
'dependencies': [
Expand All @@ -195,6 +199,7 @@
},
'targets': [
{
# GN: //third_party/cacheinvalidation:cacheinvalidation_proto_java (secondary)
'target_name': 'cacheinvalidation_proto_java',
'type': 'none',
'variables': {
Expand All @@ -213,6 +218,7 @@
'includes': [ '../../build/protoc_java.gypi' ],
},
{
# GN: //third_party/cacheinvalidation:cacheinvalidation_javalib (secondary)
'target_name': 'cacheinvalidation_javalib',
'type': 'none',
'dependencies': [
Expand Down

0 comments on commit a3f2d3f

Please sign in to comment.