compiletest: conditionally build and provide minicore as extern prelude when requested via //@ add-core-stubs directive

`//@ add-core-stubs` will imply `-Cpanic=abort`.
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-10-22 18:44:40 +08:00
parent 95d01fcee9
commit a737f75a04

View File

@ -1150,14 +1150,20 @@ fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Path, rustc: &mut Comman
} }
} }
/// `root_testpaths` refers to the path of the original test. /// `root_testpaths` refers to the path of the original test. the auxiliary and the test with an
/// the auxiliary and the test with an aux-build have the same `root_testpaths`. /// aux-build have the same `root_testpaths`.
fn compose_and_run_compiler( fn compose_and_run_compiler(
&self, &self,
mut rustc: Command, mut rustc: Command,
input: Option<String>, input: Option<String>,
root_testpaths: &TestPaths, root_testpaths: &TestPaths,
) -> ProcRes { ) -> ProcRes {
if self.props.add_core_stubs {
let minicore_path = self.build_minicore();
rustc.arg("--extern");
rustc.arg(&format!("minicore={}", minicore_path.to_str().unwrap()));
}
let aux_dir = self.aux_output_dir(); let aux_dir = self.aux_output_dir();
self.build_all_auxiliary(root_testpaths, &aux_dir, &mut rustc); self.build_all_auxiliary(root_testpaths, &aux_dir, &mut rustc);
@ -1171,6 +1177,37 @@ fn compose_and_run_compiler(
) )
} }
/// Builds `minicore`. Returns the path to the minicore rlib within the base test output
/// directory.
fn build_minicore(&self) -> PathBuf {
let output_file_path = self.output_base_dir().join("libminicore.rlib");
let mut rustc = self.make_compile_args(
&self.config.minicore_path,
TargetLocation::ThisFile(output_file_path.clone()),
Emit::None,
AllowUnused::Yes,
LinkToAux::No,
vec![],
);
rustc.args(&["--crate-type", "rlib"]);
rustc.arg("-Cpanic=abort");
let res =
self.compose_and_run(rustc, self.config.compile_lib_path.to_str().unwrap(), None, None);
if !res.status.success() {
self.fatal_proc_rec(
&format!(
"auxiliary build of {:?} failed to compile: ",
self.config.minicore_path.display()
),
&res,
);
}
output_file_path
}
/// Builds an aux dependency. /// Builds an aux dependency.
fn build_auxiliary( fn build_auxiliary(
&self, &self,
@ -1662,6 +1699,15 @@ fn make_compile_args(
rustc.args(&self.props.compile_flags); rustc.args(&self.props.compile_flags);
// FIXME(jieyouxu): we should report a fatal error or warning if user wrote `-Cpanic=` with
// something that's not `abort`, however, by moving this last we should override previous
// `-Cpanic=`s
//
// `minicore` requires `#![no_std]` and `#![no_core]`, which means no unwinding panics.
if self.props.add_core_stubs {
rustc.arg("-Cpanic=abort");
}
rustc rustc
} }