Skip to content

Commit eb732d8

Browse files
Add --toolchain option for test command
1 parent 82d7cd4 commit eb732d8

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

build_system/src/test.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ fn show_usage() {
7070
--use-system-gcc : Use system installed libgccjit
7171
--build-only : Only build rustc_codegen_gcc then exits
7272
--nb-parts : Used to split rustc_tests (for CI needs)
73-
--current-part : Used with `--nb-parts`, allows you to specify which parts to test"#
73+
--current-part : Used with `--nb-parts`, allows you to specify which parts to test
74+
--toolchain [arg] : The (rustc) toolchain to be used"#
7475
);
7576
ConfigInfo::show_usage();
7677
for (option, (doc, _)) in get_runners() {
@@ -95,6 +96,7 @@ struct TestArg {
9596
sysroot_panic_abort: bool,
9697
config_info: ConfigInfo,
9798
sysroot_features: Vec<String>,
99+
toolchain_name: Option<String>,
98100
keep_lto_tests: bool,
99101
}
100102

@@ -142,6 +144,18 @@ impl TestArg {
142144
return Err(format!("Expected an argument after `{}`, found nothing", arg));
143145
}
144146
},
147+
"--toolchain" => match args.next() {
148+
Some(toolchain_name) if !toolchain_name.is_empty() => {
149+
if !toolchain_name.starts_with('+') {
150+
test_arg.toolchain_name = Some(format!("+{toolchain_name}"));
151+
} else {
152+
test_arg.toolchain_name = Some(toolchain_name);
153+
}
154+
}
155+
_ => {
156+
return Err(format!("Expected an argument after `{}`, found nothing", arg));
157+
}
158+
},
145159
"--help" => {
146160
show_usage();
147161
return Ok(None);
@@ -181,7 +195,7 @@ impl TestArg {
181195
}
182196

183197
fn build_if_no_backend(env: &Env, args: &TestArg) -> Result<(), String> {
184-
if args.config_info.backend.is_some() {
198+
if args.config_info.backend.is_some() || args.toolchain_name.is_some() {
185199
return Ok(());
186200
}
187201
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &"rustc"];
@@ -459,6 +473,15 @@ fn std_tests(env: &Env, args: &TestArg) -> Result<(), String> {
459473
Ok(())
460474
}
461475

476+
fn get_rustc_path_for_toolchain(toolchain: &str, cwd: Option<&Path>) -> Result<String, String> {
477+
String::from_utf8(run_command(&[&"rustup", &toolchain, &"which", &"rustc"], cwd)?.stdout)
478+
.map_err(|error| format!("Failed to retrieve rustc path: {:?}", error))
479+
.and_then(|rustc| {
480+
let rustc = rustc.trim().to_owned();
481+
if rustc.is_empty() { Err(format!("`rustc` path is empty")) } else { Ok(rustc) }
482+
})
483+
}
484+
462485
fn setup_rustc(env: &mut Env, args: &TestArg) -> Result<PathBuf, String> {
463486
let toolchain = format!(
464487
"+{channel}-{host}",
@@ -517,15 +540,7 @@ fn setup_rustc(env: &mut Env, args: &TestArg) -> Result<PathBuf, String> {
517540
let cargo = cargo.trim().to_owned();
518541
if cargo.is_empty() { Err(format!("`cargo` path is empty")) } else { Ok(cargo) }
519542
})?;
520-
let rustc = String::from_utf8(
521-
run_command_with_env(&[&"rustup", &toolchain, &"which", &"rustc"], rust_dir, Some(env))?
522-
.stdout,
523-
)
524-
.map_err(|error| format!("Failed to retrieve rustc path: {:?}", error))
525-
.and_then(|rustc| {
526-
let rustc = rustc.trim().to_owned();
527-
if rustc.is_empty() { Err(format!("`rustc` path is empty")) } else { Ok(rustc) }
528-
})?;
543+
let rustc = get_rustc_path_for_toolchain(&toolchain, rust_dir)?;
529544
let llvm_filecheck = match run_command_with_env(
530545
&[
531546
&"bash",
@@ -644,8 +659,10 @@ fn run_cargo_command_with_callback<F>(
644659
where
645660
F: Fn(&[&dyn AsRef<OsStr>], Option<&Path>, &Env) -> Result<(), String>,
646661
{
647-
let toolchain = get_toolchain()?;
648-
let toolchain_arg = format!("+{}", toolchain);
662+
let toolchain_arg = match args.toolchain_name {
663+
Some(ref toolchain) => toolchain.clone(),
664+
None => format!("+{}", get_toolchain()?),
665+
};
649666
let rustc_version = String::from_utf8(
650667
run_command_with_env(&[&args.config_info.rustc_command[0], &"-V"], cwd, Some(env))?.stdout,
651668
)
@@ -1098,21 +1115,19 @@ where
10981115

10991116
env.get_mut("RUSTFLAGS").unwrap().clear();
11001117

1101-
run_command_with_output_and_env(
1102-
&[
1103-
&"./x.py",
1104-
&"test",
1105-
&"--run",
1106-
&"always",
1107-
&"--stage",
1108-
&"0",
1109-
&format!("tests/{}", test_type),
1110-
&"--compiletest-rustc-args",
1111-
&rustc_args,
1112-
],
1113-
Some(&rust_path),
1114-
Some(&env),
1115-
)?;
1118+
let test_type = format!("tests/{}", test_type);
1119+
let mut command: Vec<&dyn AsRef<OsStr>> =
1120+
vec![&"./x.py", &"test", &"--run", &"always", &"--stage", &"0", &test_type];
1121+
let rustc;
1122+
if let Some(ref toolchain) = args.toolchain_name {
1123+
env.insert("COMPILETEST_FORCE_STAGE0=1".to_string(), "1".to_string());
1124+
command.push(&"--set");
1125+
rustc = format!("build.rustc={}", get_rustc_path_for_toolchain(toolchain, None)?);
1126+
command.push(&rustc);
1127+
}
1128+
command.push(&"--compiletest-rustc-args");
1129+
command.push(&rustc_args);
1130+
run_command_with_output_and_env(&command, Some(&rust_path), Some(&env))?;
11161131
Ok(())
11171132
}
11181133

0 commit comments

Comments
 (0)