Created
June 26, 2018 23:52
-
-
Save hsyed/7b79b6b4797ccbd37d00b7de1033a36a to your computer and use it in GitHub Desktop.
Adapted version of java_grpc_library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adapted from grpc-java | |
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library") | |
load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library") | |
def _path_ignoring_repository(f): | |
if (len(f.owner.workspace_root) == 0): | |
return f.short_path | |
return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:] | |
def _gensource_impl(ctx): | |
if len(ctx.attr.srcs) > 1: | |
fail("Only one src value supported", "srcs") | |
# Use .jar since .srcjar makes protoc think output will be a directory | |
srcdotjar = ctx.new_file(ctx.label.name + "_src.jar") | |
srcs = [f for dep in ctx.attr.srcs for f in dep.proto.direct_sources] | |
includes = [f for dep in ctx.attr.srcs for f in dep.proto.transitive_imports] | |
flavor = ctx.attr.flavor | |
if flavor == "normal": | |
flavor = "" | |
ctx.action( | |
inputs = [ctx.executable._kotlin_plugin] + srcs + includes, | |
outputs = [srcdotjar], | |
executable = ctx.executable._protoc, | |
arguments = [ | |
"--plugin=protoc-gen-grpc-kotlin=" + ctx.executable._kotlin_plugin.path, | |
"--grpc-kotlin_out={0},enable_deprecated={1}:{2}".format(flavor, str(ctx.attr.enable_deprecated).lower(), srcdotjar.path), | |
] + | |
["-I{0}={1}".format(_path_ignoring_repository(include), include.path) for include in includes] + | |
[_path_ignoring_repository(src) for src in srcs], | |
) | |
ctx.action( | |
command = "cp $1 $2", | |
inputs = [srcdotjar], | |
outputs = [ctx.outputs.srcjar], | |
arguments = [srcdotjar.path, ctx.outputs.srcjar.path], | |
) | |
_gensource = rule( | |
attrs = { | |
"srcs": attr.label_list( | |
mandatory = True, | |
non_empty = True, | |
providers = ["proto"], | |
), | |
"flavor": attr.string( | |
values = [ | |
"normal", | |
"lite", # Not currently supported | |
], | |
default = "normal", | |
), | |
"enable_deprecated": attr.bool( | |
default = False, | |
), | |
"_protoc": attr.label( | |
default = Label("@com_google_protobuf//:protoc"), | |
executable = True, | |
cfg = "host", | |
), | |
"_kotlin_plugin": attr.label( | |
default = Label("//pkg/grpc/gen/kotlin"), | |
executable = True, | |
cfg = "host", | |
), | |
}, | |
outputs = { | |
"srcjar": "%{name}.srcjar", | |
}, | |
implementation = _gensource_impl, | |
) | |
def kt_grpc_library( | |
name, | |
srcs, | |
deps, | |
flavor = None, | |
enable_deprecated = None, | |
visibility = None, | |
exports = [], | |
**kwargs): | |
"""Generates and compiles gRPC Java sources for services defined in a proto | |
file. This rule is compatible with java_proto_library and java_lite_proto_library. | |
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 | |
java_proto_library or java_lite_proto_library rule. | |
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 | |
""" | |
if flavor == None: | |
flavor = "normal" | |
gensource_name = name + "_gen.srcjar" | |
_gensource( | |
name = gensource_name, | |
srcs = srcs, | |
flavor = flavor, | |
enable_deprecated = enable_deprecated, | |
visibility = ["//visibility:private"], | |
**kwargs | |
) | |
deps += [ | |
"@io_grpc_grpc_java//stub", | |
"@io_grpc_grpc_java//core", | |
"@io_grpc_grpc_java//protobuf", | |
"@com_google_protobuf//:protobuf_java", | |
"@com_google_guava_guava//jar", | |
"//third_party/kotlin:kotlinx_coroutines", | |
"//pkg/grpc/stub", | |
] | |
kt_jvm_library( | |
name = name, | |
srcs = [gensource_name], | |
visibility = visibility, | |
deps = deps, | |
exports = exports + [ | |
"//pkg/grpc/stub", | |
"@io_grpc_grpc_java//core", | |
], | |
**kwargs | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment