Skip to content

Commit

Permalink
build: Add Bazel java_grpc_library rule
Browse files Browse the repository at this point in the history
Bazel third party dependencies are specified in repositories.bzl which
gives the consumer the ability to opt-out of any dependencies they use
directly in their own project.

Fixes #2756
  • Loading branch information
simonhorlick authored and ejona86 committed Jun 22, 2017
1 parent 51ce204 commit 6d9e149
Show file tree
Hide file tree
Showing 13 changed files with 560 additions and 0 deletions.
Empty file added BUILD.bazel
Empty file.
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
workspace(name = "grpc_java")

load("//:repositories.bzl", "grpc_java_repositories")

grpc_java_repositories()
12 changes: 12 additions & 0 deletions compiler/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cc_binary(
name = "grpc_java_plugin",
srcs = [
"src/java_plugin/cpp/java_generator.cpp",
"src/java_plugin/cpp/java_generator.h",
"src/java_plugin/cpp/java_plugin.cpp",
],
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:protoc_lib",
],
)
13 changes: 13 additions & 0 deletions context/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
java_library(
name = "context",
srcs = glob([
"src/main/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"@com_google_code_findbugs_jsr305//jar",
"@com_google_errorprone_error_prone_annotations//jar",
"@com_google_guava//jar",
"@com_google_instrumentation_api//jar",
],
)
60 changes: 60 additions & 0 deletions core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
java_library(
name = "core",
srcs = glob([
"src/main/java/io/grpc/*.java",
]),
resources = glob([
"src/main/resources/**",
]),
visibility = ["//visibility:public"],
deps = [
"//context",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_errorprone_error_prone_annotations//jar",
"@com_google_guava//jar",
"@com_google_instrumentation_api//jar",
],
)

java_library(
name = "inprocess",
srcs = glob([
"src/main/java/io/grpc/inprocess/*.java",
]),
visibility = ["//visibility:public"],
deps = [
":internal",
":core",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava//jar",
],
)

java_library(
name = "internal",
srcs = glob([
"src/main/java/io/grpc/internal/*.java",
]),
visibility = ["//:__subpackages__"],
deps = [
":core",
"//context",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_errorprone_error_prone_annotations//jar",
"@com_google_guava//jar",
"@com_google_instrumentation_api//jar",
],
)

java_library(
name = "util",
srcs = glob([
"src/main/java/io/grpc/util/*.java",
]),
visibility = ["//visibility:public"],
deps = [
":core",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava//jar",
],
)
131 changes: 131 additions & 0 deletions java_grpc_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
def _path_ignoring_repository(f):
if (len(f.owner.workspace_root) == 0):
return f.short_path
return f.path[len(f.owner.workspace_root)+1:]

def _gensource_impl(ctx):
if len(ctx.attr.srcs) > 1:
fail("Only one src value supported", "srcs")
for s in ctx.attr.srcs:
if s.label.package != ctx.label.package:
print(("in srcs attribute of {0}: Proto source with label {1} should be in "
+ "same package as consuming rule").format(ctx.label, s.label))
# Use .jar since .srcjar makes protoc think output will be a directory
srcdotjar = ctx.new_file(ctx.label.name + "_src.jar")

srcs = [f for dep in ctx.attr.srcs for f in dep.proto.direct_sources]
includes = [f for dep in ctx.attr.srcs for f in dep.proto.transitive_imports]

flavor = ctx.attr.flavor
if flavor == "normal":
flavor = ""
ctx.action(
inputs = [ctx.executable._java_plugin] + srcs + includes,
outputs = [srcdotjar],
executable = ctx.executable._protoc,
arguments = [
"--plugin=protoc-gen-grpc-java=" + ctx.executable._java_plugin.path,
"--grpc-java_out={0},enable_deprecated={1}:{2}"
.format(flavor, str(ctx.attr.enable_deprecated).lower(), srcdotjar.path)]
+ ["-I{0}={1}".format(_path_ignoring_repository(include), include.path) for include in includes]
+ [src.short_path for src in srcs])
ctx.action(
command = "cp $1 $2",
inputs = [srcdotjar],
outputs = [ctx.outputs.srcjar],
arguments = [srcdotjar.path, ctx.outputs.srcjar.path])

_gensource = rule(
attrs = {
"srcs": attr.label_list(
mandatory = True,
non_empty = True,
providers = ["proto"],
),
"flavor": attr.string(
values = [
"normal",
"lite", # Not currently supported
],
default = "normal",
),
"enable_deprecated": attr.bool(
default = False,
),
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
executable = True,
cfg = "host",
),
"_java_plugin": attr.label(
default = Label("@grpc_java//compiler:grpc_java_plugin"),
executable = True,
cfg = "host",
),
},
outputs = {
"srcjar": "%{name}.srcjar",
},
implementation = _gensource_impl,
)

