Skip to content

Commit 990d60e

Browse files
committed
Enable automatic cross-compilation in run-make tests
1 parent 4559163 commit 990d60e

File tree

8 files changed

+32
-27
lines changed

8 files changed

+32
-27
lines changed

src/tools/run-make-support/src/external_deps/rustc.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::command::Command;
66
use crate::env::env_var;
77
use crate::path_helpers::cwd;
88
use crate::util::set_host_compiler_dylib_path;
9-
use crate::{is_aix, is_darwin, is_msvc, is_windows, uname};
9+
use crate::{is_aix, is_darwin, is_msvc, is_windows, target, uname};
1010

1111
/// Construct a new `rustc` invocation. This will automatically set the library
1212
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
@@ -33,9 +33,15 @@ pub fn aux_build() -> Rustc {
3333
#[must_use]
3434
pub struct Rustc {
3535
cmd: Command,
36+
target: Option<String>,
3637
}
3738

38-
crate::macros::impl_common_helpers!(Rustc);
39+
/// Only fill in the target just before execution, so that it can be overridden.
40+
crate::macros::impl_common_helpers!(Rustc, |rustc: &mut Rustc| {
41+
if let Some(target) = &rustc.target {
42+
rustc.cmd.arg(&format!("--target={target}"));
43+
}
44+
});
3945

4046
pub fn rustc_path() -> String {
4147
env_var("RUSTC")
@@ -52,27 +58,30 @@ impl Rustc {
5258
// `rustc` invocation constructor methods
5359

5460
/// Construct a new `rustc` invocation. This will automatically set the library
55-
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
61+
/// search path as `-L cwd()` and also the compilation target.
62+
/// Use [`bare_rustc`] to avoid this.
5663
#[track_caller]
5764
pub fn new() -> Self {
5865
let mut cmd = setup_common();
5966
cmd.arg("-L").arg(cwd());
60-
Self { cmd }
67+
68+
// Automatically default to cross-compilation
69+
Self { cmd, target: Some(target()) }
6170
}
6271

6372
/// Construct a bare `rustc` invocation with no flags set.
6473
#[track_caller]
6574
pub fn bare() -> Self {
6675
let cmd = setup_common();
67-
Self { cmd }
76+
Self { cmd, target: None }
6877
}
6978

7079
/// Construct a new `rustc` invocation with `aux_build` preset (setting `--crate-type=lib`).
7180
#[track_caller]
7281
pub fn new_aux_build() -> Self {
7382
let mut cmd = setup_common();
7483
cmd.arg("--crate-type=lib");
75-
Self { cmd }
84+
Self { cmd, target: None }
7685
}
7786

7887
// Argument provider methods
@@ -248,8 +257,9 @@ impl Rustc {
248257

249258
/// Specify the target triple, or a path to a custom target json spec file.
250259
pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
251-
let target = target.as_ref();
252-
self.cmd.arg(format!("--target={target}"));
260+
// We store the target as a separate field, so that it can be specified multiple times.
261+
// This is in particular useful to override the default target set in Rustc::new().
262+
self.target = Some(target.as_ref().to_string());
253263
self
254264
}
255265

src/tools/run-make-support/src/macros.rs

+9
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
/// }
2424
/// ```
2525
///
26+
/// You can pass an optional second parameter which should be a function that is passed
27+
/// `&mut self` just before the command is executed.
28+
///
2629
/// [`Command`]: crate::command::Command
2730
/// [`CompletedProcess`]: crate::command::CompletedProcess
2831
macro_rules! impl_common_helpers {
2932
($wrapper: ident) => {
33+
$crate::macros::impl_common_helpers!($wrapper, |_| {});
34+
};
35+
($wrapper: ident, $before_exec: expr) => {
3036
impl $wrapper {
3137
/// Specify an environment variable.
3238
pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Self
@@ -118,12 +124,14 @@ macro_rules! impl_common_helpers {
118124
/// Run the constructed command and assert that it is successfully run.
119125
#[track_caller]
120126
pub fn run(&mut self) -> crate::command::CompletedProcess {
127+
$before_exec(&mut *self);
121128
self.cmd.run()
122129
}
123130

124131
/// Run the constructed command and assert that it does not successfully run.
125132
#[track_caller]
126133
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
134+
$before_exec(&mut *self);
127135
self.cmd.run_fail()
128136
}
129137

@@ -133,6 +141,7 @@ macro_rules! impl_common_helpers {
133141
/// whenever possible.
134142
#[track_caller]
135143
pub fn run_unchecked(&mut self) -> crate::command::CompletedProcess {
144+
$before_exec(&mut *self);
136145
self.cmd.run_unchecked()
137146
}
138147

tests/run-make/apple-deployment-target/rmake.rs

-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ fn main() {
4141

4242
// Remove env vars to get `rustc`'s default
4343
let output = rustc()
44-
.target(target())
4544
.env_remove("MACOSX_DEPLOYMENT_TARGET")
4645
.env_remove("IPHONEOS_DEPLOYMENT_TARGET")
4746
.env_remove("WATCHOS_DEPLOYMENT_TARGET")
@@ -58,7 +57,6 @@ fn main() {
5857
run_in_tmpdir(|| {
5958
let rustc = || {
6059
let mut rustc = rustc();
61-
rustc.target(target());
6260
rustc.crate_type("lib");
6361
rustc.emit("obj");
6462
rustc.input("foo.rs");
@@ -82,7 +80,6 @@ fn main() {
8280

8381
let rustc = || {
8482
let mut rustc = rustc();
85-
rustc.target(target());
8683
rustc.crate_type("dylib");
8784
rustc.input("foo.rs");
8885
rustc.output("libfoo.dylib");
@@ -108,7 +105,6 @@ fn main() {
108105
run_in_tmpdir(|| {
109106
let rustc = || {
110107
let mut rustc = rustc();
111-
rustc.target(target());
112108
rustc.crate_type("bin");
113109
rustc.input("foo.rs");
114110
rustc.output("foo");
@@ -147,7 +143,6 @@ fn main() {
147143
run_in_tmpdir(|| {
148144
let rustc = || {
149145
let mut rustc = rustc();
150-
rustc.target(target());
151146
rustc.incremental("incremental");
152147
rustc.crate_type("lib");
153148
rustc.emit("obj");

tests/run-make/apple-sdk-version/rmake.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ fn has_sdk_version(file: &str, version: &str) {
2424

2525
fn main() {
2626
// Fetch rustc's inferred deployment target.
27-
let current_deployment_target =
28-
rustc().target(target()).print("deployment-target").run().stdout_utf8();
27+
let current_deployment_target = rustc().print("deployment-target").run().stdout_utf8();
2928
let current_deployment_target = current_deployment_target.split('=').last().unwrap().trim();
3029

3130
// Fetch current SDK version via. xcrun.
@@ -45,15 +44,15 @@ fn main() {
4544
let current_sdk_version = current_sdk_version.trim();
4645

4746
// Check the SDK version in the object file produced by the codegen backend.
48-
rustc().target(target()).crate_type("lib").emit("obj").input("foo.rs").output("foo.o").run();
47+
rustc().crate_type("lib").emit("obj").input("foo.rs").output("foo.o").run();
4948
// Set to 0, which means not set or "n/a".
5049
has_sdk_version("foo.o", "n/a");
5150

5251
// Check the SDK version in the .rmeta file, as set in `create_object_file`.
5352
//
5453
// This is just to ensure that we don't set some odd version in `create_object_file`,
5554
// if the rmeta file is packed in a different way in the future, this can safely be removed.
56-
rustc().target(target()).crate_type("rlib").input("foo.rs").output("libfoo.rlib").run();
55+
rustc().crate_type("rlib").input("foo.rs").output("libfoo.rlib").run();
5756
// Extra .rmeta file (which is encoded as an object file).
5857
cmd("ar").arg("-x").arg("libfoo.rlib").arg("lib.rmeta").run();
5958
has_sdk_version("lib.rmeta", "n/a");
@@ -69,7 +68,6 @@ fn main() {
6968
// Test with clang
7069
let file_name = format!("foo_cc{file_ext}");
7170
rustc()
72-
.target(target())
7371
.crate_type("bin")
7472
.arg("-Clinker-flavor=gcc")
7573
.input("foo.rs")
@@ -80,7 +78,6 @@ fn main() {
8078
// Test with ld64
8179
let file_name = format!("foo_ld{file_ext}");
8280
rustc()
83-
.target(target())
8481
.crate_type("bin")
8582
.arg("-Clinker-flavor=ld")
8683
.input("foo.rs")

tests/run-make/mte-ffi/rmake.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ fn run_test(variant: &str) {
2222
flags
2323
};
2424
println!("{variant} test...");
25-
rustc()
26-
.input(format!("foo_{variant}.rs"))
27-
.target(target())
28-
.linker("aarch64-linux-gnu-gcc")
29-
.run();
25+
rustc().input(format!("foo_{variant}.rs")).linker("aarch64-linux-gnu-gcc").run();
3026
gcc()
3127
.input(format!("bar_{variant}.c"))
3228
.input(dynamic_lib_name("foo"))

tests/run-make/rustc-macro-dep-files/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use run_make_support::{diff, rustc, target};
88

99
fn main() {
1010
rustc().input("foo.rs").run();
11-
rustc().input("bar.rs").target(target()).emit("dep-info").run();
11+
rustc().input("bar.rs").emit("dep-info").run();
1212
// The emitted file should not contain "proc-macro source".
1313
diff().expected_file("correct.d").actual_file("bar.d").run();
1414
}

tests/run-make/static-pie/rmake.rs

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ fn test(compiler: &str) {
4949

5050
rustc()
5151
.input("test-aslr.rs")
52-
.target(target())
5352
.linker(compiler)
5453
.arg("-Clinker-flavor=gcc")
5554
.arg("-Ctarget-feature=+crt-static")

tests/run-make/sysroot-crates-are-unstable/rmake.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fn check_crate_is_unstable(cr: &Crate) {
3131
// Trying to use this crate from a user program should fail.
3232
let output = rustc()
3333
.crate_type("rlib")
34-
.target(target())
3534
.extern_(name, path)
3635
.input("-")
3736
.stdin_buf(format!("extern crate {name};"))

0 commit comments

Comments
 (0)