2019-02-12 07:17:34 +08:00
|
|
|
# "repository" here is for Bazel builds that span multiple WORKSPACES.
|
2017-06-23 04:06:49 +08:00
|
|
|
def _path_ignoring_repository(f):
|
2019-02-12 07:17:34 +08:00
|
|
|
if len(f.owner.workspace_root) == 0:
|
2019-01-26 08:13:46 +08:00
|
|
|
return f.short_path
|
|
|
|
return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:]
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
def _create_include_path(include):
|
|
|
|
return "-I{0}={1}".format(_path_ignoring_repository(include), include.path)
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
def _java_rpc_library_impl(ctx):
|
|
|
|
if len(ctx.attr.srcs) != 1:
|
|
|
|
fail("Exactly one src value supported", "srcs")
|
|
|
|
if ctx.attr.srcs[0].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, ctx.attr.srcs[0].label))
|
2019-01-26 08:13:46 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
srcs = ctx.attr.srcs[0].proto.direct_sources
|
|
|
|
includes = ctx.attr.srcs[0].proto.transitive_imports
|
2019-01-26 08:13:46 +08:00
|
|
|
flavor = ctx.attr.flavor
|
|
|
|
if flavor == "normal":
|
|
|
|
flavor = ""
|
2019-02-12 07:17:34 +08:00
|
|
|
|
|
|
|
args = ctx.actions.args()
|
|
|
|
args.add(ctx.executable._java_plugin.path, format = "--plugin=protoc-gen-grpc-java=%s")
|
|
|
|
args.add("--grpc-java_out={0}:{1}".format(flavor, ctx.outputs.srcjar.path))
|
|
|
|
args.add_all(includes, map_each = _create_include_path)
|
|
|
|
args.add_all(srcs, map_each = _path_ignoring_repository)
|
|
|
|
|
2019-01-26 08:13:46 +08:00
|
|
|
ctx.action(
|
2019-02-12 07:17:34 +08:00
|
|
|
inputs = depset([ctx.executable._java_plugin] + srcs, transitive = [includes]),
|
|
|
|
outputs = [ctx.outputs.srcjar],
|
2019-01-26 08:13:46 +08:00
|
|
|
executable = ctx.executable._protoc,
|
2019-02-12 07:17:34 +08:00
|
|
|
arguments = [args],
|
2019-01-26 08:13:46 +08:00
|
|
|
)
|
2019-02-12 07:17:34 +08:00
|
|
|
|
|
|
|
deps_java_info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
|
|
|
|
|
|
|
|
java_info = java_common.compile(
|
|
|
|
ctx,
|
|
|
|
java_toolchain = ctx.attr._java_toolchain,
|
|
|
|
host_javabase = ctx.attr._host_javabase,
|
|
|
|
source_jars = [ctx.outputs.srcjar],
|
|
|
|
output = ctx.outputs.jar,
|
|
|
|
deps = [
|
|
|
|
java_common.make_non_strict(deps_java_info),
|
|
|
|
ctx.attr.runtime[JavaInfo],
|
|
|
|
],
|
2019-01-26 08:13:46 +08:00
|
|
|
)
|
2019-02-12 07:17:34 +08:00
|
|
|
return [java_info]
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
_java_rpc_library = rule(
|
2017-06-23 04:06:49 +08:00
|
|
|
attrs = {
|
|
|
|
"srcs": attr.label_list(
|
|
|
|
mandatory = True,
|
|
|
|
non_empty = True,
|
|
|
|
providers = ["proto"],
|
|
|
|
),
|
2019-02-12 07:17:34 +08:00
|
|
|
"deps": attr.label_list(
|
|
|
|
mandatory = True,
|
|
|
|
non_empty = True,
|
|
|
|
providers = [JavaInfo],
|
|
|
|
),
|
2017-06-23 04:06:49 +08:00
|
|
|
"flavor": attr.string(
|
|
|
|
values = [
|
|
|
|
"normal",
|
2019-02-12 07:17:34 +08:00
|
|
|
"lite",
|
2017-06-23 04:06:49 +08:00
|
|
|
],
|
|
|
|
default = "normal",
|
|
|
|
),
|
2019-02-12 07:17:34 +08:00
|
|
|
"runtime": attr.label(
|
|
|
|
mandatory = True,
|
2017-06-23 04:06:49 +08:00
|
|
|
),
|
|
|
|
"_protoc": attr.label(
|
|
|
|
default = Label("@com_google_protobuf//:protoc"),
|
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
),
|
|
|
|
"_java_plugin": attr.label(
|
2018-02-18 02:42:46 +08:00
|
|
|
default = Label("//compiler:grpc_java_plugin"),
|
2017-06-23 04:06:49 +08:00
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
),
|
2019-02-12 07:17:34 +08:00
|
|
|
"_java_toolchain": attr.label(
|
|
|
|
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
|
|
|
|
),
|
|
|
|
"_host_javabase": attr.label(
|
|
|
|
cfg = "host",
|
|
|
|
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
|
|
|
|
),
|
2017-06-23 04:06:49 +08:00
|
|
|
},
|
2019-02-12 07:17:34 +08:00
|
|
|
fragments = ["java"],
|
2017-06-23 04:06:49 +08:00
|
|
|
outputs = {
|
2019-02-12 07:17:34 +08:00
|
|
|
"jar": "lib%{name}.jar",
|
|
|
|
"srcjar": "lib%{name}-src.jar",
|
2017-06-23 04:06:49 +08:00
|
|
|
},
|
2019-02-12 07:17:34 +08:00
|
|
|
provides = [JavaInfo],
|
|
|
|
implementation = _java_rpc_library_impl,
|
2017-06-23 04:06:49 +08:00
|
|
|
)
|
|
|
|
|
2019-01-26 08:13:46 +08:00
|
|
|
def java_grpc_library(
|
|
|
|
name,
|
|
|
|
srcs,
|
|
|
|
deps,
|
|
|
|
flavor = None,
|
2019-02-12 07:17:34 +08:00
|
|
|
tags = None,
|
2019-01-26 08:13:46 +08:00
|
|
|
visibility = None,
|
|
|
|
**kwargs):
|
|
|
|
"""Generates and compiles gRPC Java sources for services defined in a proto
|
2019-02-12 07:17:34 +08:00
|
|
|
file. This rule is compatible with proto_library with java_api_version,
|
|
|
|
java_proto_library, and java_lite_proto_library.
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-01-26 08:13:46 +08:00
|
|
|
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
|
2019-02-12 07:17:34 +08:00
|
|
|
proto_library with java_api_version, java_proto_library, or
|
|
|
|
java_lite_proto_library rule.
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-01-26 08:13:46 +08:00
|
|
|
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
|
|
|
|
"""
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-01-26 08:13:46 +08:00
|
|
|
if len(deps) > 1:
|
|
|
|
print("Multiple values in 'deps' is deprecated in " + name)
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
if flavor == "lite":
|
|
|
|
inner_name = name + "__do_not_reference"
|
|
|
|
inner_visibility = ["//visibility:private"]
|
|
|
|
inner_tags = ["avoid_dep"]
|
|
|
|
runtime = "@io_grpc_grpc_java//:java_lite_grpc_library_deps__do_not_reference"
|
|
|
|
else:
|
|
|
|
inner_name = name
|
|
|
|
inner_visibility = visibility
|
|
|
|
inner_tags = tags
|
|
|
|
runtime = "@io_grpc_grpc_java//:java_grpc_library_deps__do_not_reference"
|
|
|
|
|
|
|
|
_java_rpc_library(
|
|
|
|
name = inner_name,
|
2019-01-26 08:13:46 +08:00
|
|
|
srcs = srcs,
|
2019-02-12 07:17:34 +08:00
|
|
|
deps = deps,
|
2019-01-26 08:13:46 +08:00
|
|
|
flavor = flavor,
|
2019-02-12 07:17:34 +08:00
|
|
|
runtime = runtime,
|
|
|
|
visibility = inner_visibility,
|
|
|
|
tags = inner_tags,
|
2019-01-26 08:13:46 +08:00
|
|
|
**kwargs
|
|
|
|
)
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
if flavor == "lite":
|
|
|
|
# Use java_import to work around error with android_binary:
|
|
|
|
# Dependencies on .jar artifacts are not allowed in Android binaries,
|
|
|
|
# please use a java_import to depend on...
|
|
|
|
native.java_import(
|
|
|
|
name = name,
|
|
|
|
deps = deps + [runtime],
|
|
|
|
jars = [":lib{}.jar".format(inner_name)],
|
|
|
|
srcjar = ":lib{}-src.jar".format(inner_name),
|
|
|
|
visibility = visibility,
|
|
|
|
tags = tags,
|
|
|
|
**kwargs
|
|
|
|
)
|