2017-06-23 04:06:49 +08:00
|
|
|
def _path_ignoring_repository(f):
|
|
|
|
if (len(f.owner.workspace_root) == 0):
|
|
|
|
return f.short_path
|
2017-09-21 22:18:55 +08:00
|
|
|
return f.path[f.path.find(f.owner.workspace_root)+len(f.owner.workspace_root)+1:]
|
2017-06-23 04:06:49 +08:00
|
|
|
|
|
|
|
def _gensource_impl(ctx):
|
|
|
|
if len(ctx.attr.srcs) > 1:
|
|
|
|
fail("Only one src value supported", "srcs")
|
|
|
|
for s in ctx.attr.srcs:
|
|
|
|
if s.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, s.label))
|
|
|
|
# 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._java_plugin] + srcs + includes,
|
|
|
|
outputs = [srcdotjar],
|
|
|
|
executable = ctx.executable._protoc,
|
|
|
|
arguments = [
|
|
|
|
"--plugin=protoc-gen-grpc-java=" + ctx.executable._java_plugin.path,
|
|
|
|
"--grpc-java_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]
|
2017-11-07 09:11:34 +08:00
|
|
|
+ [_path_ignoring_repository(src) for src in srcs])
|
2017-06-23 04:06:49 +08:00
|
|
|
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",
|
2017-11-07 09:11:34 +08:00
|
|
|
"lite", # Not currently supported
|
2017-06-23 04:06:49 +08:00
|
|
|
],
|
|
|
|
default = "normal",
|
|
|
|
),
|
|
|
|
"enable_deprecated": attr.bool(
|
|
|
|
default = False,
|
|
|
|
),
|
|
|
|
"_protoc": attr.label(
|
|
|
|
default = Label("@com_google_protobuf//:protoc"),
|
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
),
|
|
|
|
"_java_plugin": attr.label(
|
2018-02-18 02:42:46 +08:00
|
|
|
default = Label("//compiler:grpc_java_plugin"),
|
2017-06-23 04:06:49 +08:00
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
outputs = {
|
|
|
|
"srcjar": "%{name}.srcjar",
|
|
|
|
},
|
|
|
|
implementation = _gensource_impl,
|
|
|
|
)
|
|
|
|
|
|
|
|
def java_grpc_library(name, srcs, deps, flavor=None,
|
|
|
|
enable_deprecated=None, visibility=None,
|
|
|
|
**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"
|
|
|
|
|
|
|
|
if len(deps) > 1:
|
|
|
|
print("Multiple values in 'deps' is deprecated in " + name)
|
|
|
|
|
|
|
|
gensource_name = name + "__do_not_reference__srcjar"
|
|
|
|
_gensource(
|
|
|
|
name = gensource_name,
|
|
|
|
srcs = srcs,
|
|
|
|
flavor = flavor,
|
|
|
|
enable_deprecated = enable_deprecated,
|
|
|
|
visibility = ["//visibility:private"],
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
added_deps = [
|
2018-03-31 12:32:15 +08:00
|
|
|
"@io_grpc_grpc_java//core",
|
|
|
|
"@io_grpc_grpc_java//stub",
|
|
|
|
"@io_grpc_grpc_java//protobuf",
|
2017-08-12 00:43:08 +08:00
|
|
|
"@com_google_guava_guava//jar",
|
2017-06-23 04:06:49 +08:00
|
|
|
]
|
|
|
|
if flavor == "normal":
|
2018-01-10 03:02:03 +08:00
|
|
|
added_deps += ["@com_google_protobuf//:protobuf_java"]
|
2017-06-23 04:06:49 +08:00
|
|
|
elif flavor == "lite":
|
|
|
|
# TODO: This is currently blocked on https://github.com/google/protobuf/issues/2762
|
|
|
|
added_deps += ["@com_google_protobuf_java_lite//:protobuf_java_lite"]
|
|
|
|
else:
|
|
|
|
fail("Unknown flavor type", "flavor")
|
|
|
|
|
|
|
|
native.java_library(
|
|
|
|
name = name,
|
|
|
|
srcs = [gensource_name],
|
|
|
|
visibility = visibility,
|
|
|
|
deps = [
|
|
|
|
"@com_google_code_findbugs_jsr305//jar",
|
|
|
|
] + deps + added_deps,
|
|
|
|
**kwargs
|
|
|
|
)
|