2019-03-12 01:52:12 +08:00
|
|
|
"""Build rule for java_grpc_library."""
|
|
|
|
|
2024-04-23 07:14:47 +08:00
|
|
|
load("@rules_java//java:defs.bzl", "JavaInfo", "JavaPluginInfo", "java_common")
|
2024-04-18 07:43:21 +08:00
|
|
|
|
2019-04-20 02:15:38 +08:00
|
|
|
_JavaRpcToolchainInfo = provider(
|
|
|
|
fields = [
|
|
|
|
"java_toolchain",
|
2022-07-09 06:18:21 +08:00
|
|
|
"java_plugins",
|
2019-04-20 02:15:38 +08:00
|
|
|
"plugin",
|
|
|
|
"plugin_arg",
|
|
|
|
"protoc",
|
|
|
|
"runtime",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def _java_rpc_toolchain_impl(ctx):
|
|
|
|
return [
|
|
|
|
_JavaRpcToolchainInfo(
|
|
|
|
java_toolchain = ctx.attr._java_toolchain,
|
2022-07-09 06:18:21 +08:00
|
|
|
java_plugins = ctx.attr.java_plugins,
|
2024-01-18 08:46:15 +08:00
|
|
|
plugin = ctx.attr.plugin,
|
2019-04-20 02:15:38 +08:00
|
|
|
plugin_arg = ctx.attr.plugin_arg,
|
2024-01-18 08:46:15 +08:00
|
|
|
protoc = ctx.attr._protoc,
|
2019-04-20 02:15:38 +08:00
|
|
|
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(
|
2022-11-17 05:01:59 +08:00
|
|
|
cfg = "exec",
|
2019-04-20 02:15:38 +08:00
|
|
|
executable = True,
|
|
|
|
),
|
|
|
|
"plugin_arg": attr.string(),
|
|
|
|
"_protoc": attr.label(
|
2022-11-17 05:01:59 +08:00
|
|
|
cfg = "exec",
|
2019-04-20 02:15:38 +08:00
|
|
|
default = Label("@com_google_protobuf//:protoc"),
|
|
|
|
executable = True,
|
|
|
|
),
|
2022-07-09 06:18:21 +08:00
|
|
|
"java_plugins": attr.label_list(
|
|
|
|
default = [],
|
|
|
|
providers = [JavaPluginInfo],
|
|
|
|
),
|
2019-04-20 02:15:38 +08:00
|
|
|
"_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
|
|
|
),
|
|
|
|
},
|
|
|
|
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-07-09 04:54:36 +08:00
|
|
|
# Bazel creates a _virtual_imports directory in case the .proto source files
|
2019-07-10 04:41:19 +08:00
|
|
|
# need to be accessed at a path that's different from their source path:
|
2019-07-09 04:54:36 +08:00
|
|
|
# https://github.com/bazelbuild/bazel/blob/0.27.1/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java#L289
|
|
|
|
#
|
|
|
|
# In that case, the import path of the .proto file is the path relative to
|
|
|
|
# the virtual imports directory of the rule in question.
|
|
|
|
virtual_imports = "/_virtual_imports/"
|
|
|
|
if virtual_imports in f.path:
|
|
|
|
return f.path.split(virtual_imports)[1].split("/", 1)[1]
|
2019-07-10 04:41:19 +08:00
|
|
|
elif len(f.owner.workspace_root) == 0:
|
|
|
|
# |f| is in the main repository
|
|
|
|
return f.short_path
|
2019-07-09 04:54:36 +08:00
|
|
|
else:
|
|
|
|
# If |f| is a generated file, it will have "bazel-out/*/genfiles" prefix
|
|
|
|
# before "external/workspace", so we need to add the starting index of "external/workspace"
|
|
|
|
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()
|
2024-01-18 08:46:15 +08:00
|
|
|
args.add(toolchain.plugin.files_to_run.executable, format = "--plugin=protoc-gen-rpc-plugin=%s")
|
2019-04-20 02:15:38 +08:00
|
|
|
args.add("--rpc-plugin_out={0}:{1}".format(toolchain.plugin_arg, srcjar.path))
|
2022-12-15 04:13:31 +08:00
|
|
|
args.add_joined("--descriptor_set_in", descriptor_set_in, join_with = ctx.configuration.host_path_separator)
|
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(
|
2024-01-18 08:46:15 +08:00
|
|
|
inputs = depset(srcs, transitive = [descriptor_set_in, toolchain.plugin.files]),
|
2019-03-05 23:44:22 +08:00
|
|
|
outputs = [srcjar],
|
2024-01-18 08:46:15 +08:00
|
|
|
executable = toolchain.protoc.files_to_run,
|
2019-02-12 07:17:34 +08:00
|
|
|
arguments = [args],
|
2022-03-17 04:38:37 +08:00
|
|
|
use_default_shell_env = True,
|
2024-01-10 23:10:29 +08:00
|
|
|
toolchain = None,
|
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,
|
2020-01-31 02:44:23 +08:00
|
|
|
java_toolchain = toolchain.java_toolchain[java_common.JavaToolchainInfo],
|
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,
|
2022-07-09 06:18:21 +08:00
|
|
|
plugins = [plugin[JavaPluginInfo] for plugin in toolchain.java_plugins],
|
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
|
|
|
},
|
2023-05-31 02:48:43 +08:00
|
|
|
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
|
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,
|
2019-07-20 07:06:47 +08:00
|
|
|
providers = [ProtoInfo],
|
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
|
|
|
},
|
2023-05-31 02:48:43 +08:00
|
|
|
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
|
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")
|