Skip to content

Commit 253c24f

Browse files
steineldarjameswardaragos
authored
Add kt_jvm_proto_helper as individual rule. (#396)
* Add kt_jvm_proto_helper as individual rule. This allows the BUILD file to have separate java proto and kt proto rules in case there is a mix of java and proto projects in the same repo, and there are stricter requirement to the output (e.g. leaner dependency chains, smaller binaries for pure-java outputs etc.). This also bypasses an issue in the other rules where the kt_jvm_*_library would compile the whole dependency chain to kotlin, and if the bazel repo is large with many proto dependencies, an can easily end up caching all of these when containing the same classes. If only some of these are refreshed / synced on changes you can easily end up with an IDE claiming your new fields still dont' exist, or removed classes are still there etc. * Also make rule for gRPC part. Which makes btth kr_proto and kt_grpc helper functions. * Alternate approach to support java targets * Fix error introduced during merge --------- Co-authored-by: James Ward <[email protected]> Co-authored-by: aragos <[email protected]>
1 parent e4b88c9 commit 253c24f

File tree

1 file changed

+107
-37
lines changed

1 file changed

+107
-37
lines changed

kt_jvm_grpc.bzl

Lines changed: 107 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,74 @@ def kt_jvm_grpc_library(
160160
features = features,
161161
)
162162

163+
def kt_jvm_grpc_no_java_library(
164+
name,
165+
srcs = None,
166+
deps = None,
167+
java_grpc_dep = None,
168+
tags = None,
169+
testonly = None, # None to not override Bazel's default
170+
compatible_with = None,
171+
restricted_to = None,
172+
visibility = None,
173+
deprecation = None,
174+
features = []):
175+
"""This rule compiles Kotlin APIs for gRPC services from the proto_library in srcs.
176+
177+
Unlike kt_jvm_grpc_library, this rule will not generate a java_grpc_library target
178+
implicitly but instead expects one as an input to java_grpc_dep.
179+
180+
For standard attributes, see:
181+
https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
182+
183+
Args:
184+
name: A name for the target
185+
srcs: Exactly one proto_library target to generate Kotlin APIs for
186+
deps: Exactly one java_proto_library target for srcs[0]
187+
java_grpc_dep: java_grpc_library target for the java_proto_library in deps
188+
tags: A list of string tags passed to generated targets.
189+
testonly: Whether this target is intended only for tests.
190+
compatible_with: Standard attribute
191+
restricted_to: Standard attribute
192+
visibility: A list of targets allowed to depend on this rule.
193+
deprecation: Standard attribute
194+
features: Features enabled.
195+
"""
196+
srcs = srcs or []
197+
deps = deps or []
198+
199+
if len(srcs) != 1:
200+
fail("Expected exactly one src", "srcs")
201+
if len(deps) != 1:
202+
fail("Expected exactly one dep", "deps")
203+
if not java_grpc_dep:
204+
fail("A java_grpc_dep is required", "java_grpc_dep")
205+
206+
kt_grpc_label = ":%s_DO_NOT_DEPEND_kt_grpc" % name
207+
kt_grpc_name = kt_grpc_label[1:]
208+
209+
_kt_grpc_generate_code(
210+
name = kt_grpc_name,
211+
srcs = srcs,
212+
deps = deps,
213+
)
214+
215+
kt_jvm_library(
216+
name = name,
217+
srcs = [kt_grpc_label],
218+
deps = [
219+
java_grpc_dep,
220+
"@com_github_grpc_grpc_kotlin//stub/src/main/java/io/grpc/kotlin:stub",
221+
"@com_github_grpc_grpc_kotlin//stub/src/main/java/io/grpc/kotlin:context",
222+
],
223+
compatible_with = compatible_with,
224+
restricted_to = restricted_to,
225+
testonly = testonly,
226+
visibility = visibility,
227+
deprecation = deprecation,
228+
features = features,
229+
)
230+
163231
def _get_real_short_path(file):
164232
"""Returns the correct short path file name to be used by protoc."""
165233
short_path = file.short_path
@@ -200,7 +268,6 @@ def _kt_jvm_proto_library_helper_impl(ctx):
200268
outputs = [gen_src_dir],
201269
executable = ctx.executable._protoc,
202270
arguments = [protoc_args],
203-
mnemonic = "KtProtoGenerator",
204271
progress_message = "Generating kotlin proto extensions for " +
205272
", ".join([
206273
str(dep.label)
@@ -252,6 +319,7 @@ _kt_jvm_proto_library_helper = rule(
252319
def kt_jvm_proto_library(
253320
name,
254321
deps = None,
322+
java_deps = None,
255323
tags = None,
256324
testonly = None,
257325
compatible_with = None,
@@ -267,15 +335,17 @@ def kt_jvm_proto_library(
267335
See also https://developers.google.com/protocol-buffers/docs/kotlintutorial for how to interact
268336
with the generated Kotlin representation.
269337
270-
Note that the rule will also export the java version of the same protos as Kotlin protos depend
271-
on the java version under the hood.
338+
If "java_deps" are set, they must be exactly the java_proto_library targets for "deps". If
339+
"java_deps" are not set, this rule will generate and export the java protos for all "deps",
340+
using "flavor" to determine their type.
272341
273342
For standard attributes, see:
274343
https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
275344
276345
Args:
277346
name: A name for the target
278347
deps: One or more proto_library targets to turn into Kotlin.
348+
java_deps: (optional) java_proto_library targets corresponding to deps
279349
tags: Standard attribute
280350
testonly: Standard attribute
281351
compatible_with: Standard attribute
@@ -286,40 +356,43 @@ def kt_jvm_proto_library(
286356
deprecation: Standard attribute
287357
features: Standard attribute
288358
"""
289-
java_proto_target = ":%s_DO_NOT_DEPEND_java_proto" % name
290-
helper_target = ":%s_DO_NOT_DEPEND_kt_proto" % name
291-
292-
if flavor == "lite":
293-
native.java_lite_proto_library(
294-
name = java_proto_target[1:],
295-
deps = deps,
296-
testonly = testonly,
297-
compatible_with = compatible_with,
298-
visibility = ["//visibility:private"],
299-
restricted_to = restricted_to,
300-
tags = tags,
301-
deprecation = deprecation,
302-
features = features,
303-
)
359+
if (java_deps != None and len(java_deps) > 0):
360+
java_protos = java_deps
361+
java_exports = []
304362
else:
305-
native.java_proto_library(
306-
name = java_proto_target[1:],
307-
deps = deps,
308-
testonly = testonly,
309-
compatible_with = compatible_with,
310-
visibility = ["//visibility:private"],
311-
restricted_to = restricted_to,
312-
tags = tags,
313-
deprecation = deprecation,
314-
features = features,
315-
)
363+
java_proto_target = ":%s_DO_NOT_DEPEND_java_proto" % name
364+
if flavor == "lite":
365+
native.java_lite_proto_library(
366+
name = java_proto_target[1:],
367+
deps = deps,
368+
testonly = testonly,
369+
compatible_with = compatible_with,
370+
visibility = ["//visibility:private"],
371+
restricted_to = restricted_to,
372+
tags = tags,
373+
deprecation = deprecation,
374+
features = features,
375+
)
376+
else:
377+
native.java_proto_library(
378+
name = java_proto_target[1:],
379+
deps = deps,
380+
testonly = testonly,
381+
compatible_with = compatible_with,
382+
visibility = ["//visibility:private"],
383+
restricted_to = restricted_to,
384+
tags = tags,
385+
deprecation = deprecation,
386+
features = features,
387+
)
388+
java_protos = [java_proto_target]
389+
java_exports = [java_proto_target]
316390

391+
helper_target = ":%s_DO_NOT_DEPEND_kt_proto" % name
317392
_kt_jvm_proto_library_helper(
318393
name = helper_target[1:],
319394
proto_deps = deps,
320-
deps = [
321-
java_proto_target,
322-
],
395+
deps = java_protos,
323396
testonly = testonly,
324397
compatible_with = compatible_with,
325398
visibility = ["//visibility:private"],
@@ -334,11 +407,8 @@ def kt_jvm_proto_library(
334407
srcs = [helper_target + ".srcjar"],
335408
deps = [
336409
"@maven//:com_google_protobuf_protobuf_kotlin",
337-
java_proto_target,
338-
],
339-
exports = [
340-
java_proto_target,
341-
],
410+
] + java_protos,
411+
exports = java_exports,
342412
testonly = testonly,
343413
compatible_with = compatible_with,
344414
visibility = visibility,

0 commit comments

Comments
 (0)