Skip to content

Commit

Permalink
protobuf: Use bazel's native java_proto_library rules.
Browse files Browse the repository at this point in the history
Additional changes:
  - Introduce a Skylark macro java_library_srcs that provides the source jars of a java_*_library rule.
  - Remove bazel's own java_proto_library implementation.

Change-Id: I18f2259bc75ca0fb32dcd8a6a857c609bd2c7773
PiperOrigin-RevId: 158146210
  • Loading branch information
buchgr authored and katre committed Jun 6, 2017
1 parent d9223e0 commit 6073f1d
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 206 deletions.
6 changes: 6 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ filegroup(
visibility = ["//visibility:private"],
)

filegroup(
name = "bootstrap-derived-java-srcs",
srcs = glob(["derived/**/*.java"]),
visibility = ["//:__subpackages__"],
)

load("//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

pkg_tar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,16 @@ load("//tools/build_rules:java_rules_skylark.bzl", "bootstrap_java_library", "bo

bootstrap_java_library(
name = "skylark-deps",
srcs = ["//:bootstrap-derived-java-srcs"],
jars = [
"//third_party:auto_value-jars",
"//third_party:bootstrap_guava_and_error_prone-jars",
"//third_party:jsr305-jars",
"//third_party/protobuf:protobuf-jars",
"//third_party/java/jacoco:core-jars",
"//third_party/grpc:bootstrap-grpc-jars",
],
neverlink_jars = ["//third_party/java/jdk/langtools:javac_jar"],
srcjars = [
"//src/main/protobuf:deps_java_proto_srcjar",
"//src/main/protobuf:worker_protocol_java_proto_srcjar",
"//src/main/protobuf:java_compilation_java_proto_srcjar",
],
)

bootstrap_java_library(
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ java_library(
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:build_java_proto",
"//src/main/protobuf:command_server_java_grpc",
"//src/main/protobuf:command_server_java_proto",
"//src/main/protobuf:invocation_policy_java_proto",
"//src/main/protobuf:test_status_java_proto",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package(default_visibility = ["//src:__subpackages__"])

load("//tools/build_rules:genproto.bzl", "java_proto_library")
load("//tools/build_rules:utilities.bzl", "java_library_srcs")

filegroup(
name = "srcs",
srcs = glob(["**"]),
)

java_library_srcs(
name = "dist_jars",
deps = [":build_event_stream_java_proto"],
)

java_proto_library(
name = "build_event_stream_java_proto",
src = "build_event_stream.proto",
deps = ["build_event_stream_proto"],
)

filegroup(
name = "dist_jars",
srcs = ["build_event_stream_java_proto_srcjar"],
proto_library(
name = "build_event_stream_proto",
srcs = ["build_event_stream.proto"],
)
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/remote/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/standalone",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:remote_protocol_java_grpc",
"//src/main/protobuf:remote_protocol_java_proto",
"//third_party:apache_httpclient",
"//third_party:apache_httpcore",
Expand Down
42 changes: 28 additions & 14 deletions src/main/protobuf/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package(default_visibility = ["//visibility:public"])

load("//tools/build_rules:genproto.bzl", "java_proto_library", "cc_grpc_library")
load("//tools/build_rules:genproto.bzl", "cc_grpc_library")
load("//tools/build_rules:utilities.bzl", "java_library_srcs")
load("//third_party/protobuf/3.2.0:protobuf.bzl", "cc_proto_library", "py_proto_library")
load("//third_party/grpc:build_defs.bzl", "java_grpc_library")

FILES = [
"build",
Expand All @@ -19,11 +21,23 @@ FILES = [
"invocation_policy",
"android_deploy_info",
"apk_manifest",
"command_server",
"remote_protocol",
]

[proto_library(
name = s + "_proto",
srcs = [s + ".proto"],
) for s in FILES]

[java_proto_library(
name = s + "_java_proto",
src = s + ".proto",
deps = [":" + s + "_proto"],
) for s in FILES]

[java_library_srcs(
name = s + "_java_proto_srcs",
deps = [":" + s + "_java_proto"],
) for s in FILES]

cc_proto_library(
Expand All @@ -34,10 +48,10 @@ cc_proto_library(
protoc = "//third_party/protobuf:protoc",
)

java_proto_library(
name = "command_server_java_proto",
src = "command_server.proto",
use_grpc_plugin = True,
java_grpc_library(
name = "command_server_java_grpc",
srcs = [":command_server_proto"],
deps = [":command_server_java_proto"],
)

cc_grpc_library(
Expand All @@ -46,10 +60,10 @@ cc_grpc_library(
)

# TODO(olaola): add Golang support.
java_proto_library(
name = "remote_protocol_java_proto",
src = "remote_protocol.proto",
use_grpc_plugin = True,
java_grpc_library(
name = "remote_protocol_java_grpc",
srcs = [":remote_protocol_proto"],
deps = [":remote_protocol_java_proto"],
)

py_proto_library(
Expand All @@ -66,8 +80,8 @@ filegroup(

filegroup(
name = "dist_jars",
srcs = [
"command_server_java_proto_srcjar",
"remote_protocol_java_proto_srcjar",
] + [s + "_java_proto_srcjar" for s in FILES],
srcs = [s + "_java_proto_srcs" for s in FILES] + [
":command_server_java_grpc_srcs",
":remote_protocol_java_grpc_srcs",
],
)
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/remote",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:remote_protocol_java_grpc",
"//src/main/protobuf:remote_protocol_java_proto",
"//third_party:api_client",
"//third_party:guava",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package(default_visibility = ["//src/tools/benchmark:__subpackages__"])

load("//tools/build_rules:genproto.bzl", "java_proto_library")

java_proto_library(
name = "build_data_java_proto",
deps = [":build_data_proto"],
)

proto_library(
name = "build_data_proto",
src = "build_data.proto",
srcs = ["build_data.proto"],
)

java_binary(
name = "benchmark",
srcs = glob(["*.java"]),
main_class = "com.google.devtools.build.benchmark.Main",
deps = [
":build_data_proto",
":build_data_java_proto",
"//src/main/java/com/google/devtools/build/lib:shell",
"//src/main/java/com/google/devtools/build/lib:vfs",
"//src/main/java/com/google/devtools/common/options",
Expand All @@ -29,7 +32,7 @@ java_library(
testonly = 1,
srcs = glob(["*.java"]),
deps = [
":build_data_proto",
":build_data_java_proto",
"//src/main/java/com/google/devtools/build/lib:shell",
"//src/main/java/com/google/devtools/build/lib:vfs",
"//src/main/java/com/google/devtools/common/options",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ java_test(
srcs = ["BazelBuildCaseTest.java"],
deps = [
"//src/tools/benchmark/java/com/google/devtools/build/benchmark:benchmark_lib",
"//src/tools/benchmark/java/com/google/devtools/build/benchmark:build_data_proto",
"//src/tools/benchmark/java/com/google/devtools/build/benchmark:build_data_java_proto",
"//third_party:guava",
"//third_party:junit4",
"//third_party:truth",
Expand All @@ -29,7 +29,7 @@ java_test(
deps = [
"//src/main/java/com/google/devtools/build/lib:shell",
"//src/tools/benchmark/java/com/google/devtools/build/benchmark:benchmark_lib",
"//src/tools/benchmark/java/com/google/devtools/build/benchmark:build_data_proto",
"//src/tools/benchmark/java/com/google/devtools/build/benchmark:build_data_java_proto",
"//third_party:guava",
"//third_party:junit4",
"//third_party:truth",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib:vfs",
"//src/main/java/com/google/devtools/build/lib/remote",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:remote_protocol_java_grpc",
"//src/main/protobuf:remote_protocol_java_proto",
"//third_party:guava",
"//third_party:hazelcast",
Expand Down
92 changes: 0 additions & 92 deletions tools/build_rules/genproto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,6 @@

proto_filetype = [".proto"]

def gensrcjar_impl(ctx):
out = ctx.outputs.srcjar

ctx.action(
command=' '.join([
"JAR='%s'" % ctx.executable._jar.path,
"OUTPUT='%s'" % out.path,
"PROTO_COMPILER='%s'" % ctx.executable._proto_compiler.path,
"GRPC_JAVA_PLUGIN='%s'" % ctx.executable.grpc_java_plugin.path if \
ctx.executable.grpc_java_plugin else "",
"SOURCE='%s'" % ctx.file.src.path,
ctx.executable._gensrcjar.path,
]),
inputs=([ctx.file.src] + ctx.files._gensrcjar + ctx.files._jar +
ctx.files._jdk + ctx.files._proto_compiler +
ctx.files.grpc_java_plugin),
outputs=[out],
mnemonic="GenProtoSrcJar",
use_default_shell_env=True)

return struct(runfiles=ctx.runfiles(collect_default=True))

gensrcjar = rule(
gensrcjar_impl,
attrs = {
"src": attr.label(
allow_files = proto_filetype,
single_file = True,
),
"grpc_java_plugin": attr.label(
cfg = "host",
executable = True,
single_file = True,
),
"_gensrcjar": attr.label(
default = Label("//tools/build_rules:gensrcjar"),
cfg = "host",
executable = True,
),
# TODO(bazel-team): this should be a hidden attribute with a default
# value, but Skylark needs to support select first.
"_proto_compiler": attr.label(
default = Label("//third_party/protobuf:protoc"),
allow_files = True,
cfg = "host",
executable = True,
single_file = True,
),
"_jar": attr.label(
default = Label("@bazel_tools//tools/jdk:jar"),
allow_files = True,
cfg = "host",
executable = True,
single_file = True,
),
# The jdk dependency is required to ensure dependent libraries are found
# when we invoke jar (see issue #938).
# TODO(bazel-team): Figure out why we need to pull this in explicitly;
# the jar dependency above should just do the right thing on its own.
"_jdk": attr.label(
default = Label("@bazel_tools//tools/jdk:jdk"),
allow_files = True,
),
},
outputs = {"srcjar": "lib%{name}.srcjar"},
)

def cc_grpc_library(name, src):
basename = src[:-len(".proto")]
protoc_label = str(Label("//third_party/protobuf:protoc"))
Expand All @@ -107,28 +40,3 @@ def cc_grpc_library(name, src):
deps = [str(Label("//third_party/grpc:grpc++_unsecure"))],
includes = ["."])

# TODO(bazel-team): support proto => proto dependencies too
def java_proto_library(name, src, use_grpc_plugin=False):
grpc_java_plugin = None
if use_grpc_plugin:
grpc_java_plugin = str(Label("//third_party/grpc:grpc-java-plugin"))

gensrcjar(name=name + "_srcjar", src=src, grpc_java_plugin=grpc_java_plugin)
deps = [str(Label("//third_party/protobuf:protobuf_java"))]
if use_grpc_plugin:
deps += [
str(Label("//third_party/grpc:grpc-jar")),
str(Label("//third_party:guava")),
]
native.java_library(
name=name,
srcs=[name + "_srcjar"],
deps=deps,
# The generated code has lots of 'rawtypes' warnings.
javacopts=["-Xlint:-rawtypes"],
)

def proto_java_library(name, src):
print("Deprecated: use java_proto_library() instead, proto_java_library " +
"will be removed in version 0.2.1")
java_proto_library(name, src)
Loading

0 comments on commit 6073f1d

Please sign in to comment.