Skip to content

Commit

Permalink
Auto merge of #7360 - phil-opp:zbuild-std-custom-test-frameworks, r=a…
Browse files Browse the repository at this point in the history
…lexcrichton

[-Zbuild-std] Only build libtest when libstd is built

Currently `libtest` is always compiled when a compilation unit uses a test harness. This implicitly adds builds the standard library too because `libtest` depends on it. This breaks the use of custom test frameworks in `no_std` crates as reported in #7216 (comment).

This pull request fixes the issue by only building `libtest` if `libstd` is built. This makes sense in my opinion because when the user explicitly specified `-Zbuild-std=core`, they probably don't want to build the full standard library and rather get a compilation error when they accidentally use `libtest`.
  • Loading branch information
bors committed Sep 16, 2019
2 parents be01054 + 6b4fd44 commit 7ac51b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ pub fn compile_ws<'a>(
.iter()
.any(|unit| unit.mode.is_rustc_test() && unit.target.harness())
{
crates.push("test".to_string());
// Only build libtest when libstd is built (libtest depends on libstd)
if crates.iter().any(|c| c == "std") {
crates.push("test".to_string());
}
}
standard_lib::generate_std_roots(&bcx, &crates, std_resolve.as_ref().unwrap())?
} else {
Expand Down
42 changes: 42 additions & 0 deletions tests/testsuite/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn std_lib() {
hashbrown();
libc();
test();
custom_test_framework();
target_proc_macro();
bench();
doc();
Expand Down Expand Up @@ -186,6 +187,47 @@ fn test() {
.run();
}

fn custom_test_framework() {
let p = project()
.file(
"src/lib.rs",
r#"
#![no_std]
#![cfg_attr(test, no_main)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
pub fn test_runner(_tests: &[&dyn Fn()]) {}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
"#,
)
.file(
"target.json",
r#"
{
"llvm-target": "x86_64-unknown-none-gnu",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"arch": "x86_64",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"executables": true,
"panic-strategy": "abort"
}
"#,
)
.build();

cargo_build_std(&p, "test --target target.json --no-run -v", "core").run();
}

fn target_proc_macro() {
let p = project()
.file(
Expand Down

0 comments on commit 7ac51b7

Please sign in to comment.