Enable automatic cross-compilation in run-make tests

This commit is contained in:
Jakub Beránek
2025-03-05 20:18:05 +01:00
committed by Jieyou Xu
parent 1bb335244c
commit a27bdea4b7
8 changed files with 31 additions and 26 deletions

View File

@@ -6,7 +6,7 @@ use crate::command::Command;
use crate::env::env_var;
use crate::path_helpers::cwd;
use crate::util::set_host_compiler_dylib_path;
use crate::{is_aix, is_darwin, is_msvc, is_windows, uname};
use crate::{is_aix, is_darwin, is_msvc, is_windows, target, uname};
/// Construct a new `rustc` invocation. This will automatically set the library
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
@@ -27,9 +27,15 @@ pub fn bare_rustc() -> Rustc {
#[must_use]
pub struct Rustc {
cmd: Command,
target: Option<String>,
}
crate::macros::impl_common_helpers!(Rustc);
// Only fill in the target just before execution, so that it can be overridden.
crate::macros::impl_common_helpers!(Rustc, |rustc: &mut Rustc| {
if let Some(target) = &rustc.target {
rustc.cmd.arg(&format!("--target={target}"));
}
});
pub fn rustc_path() -> String {
env_var("RUSTC")
@@ -46,19 +52,22 @@ impl Rustc {
// `rustc` invocation constructor methods
/// Construct a new `rustc` invocation. This will automatically set the library
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
/// search path as `-L cwd()` and also the compilation target.
/// Use [`bare_rustc`] to avoid this.
#[track_caller]
pub fn new() -> Self {
let mut cmd = setup_common();
cmd.arg("-L").arg(cwd());
Self { cmd }
// Automatically default to cross-compilation
Self { cmd, target: Some(target()) }
}
/// Construct a bare `rustc` invocation with no flags set.
#[track_caller]
pub fn bare() -> Self {
let cmd = setup_common();
Self { cmd }
Self { cmd, target: None }
}
// Argument provider methods
@@ -234,8 +243,9 @@ impl Rustc {
/// Specify the target triple, or a path to a custom target json spec file.
pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
let target = target.as_ref();
self.cmd.arg(format!("--target={target}"));
// We store the target as a separate field, so that it can be specified multiple times.
// This is in particular useful to override the default target set in Rustc::new().
self.target = Some(target.as_ref().to_string());
self
}

View File

@@ -23,10 +23,16 @@
/// }
/// ```
///
/// You can pass an optional second parameter which should be a function that is passed
/// `&mut self` just before the command is executed.
///
/// [`Command`]: crate::command::Command
/// [`CompletedProcess`]: crate::command::CompletedProcess
macro_rules! impl_common_helpers {
($wrapper: ident) => {
$crate::macros::impl_common_helpers!($wrapper, |_| {});
};
($wrapper: ident, $before_exec: expr) => {
impl $wrapper {
/// In very rare circumstances, you may need a e.g. `bare_rustc()` or `bare_rustdoc()`
/// with host runtime libs configured, but want the underlying raw
@@ -130,12 +136,14 @@ macro_rules! impl_common_helpers {
/// Run the constructed command and assert that it is successfully run.
#[track_caller]
pub fn run(&mut self) -> crate::command::CompletedProcess {
$before_exec(&mut *self);
self.cmd.run()
}
/// Run the constructed command and assert that it does not successfully run.
#[track_caller]
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
$before_exec(&mut *self);
self.cmd.run_fail()
}
@@ -145,6 +153,7 @@ macro_rules! impl_common_helpers {
/// whenever possible.
#[track_caller]
pub fn run_unchecked(&mut self) -> crate::command::CompletedProcess {
$before_exec(&mut *self);
self.cmd.run_unchecked()
}

View File

@@ -41,7 +41,6 @@ fn main() {
// Remove env vars to get `rustc`'s default
let output = rustc()
.target(target())
.env_remove("MACOSX_DEPLOYMENT_TARGET")
.env_remove("IPHONEOS_DEPLOYMENT_TARGET")
.env_remove("WATCHOS_DEPLOYMENT_TARGET")
@@ -58,7 +57,6 @@ fn main() {
run_in_tmpdir(|| {
let rustc = || {
let mut rustc = rustc();
rustc.target(target());
rustc.crate_type("lib");
rustc.emit("obj");
rustc.input("foo.rs");
@@ -82,7 +80,6 @@ fn main() {
let rustc = || {
let mut rustc = rustc();
rustc.target(target());
rustc.crate_type("dylib");
rustc.input("foo.rs");
rustc.output("libfoo.dylib");
@@ -108,7 +105,6 @@ fn main() {
run_in_tmpdir(|| {
let rustc = || {
let mut rustc = rustc();
rustc.target(target());
rustc.crate_type("bin");
rustc.input("foo.rs");
rustc.output("foo");
@@ -147,7 +143,6 @@ fn main() {
run_in_tmpdir(|| {
let rustc = || {
let mut rustc = rustc();
rustc.target(target());
rustc.incremental("incremental");
rustc.crate_type("lib");
rustc.emit("obj");

View File

@@ -24,8 +24,7 @@ fn has_sdk_version(file: &str, version: &str) {
fn main() {
// Fetch rustc's inferred deployment target.
let current_deployment_target =
rustc().target(target()).print("deployment-target").run().stdout_utf8();
let current_deployment_target = rustc().print("deployment-target").run().stdout_utf8();
let current_deployment_target = current_deployment_target.split('=').last().unwrap().trim();
// Fetch current SDK version via. xcrun.
@@ -45,7 +44,7 @@ fn main() {
let current_sdk_version = current_sdk_version.trim();
// Check the SDK version in the object file produced by the codegen backend.
rustc().target(target()).crate_type("lib").emit("obj").input("foo.rs").output("foo.o").run();
rustc().crate_type("lib").emit("obj").input("foo.rs").output("foo.o").run();
// Set to 0, which means not set or "n/a".
has_sdk_version("foo.o", "n/a");
@@ -53,7 +52,7 @@ fn main() {
//
// This is just to ensure that we don't set some odd version in `create_object_file`,
// if the rmeta file is packed in a different way in the future, this can safely be removed.
rustc().target(target()).crate_type("rlib").input("foo.rs").output("libfoo.rlib").run();
rustc().crate_type("rlib").input("foo.rs").output("libfoo.rlib").run();
// Extra .rmeta file (which is encoded as an object file).
cmd("ar").arg("-x").arg("libfoo.rlib").arg("lib.rmeta").run();
has_sdk_version("lib.rmeta", "n/a");
@@ -69,7 +68,6 @@ fn main() {
// Test with clang
let file_name = format!("foo_cc{file_ext}");
rustc()
.target(target())
.crate_type("bin")
.arg("-Clinker-flavor=gcc")
.input("foo.rs")
@@ -80,7 +78,6 @@ fn main() {
// Test with ld64
let file_name = format!("foo_ld{file_ext}");
rustc()
.target(target())
.crate_type("bin")
.arg("-Clinker-flavor=ld")
.input("foo.rs")

View File

@@ -22,11 +22,7 @@ fn run_test(variant: &str) {
flags
};
println!("{variant} test...");
rustc()
.input(format!("foo_{variant}.rs"))
.target(target())
.linker("aarch64-linux-gnu-gcc")
.run();
rustc().input(format!("foo_{variant}.rs")).linker("aarch64-linux-gnu-gcc").run();
gcc()
.input(format!("bar_{variant}.c"))
.input(dynamic_lib_name("foo"))

View File

@@ -10,7 +10,7 @@ use run_make_support::{diff, rustc, target};
fn main() {
rustc().input("foo.rs").run();
rustc().input("bar.rs").target(target()).emit("dep-info").run();
rustc().input("bar.rs").emit("dep-info").run();
// The emitted file should not contain "proc-macro source".
diff().expected_file("correct.d").actual_file("bar.d").run();
}

View File

@@ -49,7 +49,6 @@ fn test(compiler: &str) {
rustc()
.input("test-aslr.rs")
.target(target())
.linker(compiler)
.arg("-Clinker-flavor=gcc")
.arg("-Ctarget-feature=+crt-static")

View File

@@ -31,7 +31,6 @@ fn check_crate_is_unstable(cr: &Crate) {
// Trying to use this crate from a user program should fail.
let output = rustc()
.crate_type("rlib")
.target(target())
.extern_(name, path)
.input("-")
.stdin_buf(format!("extern crate {name};"))