Skip to content

Commit 4cbfd02

Browse files
committed
Improve test suite for -Zbuild-std
This commit is aimed directly at rust-lang/wg-cargo-std-aware#33 and in general making the `-Zbuild-std` tests more robust. The main change here is that a new source tree is checked in, `tests/testsuite/mock-std`, which mirrors rust-lang/rust's own tree for libstd. This mock tree is as empty as it can be, ideally duplicating almost nothing but for not requiring duplication of Cargo metadata about patches and such. The end result here looks like: * All `-Zbuild-std` tests are now run in parallel * All tests run much more quickly since they're compiling tiny crates instead of actually compiling libstd/libcore * No tests require network access * We verify that crates have access to the "custom" libraries that we build Coverage of tests is not currently expanded, but it's hoped that we could add that shortly afterwards. Coverage has actually gone down slightly since the custom target test was commented out temporarily and the full integration test of running `-Zbuild-std` isn't run on CI any more. Closes rust-lang/wg-cargo-std-aware#33
1 parent b2d4f20 commit 4cbfd02

File tree

24 files changed

+569
-182
lines changed

24 files changed

+569
-182
lines changed

src/cargo/core/compiler/standard_lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspac
77
use crate::ops::{self, Packages};
88
use crate::util::errors::CargoResult;
99
use std::collections::{HashMap, HashSet};
10+
use std::env;
1011
use std::path::PathBuf;
1112

1213
/// Parse the `-Zbuild-std` flag.
@@ -148,6 +149,10 @@ pub fn generate_std_roots<'a>(
148149
}
149150

150151
fn detect_sysroot_src_path(ws: &Workspace<'_>) -> CargoResult<PathBuf> {
152+
if let Some(s) = env::var_os("__CARGO_TESTS_ONLY_SRC_ROOT") {
153+
return Ok(s.into());
154+
}
155+
151156
// NOTE: This is temporary until we figure out how to acquire the source.
152157
// If we decide to keep the sysroot probe, then BuildConfig will need to
153158
// be restructured so that the TargetInfo is created earlier and passed

tests/testsuite/mock-std/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[workspace]
2+
members = [
3+
"src/libtest",
4+
]
5+
6+
[patch.crates-io]
7+
rustc-std-workspace-std = { path = 'src/tools/rustc-std-workspace-std' }
8+
rustc-std-workspace-core = { path = 'src/tools/rustc-std-workspace-core' }
9+
rustc-std-workspace-alloc = { path = 'src/tools/rustc-std-workspace-alloc' }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "alloc"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
path = "lib.rs"
9+
10+
[dependencies]
11+
core = { path = "../libcore" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn custom_api() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "compiler_builtins"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
path = "lib.rs"
9+
10+
[dependencies]
11+
core = { path = "../libcore" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// intentionally blank
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "core"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
path = "lib.rs"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! This build script is basically the whole hack that makes this entire "mock
2+
//! std" feature work. Here we print out `rustc-link-search` pointing to the
3+
//! sysroot of the actual compiler itself, and that way we can indeed implicitly
4+
//! pull in those crates, but only via `extern crate`. That means that we can
5+
//! build tiny shim core/std/etc crates while they actually load all the various
6+
//! language/library details from the actual crates, meaning that instead of
7+
//! literally compiling libstd we compile just our own tiny shims.
8+
9+
use std::process::Command;
10+
use std::env;
11+
12+
fn main() {
13+
let output = Command::new("rustc")
14+
.arg("--print")
15+
.arg("sysroot")
16+
.output()
17+
.unwrap();
18+
assert!(output.status.success());
19+
let stdout = String::from_utf8(output.stdout).unwrap();
20+
let stdout = stdout.trim();
21+
let host = env::var("HOST").unwrap();
22+
println!("cargo:rustc-link-search={}/lib/rustlib/{}/lib", stdout, host);
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![no_std]
2+
pub use core::*;
3+
4+
pub fn custom_api() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "panic_unwind"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
path = "lib.rs"
9+
10+
[dependencies]
11+
core = { path = "../libcore" }

0 commit comments

Comments
 (0)