-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Obtain the "CARGO_CRATE_NAME" value from the CrateInfo (#999)
At this point we have a `CrateInfo` for the target which contains the name of the crate, regardless whether it's been derived from the `ctx.attr.crate_name` or explicitly assigned. The code without this PR works well for the `rules_rust`, because the `CrateInfo.crate_name` is obtained by calling the `get_crate_name_from_attr` function. However, this is not sufficient for aspects, as they may be attached to a target that doesn't specify crate name, or one may want to assign a modified name to crates created by aspects.
- Loading branch information
Showing
6 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
load(":consistent_crate_name_test.bzl", "consistent_crate_name_test_suite") | ||
|
||
############################ UNIT TESTS ############################# | ||
consistent_crate_name_test_suite(name = "consistent_crate_name_test_suite") |
46 changes: 46 additions & 0 deletions
46
test/unit/consistent_crate_name/consistent_crate_name_test.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Unittest to verify that we can treat all dependencies as direct dependencies""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "analysistest") | ||
load("//test/unit:common.bzl", "assert_action_mnemonic", "assert_env_value") | ||
load("//test/unit/consistent_crate_name:with_modified_crate_name.bzl", "with_modified_crate_name") | ||
|
||
def _consistent_crate_name_env_test(ctx): | ||
env = analysistest.begin(ctx) | ||
tut = analysistest.target_under_test(env) | ||
action = tut.actions[0] | ||
assert_action_mnemonic(env, action, "Rustc") | ||
assert_env_value( | ||
env, | ||
action, | ||
"CARGO_CRATE_NAME", | ||
"lib_my_custom_crate_suffix", | ||
) | ||
return analysistest.end(env) | ||
|
||
consistent_crate_name_env_test = analysistest.make(_consistent_crate_name_env_test) | ||
|
||
def _consistent_crate_name_test(): | ||
with_modified_crate_name( | ||
name = "lib", | ||
src = "lib.rs", | ||
) | ||
|
||
consistent_crate_name_env_test( | ||
name = "consistent_crate_name_env_test", | ||
target_under_test = ":lib", | ||
) | ||
|
||
def consistent_crate_name_test_suite(name): | ||
"""Entry-point macro called from the BUILD file. | ||
Args: | ||
name: Name of the macro. | ||
""" | ||
_consistent_crate_name_test() | ||
|
||
native.test_suite( | ||
name = name, | ||
tests = [ | ||
":consistent_crate_name_env_test", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
75 changes: 75 additions & 0 deletions
75
test/unit/consistent_crate_name/with_modified_crate_name.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""A custom rule that threats all its dependencies as direct dependencies.""" | ||
|
||
load("//rust:defs.bzl", "rust_common") | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVariantInfo") | ||
|
||
# buildifier: disable=bzl-visibility | ||
load("//rust/private:rustc.bzl", "rustc_compile_action") | ||
|
||
def _with_modified_crate_name_impl(ctx): | ||
toolchain = ctx.toolchains[Label("//rust:toolchain")] | ||
|
||
crate_root = ctx.attr.src.files.to_list()[0] | ||
output_hash = repr(hash(crate_root.path)) | ||
crate_name = ctx.label.name + "_my_custom_crate_suffix" | ||
crate_type = "rlib" | ||
|
||
rust_lib_name = "{prefix}{name}-{lib_hash}{extension}".format( | ||
prefix = "lib", | ||
name = crate_name, | ||
lib_hash = output_hash, | ||
extension = ".rlib", | ||
) | ||
|
||
deps = [DepVariantInfo( | ||
crate_info = dep[CrateInfo] if CrateInfo in dep else None, | ||
dep_info = dep[DepInfo] if DepInfo in dep else None, | ||
build_info = dep[BuildInfo] if BuildInfo in dep else None, | ||
cc_info = dep[CcInfo] if CcInfo in dep else None, | ||
) for dep in ctx.attr.deps] | ||
|
||
rust_lib = ctx.actions.declare_file(rust_lib_name) | ||
return rustc_compile_action( | ||
ctx = ctx, | ||
attr = ctx.attr, | ||
toolchain = toolchain, | ||
crate_info = rust_common.create_crate_info( | ||
name = crate_name, | ||
type = crate_type, | ||
root = crate_root, | ||
srcs = ctx.attr.src.files, | ||
deps = depset(deps), | ||
proc_macro_deps = depset([]), | ||
aliases = {}, | ||
output = rust_lib, | ||
owner = ctx.label, | ||
edition = "2018", | ||
compile_data = depset([]), | ||
rustc_env = {}, | ||
is_test = False, | ||
), | ||
output_hash = output_hash, | ||
) | ||
|
||
with_modified_crate_name = rule( | ||
implementation = _with_modified_crate_name_impl, | ||
attrs = { | ||
"deps": attr.label_list(), | ||
"src": attr.label(allow_single_file = [".rs"]), | ||
"_cc_toolchain": attr.label( | ||
default = "@bazel_tools//tools/cpp:current_cc_toolchain", | ||
), | ||
"_error_format": attr.label(default = "@rules_rust//:error_format"), | ||
"_process_wrapper": attr.label( | ||
default = Label("@rules_rust//util/process_wrapper"), | ||
executable = True, | ||
allow_single_file = True, | ||
cfg = "exec", | ||
), | ||
}, | ||
toolchains = ["@rules_rust//rust:toolchain", "@bazel_tools//tools/cpp:toolchain_type"], | ||
incompatible_use_toolchain_transition = True, | ||
fragments = ["cpp"], | ||
) |