2019-03-12 01:52:12 +08:00
|
|
|
"""Build rule for java_grpc_library."""
|
|
|
|
|
2019-03-08 08:22:54 +08:00
|
|
|
load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain")
|
|
|
|
|
2019-04-20 02:15:38 +08:00
|
|
|
_JavaRpcToolchainInfo = provider(
|
|
|
|
fields = [
|
|
|
|
"host_javabase",
|
|
|
|
"java_toolchain",
|
|
|
|
"plugin",
|
|
|
|
"plugin_arg",
|
|
|
|
"protoc",
|
|
|
|
"runtime",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def _java_rpc_toolchain_impl(ctx):
|
|
|
|
return [
|
|
|
|
_JavaRpcToolchainInfo(
|
|
|
|
host_javabase = ctx.attr._host_javabase,
|
|
|
|
java_toolchain = ctx.attr._java_toolchain,
|
|
|
|
plugin = ctx.executable.plugin,
|
|
|
|
plugin_arg = ctx.attr.plugin_arg,
|
|
|
|
protoc = ctx.executable._protoc,
|
|
|
|
runtime = ctx.attr.runtime,
|
|
|
|
),
|
|
|
|
platform_common.ToolchainInfo(), # Magic for b/78647825
|
|
|
|
]
|
|
|
|
|
|
|
|
java_rpc_toolchain = rule(
|
|
|
|
attrs = {
|
|
|
|
# This attribute has a "magic" name recognized by the native DexArchiveAspect (b/78647825).
|
|
|
|
"runtime": attr.label_list(
|
|
|
|
cfg = "target",
|
|
|
|
providers = [JavaInfo],
|
|
|
|
),
|
|
|
|
"plugin": attr.label(
|
|
|
|
cfg = "host",
|
|
|
|
executable = True,
|
|
|
|
),
|
|
|
|
"plugin_arg": attr.string(),
|
|
|
|
"_protoc": attr.label(
|
|
|
|
cfg = "host",
|
|
|
|
default = Label("@com_google_protobuf//:protoc"),
|
|
|
|
executable = True,
|
|
|
|
),
|
|
|
|
"_java_toolchain": attr.label(
|
2019-06-07 04:54:32 +08:00
|
|
|
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
|
2019-04-20 02:15:38 +08:00
|
|
|
),
|
|
|
|
"_host_javabase": attr.label(
|
|
|
|
cfg = "host",
|
|
|
|
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
provides = [
|
|
|
|
_JavaRpcToolchainInfo,
|
|
|
|
platform_common.ToolchainInfo,
|
|
|
|
],
|
|
|
|
implementation = _java_rpc_toolchain_impl,
|
|
|
|
)
|
|
|
|
|
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 _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-04-20 02:15:38 +08:00
|
|
|
toolchain = ctx.attr._toolchain[_JavaRpcToolchainInfo]
|
2019-03-01 12:30:48 +08:00
|
|
|
srcs = ctx.attr.srcs[0][ProtoInfo].direct_sources
|
2019-06-06 01:06:02 +08:00
|
|
|
descriptor_set_in = ctx.attr.srcs[0][ProtoInfo].transitive_descriptor_sets
|
2019-02-12 07:17:34 +08:00
|
|
|
|
2019-03-05 23:44:22 +08:00
|
|
|
srcjar = ctx.actions.declare_file("%s-proto-gensrc.jar" % ctx.label.name)
|
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
args = ctx.actions.args()
|
2019-04-20 02:15:38 +08:00
|
|
|
args.add(toolchain.plugin, format = "--plugin=protoc-gen-rpc-plugin=%s")
|
|
|
|
args.add("--rpc-plugin_out={0}:{1}".format(toolchain.plugin_arg, srcjar.path))
|
2019-06-06 01:06:02 +08:00
|
|
|
args.add_joined("--descriptor_set_in", descriptor_set_in, join_with = ":")
|
2019-02-12 07:17:34 +08:00
|
|
|
args.add_all(srcs, map_each = _path_ignoring_repository)
|
|
|
|
|
2019-02-28 19:08:11 +08:00
|
|
|
ctx.actions.run(
|
2019-06-06 01:06:02 +08:00
|
|
|
inputs = depset([toolchain.plugin] + srcs, transitive = [descriptor_set_in]),
|
2019-03-05 23:44:22 +08:00
|
|
|
outputs = [srcjar],
|
2019-04-20 02:15:38 +08:00
|
|
|
executable = toolchain.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,
|
2019-04-20 02:15:38 +08:00
|
|
|
java_toolchain = find_java_toolchain(ctx, toolchain.java_toolchain),
|
|
|
|
host_javabase = find_java_runtime_toolchain(ctx, toolchain.host_javabase),
|
2019-03-05 23:44:22 +08:00
|
|
|
source_jars = [srcjar],
|
2019-02-12 07:17:34 +08:00
|
|
|
output = ctx.outputs.jar,
|
2019-04-20 02:15:38 +08:00
|
|
|
output_source_jar = ctx.outputs.srcjar,
|
2019-02-12 07:17:34 +08:00
|
|
|
deps = [
|
|
|
|
java_common.make_non_strict(deps_java_info),
|
2019-04-20 02:15:38 +08:00
|
|
|
] + [dep[JavaInfo] for dep in toolchain.runtime],
|
2019-01-26 08:13:46 +08:00
|
|
|
)
|
2019-04-20 02:15:38 +08:00
|
|
|
|
2019-02-12 07:17:34 +08:00
|
|
|
return [java_info]
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-04-20 02:15:38 +08:00
|
|
|
_java_grpc_library = rule(
|
2017-06-23 04:06:49 +08:00
|
|
|
attrs = {
|
|
|
|
"srcs": attr.label_list(
|
|
|
|
mandatory = True,
|
2019-02-28 19:08:11 +08:00
|
|
|
allow_empty = False,
|
2019-03-01 12:30:48 +08:00
|
|
|
providers = [ProtoInfo],
|
2017-06-23 04:06:49 +08:00
|
|
|
),
|
2019-02-12 07:17:34 +08:00
|
|
|
"deps": attr.label_list(
|
|
|
|
mandatory = True,
|
2019-02-28 19:08:11 +08:00
|
|
|
allow_empty = False,
|
2019-02-12 07:17:34 +08:00
|
|
|
providers = [JavaInfo],
|
|
|
|
),
|
2019-04-20 02:15:38 +08:00
|
|
|
"_toolchain": attr.label(
|
|
|
|
default = Label("//compiler:java_grpc_library_toolchain"),
|
2017-06-23 04:06:49 +08:00
|
|
|
),
|
2019-04-20 02:15:38 +08:00
|
|
|
},
|
|
|
|
fragments = ["java"],
|
|
|
|
outputs = {
|
|
|
|
"jar": "lib%{name}.jar",
|
|
|
|
"srcjar": "lib%{name}-src.jar",
|
|
|
|
},
|
|
|
|
provides = [JavaInfo],
|
|
|
|
implementation = _java_rpc_library_impl,
|
|
|
|
)
|
|
|
|
|
|
|
|
_java_lite_grpc_library = rule(
|
|
|
|
attrs = {
|
|
|
|
"srcs": attr.label_list(
|
2019-02-12 07:17:34 +08:00
|
|
|
mandatory = True,
|
2019-04-20 02:15:38 +08:00
|
|
|
allow_empty = False,
|
|
|
|
providers = ["proto"],
|
2017-06-23 04:06:49 +08:00
|
|
|
),
|
2019-04-20 02:15:38 +08:00
|
|
|
"deps": attr.label_list(
|
|
|
|
mandatory = True,
|
|
|
|
allow_empty = False,
|
|
|
|
providers = [JavaInfo],
|
2019-02-12 07:17:34 +08:00
|
|
|
),
|
2019-04-20 02:15:38 +08:00
|
|
|
# This attribute has a "magic" name recognized by the native DexArchiveAspect (b/78647825).
|
|
|
|
"_toolchain": attr.label(
|
|
|
|
default = Label("//compiler:java_lite_grpc_library_toolchain"),
|
2019-02-12 07:17:34 +08:00
|
|
|
),
|
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,
|
|
|
|
**kwargs):
|
2019-04-24 03:07:29 +08:00
|
|
|
"""Generates gRPC Java code for services in a `proto_library`.
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-04-24 03:07:29 +08:00
|
|
|
This rule only generates code for services; it does not generate code for
|
|
|
|
messages. You will need a separate java_proto_library or
|
2019-02-12 07:17:34 +08:00
|
|
|
java_lite_proto_library rule.
|
2017-06-23 04:06:49 +08:00
|
|
|
|
2019-01-26 08:13:46 +08:00
|
|
|
Args:
|
2019-04-24 03:07:29 +08:00
|
|
|
name: A unique name for this rule.
|
|
|
|
srcs: (List of `labels`) a single proto_library target that contains the
|
|
|
|
schema of the service.
|
|
|
|
deps: (List of `labels`) a single java_proto_library or
|
|
|
|
java_lite_proto_library target for the proto_library in srcs.
|
2019-01-26 08:13:46 +08:00
|
|
|
flavor: (str) "normal" (default) for normal proto runtime. "lite"
|
2019-04-24 03:07:29 +08:00
|
|
|
for the lite runtime.
|
|
|
|
**kwargs: Other common attributes
|
2019-01-26 08:13:46 +08:00
|
|
|
"""
|
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-04-20 02:15:38 +08:00
|
|
|
if flavor == None or flavor == "normal":
|
|
|
|
_java_grpc_library(
|
2019-02-12 07:17:34 +08:00
|
|
|
name = name,
|
2019-04-20 02:15:38 +08:00
|
|
|
srcs = srcs,
|
|
|
|
deps = deps,
|
2019-02-12 07:17:34 +08:00
|
|
|
**kwargs
|
|
|
|
)
|
2019-04-20 02:15:38 +08:00
|
|
|
elif flavor == "lite":
|
|
|
|
_java_lite_grpc_library(
|
|
|
|
name = name,
|
|
|
|
srcs = srcs,
|
|
|
|
deps = deps,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
fail("Flavor must be normal or lite")
|