def java_grpc_library(name, srcs, deps, flavor=None,
enable_deprecated=None, visibility=None,
**kwargs):
"""Generates and compiles gRPC Java sources for services defined in a proto
file. This rule is compatible with java_proto_library and java_lite_proto_library.
Do note that this rule only scans through the proto file for RPC services. It
does not generate Java classes for proto messages. You will need a separate
java_proto_library or java_lite_proto_library rule.
Args:
name: (str) A unique name for this rule. Required.
srcs: (list) a single proto_library target that contains the schema of the
service. Required.
deps: (list) a single java_proto_library target for the proto_library in
srcs. Required.
flavor: (str) "normal" (default) for normal proto runtime. "lite"
for the lite runtime.
visibility: (list) the visibility list
**kwargs: Passed through to generated targets
"""
if flavor == None:
flavor = "normal"

if len(deps) > 1:
print("Multiple values in 'deps' is deprecated in " + name)

gensource_name = name + "__do_not_reference__srcjar"
_gensource(
name = gensource_name,
srcs = srcs,
flavor = flavor,
enable_deprecated = enable_deprecated,
visibility = ["//visibility:private"],
**kwargs
)

added_deps = [
"@grpc_java//core",
"@grpc_java//stub",
"@grpc_java//protobuf",
"@com_google_guava//jar",
]
if flavor == "normal":
added_deps += ["@com_google_protobuf_java//:protobuf_java"]
elif flavor == "lite":
# TODO: This is currently blocked on https://github.com/google/protobuf/issues/2762
added_deps += ["@com_google_protobuf_java_lite//:protobuf_java_lite"]
else:
fail("Unknown flavor type", "flavor")

native.java_library(
name = name,
srcs = [gensource_name],
visibility = visibility,
deps = [
"@com_google_code_findbugs_jsr305//jar",
] + deps + added_deps,
**kwargs
)
28 changes: 28 additions & 0 deletions netty/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
java_library(
name = "netty",
srcs = glob([
"src/main/java/**/*.java",
"third_party/netty/java/**/*.java",
]),
resources = glob([
"src/main/resources/**",
]),
visibility = ["//visibility:public"],
deps = [
"//core",
"//core:internal",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_errorprone_error_prone_annotations//jar",
"@com_google_guava//jar",
"@io_netty_buffer//jar",
"@io_netty_codec//jar",
"@io_netty_codec_http//jar",
"@io_netty_codec_http2//jar",
"@io_netty_codec_socks//jar",
"@io_netty_common//jar",
"@io_netty_handler//jar",
"@io_netty_handler_proxy//jar",
"@io_netty_resolver//jar",
"@io_netty_transport//jar",
],
)
19 changes: 19 additions & 0 deletions okhttp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
java_library(
name = "okhttp",
srcs = glob([
"third_party/okhttp/java/**/*.java",
"src/main/java/**/*.java",
]),
resources = glob([
"src/main/resources/**",
]),
visibility = ["//visibility:public"],
deps = [
"//core",
"//core:internal",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava//jar",
"@com_squareup_okhttp//jar",
"@com_squareup_okio//jar",
],
)
16 changes: 16 additions & 0 deletions protobuf-lite/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
java_library(
name = "protobuf_lite",
srcs = glob([
"src/main/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"//core",
"//core:internal",
"@com_google_api_grpc_google_common_protos//jar",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava//jar",
"@com_google_protobuf//:protobuf_java",
"@com_google_protobuf//:protobuf_java_util",
],
)
13 changes: 13 additions & 0 deletions protobuf-nano/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
java_library(
name = "protobuf_nano",
srcs = glob([
"src/main/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"//core",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava//jar",
"@com_google_protobuf_nano_protobuf_javanano//jar",
],
)
16 changes: 16 additions & 0 deletions protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
java_library(
name = "protobuf",
srcs = glob([
"src/main/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"//core",
"//protobuf-lite:protobuf_lite",
"@com_google_api_grpc_google_common_protos//jar",
"@com_google_code_findbugs_jsr305//jar",
"@com_google_guava//jar",
"@com_google_protobuf//:protobuf_java",
"@com_google_protobuf//:protobuf_java_util",
],
)
Loading

0 comments on commit 6d9e149

Please sign in to comment.