forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java_grpc_library.bzl
220 lines (201 loc) · 7.48 KB
/
java_grpc_library.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Build rule for java_grpc_library."""
_JavaRpcToolchainInfo = provider(
fields = [
"java_toolchain",
"java_plugins",
"plugin",
"plugin_arg",
"protoc",
"runtime",
],
)
def _java_rpc_toolchain_impl(ctx):
return [
_JavaRpcToolchainInfo(
java_toolchain = ctx.attr._java_toolchain,
java_plugins = ctx.attr.java_plugins,
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 = "exec",
executable = True,
),
"plugin_arg": attr.string(),
"_protoc": attr.label(
cfg = "exec",
default = Label("@com_google_protobuf//:protoc"),
executable = True,
),
"java_plugins": attr.label_list(
default = [],
providers = [JavaPluginInfo],
),
"_java_toolchain": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
),
},
provides = [
_JavaRpcToolchainInfo,
platform_common.ToolchainInfo,
],
implementation = _java_rpc_toolchain_impl,
)
# "repository" here is for Bazel builds that span multiple WORKSPACES.
def _path_ignoring_repository(f):
# Bazel creates a _virtual_imports directory in case the .proto source files
# need to be accessed at a path that's different from their source path:
# 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]
elif len(f.owner.workspace_root) == 0:
# |f| is in the main repository
return f.short_path
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:]
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))
toolchain = ctx.attr._toolchain[_JavaRpcToolchainInfo]
srcs = ctx.attr.srcs[0][ProtoInfo].direct_sources
descriptor_set_in = ctx.attr.srcs[0][ProtoInfo].transitive_descriptor_sets
srcjar = ctx.actions.declare_file("%s-proto-gensrc.jar" % ctx.label.name)
args = ctx.actions.args()
args.add(toolchain.plugin, format = "--plugin=protoc-gen-rpc-plugin=%s")
args.add("--rpc-plugin_out={0}:{1}".format(toolchain.plugin_arg, srcjar.path))
args.add_joined("--descriptor_set_in", descriptor_set_in, join_with = ctx.configuration.host_path_separator)
args.add_all(srcs, map_each = _path_ignoring_repository)
# TODO: Once Bazel 5.x support is dropped, add 'toolchain = None' inside the action.
ctx.actions.run(
inputs = depset([toolchain.plugin] + srcs, transitive = [descriptor_set_in]),
outputs = [srcjar],
executable = toolchain.protoc,
arguments = [args],
use_default_shell_env = True,
)
deps_java_info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
java_info = java_common.compile(
ctx,
java_toolchain = toolchain.java_toolchain[java_common.JavaToolchainInfo],
source_jars = [srcjar],
output = ctx.outputs.jar,
output_source_jar = ctx.outputs.srcjar,
plugins = [plugin[JavaPluginInfo] for plugin in toolchain.java_plugins],
deps = [
java_common.make_non_strict(deps_java_info),
] + [dep[JavaInfo] for dep in toolchain.runtime],
)
return [java_info]
_java_grpc_library = rule(
attrs = {
"srcs": attr.label_list(
mandatory = True,
allow_empty = False,
providers = [ProtoInfo],
),
"deps": attr.label_list(
mandatory = True,
allow_empty = False,
providers = [JavaInfo],
),
"_toolchain": attr.label(
default = Label("//compiler:java_grpc_library_toolchain"),
),
# TODO: Enable AEGs when Bazel 5.x support is dropped.
"_use_auto_exec_groups": attr.bool(default = False),
},
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
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(
mandatory = True,
allow_empty = False,
providers = [ProtoInfo],
),
"deps": attr.label_list(
mandatory = True,
allow_empty = False,
providers = [JavaInfo],
),
# This attribute has a "magic" name recognized by the native DexArchiveAspect (b/78647825).
"_toolchain": attr.label(
default = Label("//compiler:java_lite_grpc_library_toolchain"),
),
# TODO: Enable AEGs when Bazel 5.x support is dropped.
"_use_auto_exec_groups": attr.bool(default = False),
},
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
fragments = ["java"],
outputs = {
"jar": "lib%{name}.jar",
"srcjar": "lib%{name}-src.jar",
},
provides = [JavaInfo],
implementation = _java_rpc_library_impl,
)
def java_grpc_library(
name,
srcs,
deps,
flavor = None,
**kwargs):
"""Generates gRPC Java code for services in a `proto_library`.
This rule only generates code for services; it does not generate code for
messages. You will need a separate java_proto_library or
java_lite_proto_library rule.
Args:
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.
flavor: (str) "normal" (default) for normal proto runtime. "lite"
for the lite runtime.
**kwargs: Other common attributes
"""
if len(deps) > 1:
print("Multiple values in 'deps' is deprecated in " + name)
if flavor == None or flavor == "normal":
_java_grpc_library(
name = name,
srcs = srcs,
deps = deps,
**kwargs
)
elif flavor == "lite":
_java_lite_grpc_library(
name = name,
srcs = srcs,
deps = deps,
**kwargs
)
else:
fail("Flavor must be normal or lite")