Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1bc1993
make cargo test work for bootstrap self test
Shourya742 Sep 15, 2025
a12969e
walk up the ancestors
Shourya742 Sep 15, 2025
6c79f54
add an API in ConfigBuilder to point to config file for toml parsing
Shourya742 Sep 16, 2025
d488d33
let verify method run in test settings
Shourya742 Sep 16, 2025
ce4604e
remove using default toml config for test in get_toml, we handle it v…
Shourya742 Sep 16, 2025
24ed1a0
allow symlinking during test
Shourya742 Sep 16, 2025
05131bd
move most of the test to new testCtx
Shourya742 Sep 16, 2025
a29474d
this is dicy, whether we have a method to explicitly enable_llvm_over…
Shourya742 Sep 16, 2025
0c68c82
rename config_toml to with_default_toml_config
Shourya742 Sep 21, 2025
9189bf7
remove create_config_without_ci_llvm_override duplication
Shourya742 Sep 21, 2025
671aabd
add dry_run flag in config builder and remove runtime test hacks
Shourya742 Sep 21, 2025
afe380d
initialize out with CARGO_TARGET_DIR and then go for manifest and the…
Shourya742 Sep 21, 2025
a48cd76
add explicit config assignment when running the test, as the src is a…
Shourya742 Sep 21, 2025
33e262e
remove prepare_test_specific_dir and update tests accordingly
Shourya742 Sep 21, 2025
6adbb3a
remove explicit target assignment in config during rustc initialization
Shourya742 Sep 22, 2025
8a0e380
add check for toml file
Shourya742 Sep 22, 2025
83b784f
add comment explaining the test_build_dir
Shourya742 Sep 22, 2025
aaa82ae
move config check logic from get_toml to parse_inner
Shourya742 Sep 22, 2025
079addf
Ensure that `--build-dir` is always specified in tests
Kobzol Sep 25, 2025
191c7ed
Make `cargo test` work again
Kobzol Sep 25, 2025
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
49 changes: 18 additions & 31 deletions src/bootstrap/src/core/builder/tests.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@Kobzol Kobzol Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the documentation needs a lot of updates. Shourya is actually gonna work on that next :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am currently working on this, separate from this PR, I hope to open a PR by next week.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config {
}

fn configure_with_args(cmd: &[&str], host: &[&str], target: &[&str]) -> Config {
TestCtx::new()
.config(cmd[0])
.args(&cmd[1..])
.hosts(host)
.targets(target)
.args(&["--build", TEST_TRIPLE_1])
.create_config()
TestCtx::new().config(cmd[0]).args(&cmd[1..]).hosts(host).targets(target).create_config()
}

