all: fix formatting on bzl file

This commit is contained in:
Carl Mastrangelo 2019-01-25 16:13:46 -08:00 committed by GitHub
parent f6689a1f86
commit 6c8020e584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 95 additions and 86 deletions

View File

@ -1,39 +1,43 @@
def _path_ignoring_repository(f): def _path_ignoring_repository(f):
if (len(f.owner.workspace_root) == 0): if (len(f.owner.workspace_root) == 0):
return f.short_path return f.short_path
return f.path[f.path.find(f.owner.workspace_root)+len(f.owner.workspace_root)+1:] return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:]
def _gensource_impl(ctx): def _gensource_impl(ctx):
if len(ctx.attr.srcs) > 1: if len(ctx.attr.srcs) > 1:
fail("Only one src value supported", "srcs") fail("Only one src value supported", "srcs")
for s in ctx.attr.srcs: for s in ctx.attr.srcs:
if s.label.package != ctx.label.package: if s.label.package != ctx.label.package:
print(("in srcs attribute of {0}: Proto source with label {1} should be in " print(("in srcs attribute of {0}: Proto source with label {1} should be in " +
+ "same package as consuming rule").format(ctx.label, s.label)) "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] # Use .jar since .srcjar makes protoc think output will be a directory
includes = [f for dep in ctx.attr.srcs for f in dep.proto.transitive_imports] srcdotjar = ctx.new_file(ctx.label.name + "_src.jar")
flavor = ctx.attr.flavor srcs = [f for dep in ctx.attr.srcs for f in dep.proto.direct_sources]
if flavor == "normal": includes = [f for dep in ctx.attr.srcs for f in dep.proto.transitive_imports]
flavor = ""
ctx.action( flavor = ctx.attr.flavor
inputs = [ctx.executable._java_plugin] + srcs + includes, if flavor == "normal":
outputs = [srcdotjar], flavor = ""
executable = ctx.executable._protoc, ctx.action(
arguments = [ inputs = [ctx.executable._java_plugin] + srcs + includes,
"--plugin=protoc-gen-grpc-java=" + ctx.executable._java_plugin.path, outputs = [srcdotjar],
"--grpc-java_out={0},enable_deprecated={1}:{2}" executable = ctx.executable._protoc,
.format(flavor, str(ctx.attr.enable_deprecated).lower(), srcdotjar.path)] arguments = [
+ ["-I{0}={1}".format(_path_ignoring_repository(include), include.path) for include in includes] "--plugin=protoc-gen-grpc-java=" + ctx.executable._java_plugin.path,
+ [_path_ignoring_repository(src) for src in srcs]) "--grpc-java_out={0},enable_deprecated={1}:{2}"
ctx.action( .format(flavor, str(ctx.attr.enable_deprecated).lower(), srcdotjar.path),
command = "cp $1 $2", ] +
inputs = [srcdotjar], ["-I{0}={1}".format(_path_ignoring_repository(include), include.path) for include in includes] +
outputs = [ctx.outputs.srcjar], [_path_ignoring_repository(src) for src in srcs],
arguments = [srcdotjar.path, ctx.outputs.srcjar.path]) )
ctx.action(
command = "cp $1 $2",
inputs = [srcdotjar],
outputs = [ctx.outputs.srcjar],
arguments = [srcdotjar.path, ctx.outputs.srcjar.path],
)
_gensource = rule( _gensource = rule(
attrs = { attrs = {
@ -69,65 +73,70 @@ _gensource = rule(
implementation = _gensource_impl, implementation = _gensource_impl,
) )
def java_grpc_library(name, srcs, deps, flavor=None, def java_grpc_library(
enable_deprecated=None, visibility=None, name,
**kwargs): srcs,
"""Generates and compiles gRPC Java sources for services defined in a proto deps,
file. This rule is compatible with java_proto_library and java_lite_proto_library. 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 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 does not generate Java classes for proto messages. You will need a separate
java_proto_library or java_lite_proto_library rule. java_proto_library or java_lite_proto_library rule.
Args: Args:
name: (str) A unique name for this rule. Required. name: (str) A unique name for this rule. Required.
srcs: (list) a single proto_library target that contains the schema of the srcs: (list) a single proto_library target that contains the schema of the
service. Required. service. Required.
deps: (list) a single java_proto_library target for the proto_library in deps: (list) a single java_proto_library target for the proto_library in
srcs. Required. srcs. Required.
flavor: (str) "normal" (default) for normal proto runtime. "lite" flavor: (str) "normal" (default) for normal proto runtime. "lite"
for the lite runtime. for the lite runtime.
visibility: (list) the visibility list visibility: (list) the visibility list
**kwargs: Passed through to generated targets **kwargs: Passed through to generated targets
""" """
if flavor == None: if flavor == None:
flavor = "normal" flavor = "normal"
if len(deps) > 1: if len(deps) > 1:
print("Multiple values in 'deps' is deprecated in " + name) print("Multiple values in 'deps' is deprecated in " + name)
gensource_name = name + "__do_not_reference__srcjar" gensource_name = name + "__do_not_reference__srcjar"
_gensource( _gensource(
name = gensource_name, name = gensource_name,
srcs = srcs, srcs = srcs,
flavor = flavor, flavor = flavor,
enable_deprecated = enable_deprecated, enable_deprecated = enable_deprecated,
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
**kwargs **kwargs
) )
added_deps = [ added_deps = [
"@io_grpc_grpc_java//core", "@io_grpc_grpc_java//core",
"@io_grpc_grpc_java//stub", "@io_grpc_grpc_java//stub",
"@io_grpc_grpc_java//stub:javax_annotation", "@io_grpc_grpc_java//stub:javax_annotation",
"@com_google_guava_guava//jar", "@com_google_guava_guava//jar",
]
if flavor == "normal":
added_deps += [
"@com_google_protobuf//:protobuf_java",
"@io_grpc_grpc_java//protobuf",
] ]
elif flavor == "lite": if flavor == "normal":
added_deps += ["@io_grpc_grpc_java//protobuf-lite"] added_deps += [
else: "@com_google_protobuf//:protobuf_java",
fail("Unknown flavor type", "flavor") "@io_grpc_grpc_java//protobuf",
]
elif flavor == "lite":
added_deps += ["@io_grpc_grpc_java//protobuf-lite"]
else:
fail("Unknown flavor type", "flavor")
native.java_library( native.java_library(
name = name, name = name,
srcs = [gensource_name], srcs = [gensource_name],
visibility = visibility, visibility = visibility,
deps = [ deps = [
"@com_google_code_findbugs_jsr305//jar", "@com_google_code_findbugs_jsr305//jar",
] + deps + added_deps, ] + deps + added_deps,
**kwargs **kwargs
) )