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

Regenerated proto raze outputs + addressed buildifier defects #435

Merged
merged 3 commits into from
Oct 15, 2020
Merged
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
2 changes: 1 addition & 1 deletion examples/proto/basic/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate common_proto_rust;

pub fn do_something(x: &common_proto_rust::Config) -> bool {
pub fn do_something(_x: &common_proto_rust::Config) -> bool {
true
}
111 changes: 92 additions & 19 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ load("@io_bazel_rules_rust//rust:private/utils.bzl", "determine_output_hash", "f
load("@rules_proto//proto:defs.bzl", "ProtoInfo")

RustProtoInfo = provider(
doc = "Rust protobuf provider info",
fields = {
"proto_sources": "List[string]: list of source paths of protos",
"transitive_proto_sources": "depset[string]",
},
)

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

Args:
file (File): The target source file.
source_root_attr (str): The directory relative to which the `.proto` \
files defined in the proto_library are defined.

Returns:
str: The protoc suitible path of `file`
"""

# Bazel creates symlinks to the .proto files under a directory called
# "_virtual_imports/<rule name>" if we do any sort of munging of import
Expand Down Expand Up @@ -82,6 +92,15 @@ def _compute_proto_source_path(file, source_root_attr):
return path

def _rust_proto_aspect_impl(target, ctx):
"""The implementation of the `rust_proto_aspect` aspect

Args:
target (Target): The target to which the aspect is applied
ctx (ctx): The rule context which the targetis created from

Returns:
list: A list containg a `RustProtoInfo` provider
"""
if ProtoInfo not in target:
return None

Expand All @@ -102,18 +121,26 @@ def _rust_proto_aspect_impl(target, ctx):
for f in ctx.rule.attr.deps
if RustProtoInfo in f
]
return RustProtoInfo(
return [RustProtoInfo(
proto_sources = sources,
transitive_proto_sources = depset(transitive = transitive_sources, direct = sources),
)
)]

_rust_proto_aspect = aspect(
_rust_proto_aspect_impl,
doc = "An aspect that gathers rust proto direct and transitive sources",
implementation = _rust_proto_aspect_impl,
attr_aspects = ["deps"],
)

def _gen_lib(ctx, grpc, srcs, lib):
"""Generate a lib.rs file for the crates."""
"""Generate a lib.rs file for the crates.

Args:
ctx (ctx): The current rule's context object
grpc (bool): True if the current rule is a `gRPC` rule.
srcs (list): A list of protoc suitible file paths (str).
lib (File): The File object where the rust source file should be written
"""
content = ["extern crate protobuf;"]
if grpc:
content.append("extern crate grpc;")
Expand All @@ -127,9 +154,33 @@ def _gen_lib(ctx, grpc, srcs, lib):
ctx.actions.write(lib, "\n".join(content))

def _expand_provider(lst, provider):
"""Gathers a list of a specific provider from a list of targets.

Args:
lst (list): A list of Targets
provider (Provider): The target provider type to extract `lst`

Returns:
list: A list of providers of the type from `provider`.
"""
return [el[provider] for el in lst if provider in el]

def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc, compile_deps):
"""Create and run a rustc compile action based on the current rule's attributes

Args:
protos (depset): Paths of protos to compile.
descriptor_sets (depset): A set of transitive protobuf `FileDescriptorSet`s
imports (depset): A set of transitive protobuf Imports.
crate_name (str): The name of the Crate for the current target
ctx (ctx): The current rule's context object
grpc (bool): True if the current rule is a `gRPC` rule.
compile_deps (list): A list of Rust dependencies (`List[Target]`)

Returns:
list: A list of providers, see `rustc_compile_action`
"""

# Create all the source in a specific folder
proto_toolchain = ctx.toolchains["@io_bazel_rules_rust//proto:toolchain"]
output_dir = "%s.%s.rust" % (crate_name, "grpc" if grpc else "proto")
Expand Down Expand Up @@ -158,7 +209,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc,
output_hash,
))

result = rustc_compile_action(
return rustc_compile_action(
ctx = ctx,
toolchain = find_toolchain(ctx),
crate_info = CrateInfo(
Expand All @@ -176,33 +227,44 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc,
),
output_hash = output_hash,
)
return result

def _rust_protogrpc_library_impl(ctx, grpc):
"""Implementation of the rust_(proto|grpc)_library."""
"""Implementation of the rust_(proto|grpc)_library.

Args:
ctx (ctx): The current rule's context object
grpc (bool): True if the current rule is a `gRPC` rule.

Returns:
list: A list of providers, see `_rust_proto_compile`
"""
proto = _expand_provider(ctx.attr.deps, ProtoInfo)
transitive_sources = [
f[RustProtoInfo].transitive_proto_sources
for f in ctx.attr.deps
if RustProtoInfo in f
]

srcs = depset(transitive = transitive_sources)
return _rust_proto_compile(
srcs,
depset(transitive = [p.transitive_descriptor_sets for p in proto]),
depset(transitive = [p.transitive_imports for p in proto]),
ctx.label.name,
ctx,
grpc,
ctx.attr.rust_deps,
protos = depset(transitive = transitive_sources),
descriptor_sets = depset(transitive = [p.transitive_descriptor_sets for p in proto]),
imports = depset(transitive = [p.transitive_imports for p in proto]),
crate_name = ctx.label.name,
ctx = ctx,
grpc = grpc,
compile_deps = ctx.attr.rust_deps,
)

def _rust_proto_library_impl(ctx):
return _rust_protogrpc_library_impl(ctx, False)
"""The implementation of the `rust_proto_library` rule

def _rust_grpc_library_impl(ctx):
return _rust_protogrpc_library_impl(ctx, True)
Args:
ctx (ctx): The rule's context object.

Returns:
list: A list of providers, see `_rust_protogrpc_library_impl`
"""
return _rust_protogrpc_library_impl(ctx, False)

rust_proto_library = rule(
implementation = _rust_proto_library_impl,
Expand Down Expand Up @@ -272,6 +334,17 @@ rust_binary(
""",
)

def _rust_grpc_library_impl(ctx):
"""The implementation of the `rust_grpc_library` rule

Args:
ctx (ctx): The rule's context object

Returns:
list: A list of providers. See `_rust_protogrpc_library_impl`
"""
return _rust_protogrpc_library_impl(ctx, True)

rust_grpc_library = rule(
implementation = _rust_grpc_library_impl,
attrs = {
Expand Down
56 changes: 47 additions & 9 deletions proto/raze/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""
@generated
cargo-raze workspace build file.

DO NOT EDIT! Replaced on runs of cargo-raze
Expand All @@ -10,51 +11,88 @@ licenses([
"notice", # See individual crates for specific licenses
])

# Aliased targets
alias(
name = "grpc",
actual = "@raze__grpc__0_6_2//:grpc",
actual = "@rules_rust_proto__grpc__0_6_2//:grpc",
tags = [
"cargo-raze",
"manual",
],
)

alias(
name = "grpc_compiler",
actual = "@raze__grpc_compiler__0_6_2//:grpc_compiler",
actual = "@rules_rust_proto__grpc_compiler__0_6_2//:grpc_compiler",
tags = [
"cargo-raze",
"manual",
],
)

alias(
# Extra aliased target, from raze configuration
# N.B.: The exact form of this is subject to change.
name = "cargo_bin_protoc_gen_rust_grpc",
actual = "@raze__grpc_compiler__0_6_2//:cargo_bin_protoc_gen_rust_grpc",
actual = "@rules_rust_proto__grpc_compiler__0_6_2//:cargo_bin_protoc_gen_rust_grpc",
tags = [
"cargo-raze",
"manual",
],
)

alias(
name = "log",
actual = "@raze__log__0_4_6//:log",
actual = "@rules_rust_proto__log__0_4_6//:log",
tags = [
"cargo-raze",
"manual",
],
)

alias(
name = "protobuf",
actual = "@raze__protobuf__2_8_2//:protobuf",
actual = "@rules_rust_proto__protobuf__2_8_2//:protobuf",
tags = [
"cargo-raze",
"manual",
],
)

alias(
name = "protobuf_codegen",
actual = "@raze__protobuf_codegen__2_8_2//:protobuf_codegen",
actual = "@rules_rust_proto__protobuf_codegen__2_8_2//:protobuf_codegen",
tags = [
"cargo-raze",
"manual",
],
)

alias(
# Extra aliased target, from raze configuration
# N.B.: The exact form of this is subject to change.
name = "cargo_bin_protoc_gen_rust",
actual = "@raze__protobuf_codegen__2_8_2//:cargo_bin_protoc_gen_rust",
actual = "@rules_rust_proto__protobuf_codegen__2_8_2//:cargo_bin_protoc_gen_rust",
tags = [
"cargo-raze",
"manual",
],
)

alias(
name = "tls_api",
actual = "@raze__tls_api__0_1_22//:tls_api",
actual = "@rules_rust_proto__tls_api__0_1_22//:tls_api",
tags = [
"cargo-raze",
"manual",
],
)

alias(
name = "tls_api_stub",
actual = "@raze__tls_api_stub__0_1_22//:tls_api_stub",
actual = "@rules_rust_proto__tls_api_stub__0_1_22//:tls_api_stub",
tags = [
"cargo-raze",
"manual",
],
)
6 changes: 4 additions & 2 deletions proto/raze/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

[package]
name = "fake_lib"
name = "fake_rules_rust_proto"
version = "0.0.1"

[lib]
path = "lib.rs"
path = "fake_rules_rust_proto.rs"

[dependencies]
# Newer version of protobuf have been released, but the 0.6.2 release of
Expand All @@ -26,6 +26,8 @@ log = "0.4, <0.4.7"
[raze]
genmode = "Remote"
workspace_path = "//proto/raze"
gen_workspace_prefix = "rules_rust_proto"
output_buildfile_suffix = "BUILD"

[raze.crates.lazy_static.'1.4.0']
additional_flags = [
Expand Down
Loading