fn first<A, B>(v: Vec<(A, B)>) -> Vec<A> {
Expand Down Expand Up @@ -218,25 +212,25 @@ fn prepare_rustc_checkout(ctx: &mut GitCtx) {

/// Parses a Config directory from `path`, with the given value of `download_rustc`.
fn parse_config_download_rustc_at(path: &Path, download_rustc: &str, ci: bool) -> Config {
Config::parse_inner(
Flags::parse(&[
"build".to_owned(),
"--dry-run".to_owned(),
"--ci".to_owned(),
if ci { "true" } else { "false" }.to_owned(),
format!("--set=rust.download-rustc='{download_rustc}'"),
"--src".to_owned(),
path.to_str().unwrap().to_owned(),
]),
|&_| Ok(Default::default()),
)
TestCtx::new()
.config("build")
.args(&[
"--ci",
if ci { "true" } else { "false" },
format!("--set=rust.download-rustc='{download_rustc}'").as_str(),
"--src",
path.to_str().unwrap(),
])
.no_override_download_ci_llvm()
.create_config()
}

mod dist {
use pretty_assertions::assert_eq;

use super::{Config, TEST_TRIPLE_1, TEST_TRIPLE_2, TEST_TRIPLE_3, first, run_build};
use crate::Flags;
use crate::core::builder::tests::host_target;
use crate::core::builder::*;

fn configure(host: &[&str], target: &[&str]) -> Config {
Expand All @@ -245,11 +239,11 @@ mod dist {

#[test]
fn llvm_out_behaviour() {
let mut config = configure(&[TEST_TRIPLE_1], &[TEST_TRIPLE_2]);
let mut config = configure(&[], &[TEST_TRIPLE_2]);
config.llvm_from_ci = true;
let build = Build::new(config.clone());

let target = TargetSelection::from_user(TEST_TRIPLE_1);
let target = TargetSelection::from_user(&host_target());
assert!(build.llvm_out(target).ends_with("ci-llvm"));
let target = TargetSelection::from_user(TEST_TRIPLE_2);
assert!(build.llvm_out(target).ends_with("llvm"));
Expand Down Expand Up @@ -314,7 +308,7 @@ mod sysroot_target_dirs {
/// cg_gcc tests instead.
#[test]
fn test_test_compiler() {
let config = configure_with_args(&["test", "compiler"], &[TEST_TRIPLE_1], &[TEST_TRIPLE_1]);
let config = configure_with_args(&["test", "compiler"], &[&host_target()], &[TEST_TRIPLE_1]);
let cache = run_build(&config.paths.clone(), config);

let compiler = cache.contains::<test::CrateLibrustc>();
Expand Down Expand Up @@ -347,7 +341,7 @@ fn test_test_coverage() {
// Print each test case so that if one fails, the most recently printed
// case is the one that failed.
println!("Testing case: {cmd:?}");
let config = configure_with_args(cmd, &[TEST_TRIPLE_1], &[TEST_TRIPLE_1]);
let config = configure_with_args(cmd, &[], &[TEST_TRIPLE_1]);
let mut cache = run_build(&config.paths.clone(), config);

let modes =
Expand All @@ -359,14 +353,7 @@ fn test_test_coverage() {
#[test]
fn test_prebuilt_llvm_config_path_resolution() {
fn configure(config: &str) -> Config {
Config::parse_inner(
Flags::parse(&[
"build".to_string(),
"--dry-run".to_string(),
"--config=/does/not/exist".to_string(),
]),
|&_| toml::from_str(&config),
)
TestCtx::new().config("build").with_default_toml_config(config).create_config()
}

// Removes Windows disk prefix if present
Expand Down
66 changes: 47 additions & 19 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,28 @@ impl Config {
// Set config values based on flags.
let mut exec_ctx = ExecutionContext::new(flags_verbose, flags_cmd.fail_fast());
exec_ctx.set_dry_run(if flags_dry_run { DryRun::UserSelected } else { DryRun::Disabled });
let mut src = {

let default_src_dir = {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
// Undo `src/bootstrap`
manifest_dir.parent().unwrap().parent().unwrap().to_owned()
};
let src = if let Some(s) = compute_src_directory(flags_src, &exec_ctx) {
s
} else {
default_src_dir.clone()
};

if let Some(src_) = compute_src_directory(flags_src, &exec_ctx) {
src = src_;
#[cfg(test)]
{
if let Some(config_path) = flags_config.as_ref() {
assert!(
!config_path.starts_with(&src),
"Path {config_path:?} should not be inside or equal to src dir {src:?}"
);
} else {
panic!("During test the config should be explicitly added");
}
}

// Now load the TOML config, as soon as possible
Expand Down Expand Up @@ -630,19 +644,13 @@ impl Config {
let llvm_assertions = llvm_assertions.unwrap_or(false);
let mut target_config = HashMap::new();
let mut channel = "dev".to_string();
let out = flags_build_dir.or(build_build_dir.map(PathBuf::from)).unwrap_or_else(|| {
if cfg!(test) {
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
Path::new(
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
)
.parent()
.unwrap()
.to_path_buf()
} else {
PathBuf::from("build")
}
});

let out = flags_build_dir.or_else(|| build_build_dir.map(PathBuf::from));
let out = if cfg!(test) {
out.expect("--build-dir has to be specified in tests")
} else {
out.unwrap_or_else(|| PathBuf::from("build"))
};

// NOTE: Bootstrap spawns various commands with different working directories.
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
Expand All @@ -653,6 +661,10 @@ impl Config {
out
};

let default_stage0_rustc_path = |dir: &Path| {
dir.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
};

if cfg!(test) {
// When configuring bootstrap for tests, make sure to set the rustc and Cargo to the
// same ones used to call the tests (if custom ones are not defined in the toml). If we
Expand All @@ -661,6 +673,22 @@ impl Config {
// Cargo in their bootstrap.toml.
build_rustc = build_rustc.take().or(std::env::var_os("RUSTC").map(|p| p.into()));
build_cargo = build_cargo.take().or(std::env::var_os("CARGO").map(|p| p.into()));

// If we are running only `cargo test` (and not `x test bootstrap`), which is useful
// e.g. for debugging bootstrap itself, then we won't have RUSTC and CARGO set to the
// proper paths.
// We thus "guess" that the build directory is located at <src>/build, and try to load
// rustc and cargo from there
let is_test_outside_x = std::env::var("CARGO_TARGET_DIR").is_err();
if is_test_outside_x && build_rustc.is_none() {
let stage0_rustc = default_stage0_rustc_path(&default_src_dir.join("build"));
assert!(
stage0_rustc.exists(),
"Trying to run cargo test without having a stage0 rustc available in {}",
stage0_rustc.display()
);
build_rustc = Some(stage0_rustc);
}
}

if !flags_skip_stage0_validation {
Expand Down Expand Up @@ -694,7 +722,7 @@ impl Config {

let initial_rustc = build_rustc.unwrap_or_else(|| {
download_beta_toolchain(&dwn_ctx, &out);
out.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
default_stage0_rustc_path(&out)
});

let initial_sysroot = t!(PathBuf::from_str(
Expand Down Expand Up @@ -1534,11 +1562,11 @@ impl Config {
println!("WARNING: CI rustc has some fields that are no longer supported in bootstrap; download-rustc will be disabled.");
println!("HELP: Consider rebasing to a newer commit if available.");
return None;
},
}
Err(e) => {
eprintln!("ERROR: Failed to parse CI rustc bootstrap.toml: {e}");
exit!(2);
},
}
};

let current_config_toml = Self::get_toml(config_path).unwrap();
Expand Down
Loading
Loading