Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate rules_rust for https://github.com/bazelbuild/bazel/issues/7153 and https://github.com/bazelbuild/bazel/issues/7152 #235

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 5 additions & 38 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,16 @@ load("//rust:private/utils.bzl", "find_toolchain")

RustProtoProvider = provider(
fields = {
"proto_sources": "List[string]: list of source paths of protos",
"transitive_proto_sources": "depset[string]",
"proto_sources": "List[File]: list of source paths of protos",
"transitive_proto_sources": "depset[File]",
},
)

def _compute_proto_source_path(file, source_root_attr):
"""Take the short path of file and make it suitable for protoc."""

# For proto, they need to be requested with their absolute name to be
# compatible with the descriptor_set passed by proto_library.
# I.e. if you compile a protobuf at @repo1//package:file.proto, the proto
# compiler would generate a file descriptor with the path
# `package/file.proto`. Since we compile from the proto descriptor, we need
# to pass the list of descriptors and the list of path to compile.
# For the precedent example, the file (noted `f`) would have
# `f.short_path` returns `external/repo1/package/file.proto`.
# In addition, proto_library can provide a proto_source_path to change the base
# path, which should a be a prefix.
path = file.short_path

# Strip external prefix.
path = path.split("/", 2)[2] if path.startswith("../") else path

# Strip source_root.
if path.startswith(source_root_attr):
return path[len(source_root_attr):]
else:
return path

def _rust_proto_aspect_impl(target, ctx):
if ProtoInfo not in target:
return None
source_root = ctx.rule.attr.proto_source_root
if source_root and source_root[-1] != "/":
source_root += "/"

sources = [
_compute_proto_source_path(f, source_root)
for f in target[ProtoInfo].direct_sources
]
sources = target[ProtoInfo].direct_sources
transitive_sources = [
f[RustProtoProvider].transitive_proto_sources
for f in ctx.rule.attr.deps
Expand Down Expand Up @@ -113,9 +83,6 @@ def _gen_lib(ctx, grpc, srcs, lib):
content.append("pub use %s_grpc::*;" % _generated_file_stem(f))
ctx.actions.write(lib, "\n".join(content))

def _expand_provider(lst, provider):
return [getattr(el, provider) for el in lst if hasattr(el, provider)]

def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc, compile_deps):
# Create all the source in a specific folder
proto_toolchain = ctx.toolchains["@io_bazel_rules_rust//proto:toolchain"]
Expand Down Expand Up @@ -162,7 +129,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc,

def _rust_protogrpc_library_impl(ctx, grpc):
"""Implementation of the rust_(proto|grpc)_library."""
proto = _expand_provider(ctx.attr.deps, "proto")
proto = [d[ProtoInfo] for d in ctx.attr.deps if d[ProtoInfo]]
transitive_sources = [
f[RustProtoProvider].transitive_proto_sources
for f in ctx.attr.deps
Expand Down Expand Up @@ -260,7 +227,7 @@ rust_grpc_library = rule(
),
"rust_deps": attr.label_list(
doc = "The crates the generated library depends on.",
default = GRPC_COMPILE_DEPS
default = GRPC_COMPILE_DEPS,
),
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
"_optional_output_wrapper": attr.label(
Expand Down
7 changes: 5 additions & 2 deletions proto/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
"""Toolchain for compiling rust stubs from protobug and gRPC."""

def generated_file_stem(f):
basename = f.rsplit("/", 2)[-1]
basename = f.short_path.rsplit("/", 2)[-1]
basename = basename.replace("-", "_")
return basename.rsplit(".", 2)[0]

def _to_basename(a):
return a.basename

def rust_generate_proto(
ctx,
transitive_descriptor_sets,
Expand Down Expand Up @@ -83,7 +86,7 @@ def rust_generate_proto(
format_joined = "--descriptor_set_in=%s",
)

args.add_all(protos)
args.add_all(imports, map_each = _to_basename)
ctx.actions.run(
inputs = depset(
transitive = [
Expand Down
4 changes: 2 additions & 2 deletions test/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ load("@io_bazel_rules_rust//proto:proto.bzl", "rust_proto_library")
proto_library(
name = "a_proto",
srcs = ["a.proto"],
proto_source_root = "test/proto",
strip_import_prefix = "/test/proto",
)

proto_library(
name = "b_proto",
srcs = ["b.proto"],
deps = [":a_proto"],
proto_source_root = "test/proto",
strip_import_prefix = "/test/proto",
)

rust_proto_library(
Expand Down