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

Perform - to _ replacement in test output names #528

Merged
merged 1 commit into from
Dec 8, 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
3 changes: 2 additions & 1 deletion cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# buildifier: disable=module-docstring
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("@io_bazel_rules_rust//rust:private/rust.bzl", "name_to_crate_name")
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "BuildInfo", "DepInfo", "get_cc_toolchain", "get_compilation_mode_opts", "get_linker_and_args")
load("@io_bazel_rules_rust//rust:private/utils.bzl", "expand_locations", "find_toolchain")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")
Expand Down Expand Up @@ -107,7 +108,7 @@ def _build_script_impl(ctx):
env["AR"] = ar_executable

for f in ctx.attr.crate_features:
env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"
env["CARGO_FEATURE_" + name_to_crate_name(f).upper()] = "1"

env.update(expand_locations(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ rust_library(
)

rust_test(
name = "hello_lib_test",
name = "hello-lib-test",
crate = ":hello_lib",
)

Expand Down
4 changes: 3 additions & 1 deletion proto/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

"""Toolchain for compiling rust stubs from protobuf and gRPC."""

load("@io_bazel_rules_rust//rust:private/rust.bzl", "name_to_crate_name")

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

def rust_generate_proto(
Expand Down
26 changes: 22 additions & 4 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _rust_library_impl(ctx):
# Determine unique hash for this rlib
output_hash = determine_output_hash(crate_root)

crate_name = ctx.label.name.replace("-", "_")
crate_name = name_to_crate_name(ctx.label.name)
rust_lib_name = _determine_lib_name(
crate_name,
ctx.attr.crate_type,
Expand Down Expand Up @@ -191,7 +191,7 @@ def _rust_binary_impl(ctx):
list: A list of providers. See `rustc_compile_action`
"""
toolchain = find_toolchain(ctx)
crate_name = ctx.label.name.replace("-", "_")
crate_name = name_to_crate_name(ctx.label.name)

output = ctx.actions.declare_file(ctx.label.name + toolchain.binary_ext)

Expand Down Expand Up @@ -228,7 +228,7 @@ def _rust_test_common(ctx, toolchain, output):
"""
_assert_no_deprecated_attributes(ctx)

crate_name = ctx.label.name.replace("-", "_")
crate_name = name_to_crate_name(ctx.label.name)

if ctx.attr.crate:
# Target is building the crate in `test` config
Expand Down Expand Up @@ -282,7 +282,7 @@ def _rust_test_impl(ctx):
toolchain = find_toolchain(ctx)

output = ctx.actions.declare_file(
ctx.label.name + toolchain.binary_ext,
name_to_crate_name(ctx.label.name) + toolchain.binary_ext,
)

return _rust_test_common(ctx, toolchain, output)
Expand Down Expand Up @@ -938,3 +938,21 @@ rust_benchmark(
Run the benchmark test using: `bazel run //fibonacci:fibonacci_bench`.
""",
)

def name_to_crate_name(name):
illicitonion marked this conversation as resolved.
Show resolved Hide resolved
"""Converts a build target's name into the name of its associated crate.

Crate names cannot contain certain characters, such as -, which are allowed
in build target names. All illegal characters will be converted to
underscores.

This is a similar conversion as that which cargo does, taking a
`Cargo.toml`'s `package.name` and canonicalizing it

Args:
name (str): The name of the target.

Returns:
str: The name of the crate for this target.
"""
illicitonion marked this conversation as resolved.
Show resolved Hide resolved
return name.replace("-", "_")