Skip to content

Commit 0f81339

Browse files
Auto merge of #139244 - jieyouxu:exp/auto-cross-run-make, r=<try>
[WIP] Enable automatic cross-compilation in run-make tests > [!CAUTION] > > Stacked on top of #142414, needs rebase. Supersedes #138066. Blocker for #141856. Based on #138066 with #139242 + #139239 cherry-picked in, plus `rustdoc()` cross-compile changes. r? `@ghost` try-job: armhf-gnu try-job: test-various
2 parents 55d4364 + 7d54282 commit 0f81339

File tree

51 files changed

+158
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+158
-64
lines changed

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

Lines changed: 17 additions & 7 deletions
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.
@@ -27,9 +27,15 @@ pub fn bare_rustc() -> Rustc {
2727
#[must_use]
2828
pub struct Rustc {
2929
cmd: Command,
30+
target: Option<String>,
3031
}
3132

32-
crate::macros::impl_common_helpers!(Rustc);
33+
// Only fill in the target just before execution, so that it can be overridden.
34+
crate::macros::impl_common_helpers!(Rustc, |rustc: &mut Rustc| {
35+
if let Some(target) = &rustc.target {
36+
rustc.cmd.arg(&format!("--target={target}"));
37+
}
38+
});
3339

3440
pub fn rustc_path() -> String {
3541
env_var("RUSTC")
@@ -46,19 +52,22 @@ impl Rustc {
4652
// `rustc` invocation constructor methods
4753

4854
/// Construct a new `rustc` invocation. This will automatically set the library
49-
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
55+
/// search path as `-L cwd()` and also the compilation target.
56+
/// Use [`bare_rustc`] to avoid this.
5057
#[track_caller]
5158
pub fn new() -> Self {
5259
let mut cmd = setup_common();
5360
cmd.arg("-L").arg(cwd());
54-
Self { cmd }
61+
62+
// Automatically default to cross-compilation
63+
Self { cmd, target: Some(target()) }
5564
}
5665

5766
/// Construct a bare `rustc` invocation with no flags set.
5867
#[track_caller]
5968
pub fn bare() -> Self {
6069
let cmd = setup_common();
61-
Self { cmd }
70+
Self { cmd, target: None }
6271
}
6372

6473
// Argument provider methods
@@ -234,8 +243,9 @@ impl Rustc {
234243

235244
/// Specify the target triple, or a path to a custom target json spec file.
236245
pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
237-
let target = target.as_ref();
238-
self.cmd.arg(format!("--target={target}"));
246+
// We store the target as a separate field, so that it can be specified multiple times.
247+
// This is in particular useful to override the default target set in Rustc::new().
248+
self.target = Some(target.as_ref().to_string());
239249
self
240250
}
241251

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,36 @@ use std::path::Path;
33

44
use crate::command::Command;
55
use crate::env::env_var;
6+
use crate::target;
67
use crate::util::set_host_compiler_dylib_path;
78

8-
/// Construct a new `rustdoc` invocation. This will configure the host compiler runtime libs.
9+
/// Construct a new `rustdoc` invocation with target automatically set to cross-compile target and
10+
/// with host compiler runtime libs configured. Use [`bare_rustdoc`] to avoid automatically setting
11+
/// cross-compile target.
912
#[track_caller]
1013
pub fn rustdoc() -> Rustdoc {
1114
Rustdoc::new()
1215
}
1316

17+
/// Bare `rustdoc` invocation, no args set.
18+
#[track_caller]
19+
pub fn bare_rustdoc() -> Rustdoc {
20+
Rustdoc::bare()
21+
}
22+
1423
#[derive(Debug)]
1524
#[must_use]
1625
pub struct Rustdoc {
1726
cmd: Command,
27+
target: Option<String>,
1828
}
1929

20-
crate::macros::impl_common_helpers!(Rustdoc);
30+
// Only fill in the target just before execution, so that it can be overridden.
31+
crate::macros::impl_common_helpers!(Rustdoc, |rustdoc: &mut Rustdoc| {
32+
if let Some(target) = &rustdoc.target {
33+
rustdoc.cmd.arg(&format!("--target={target}"));
34+
}
35+
});
2136

2237
#[track_caller]
2338
fn setup_common() -> Command {
@@ -28,11 +43,20 @@ fn setup_common() -> Command {
2843
}
2944

3045
impl Rustdoc {
31-
/// Construct a bare `rustdoc` invocation. This will configure the host compiler runtime libs.
46+
/// Construct a new `rustdoc` invocation with target automatically set to cross-compile target
47+
/// and with host compiler runtime libs configured. Use [`bare_rustdoc`] to avoid automatically
48+
/// setting cross-compile target.
3249
#[track_caller]
3350
pub fn new() -> Self {
3451
let cmd = setup_common();
35-
Self { cmd }
52+
Self { cmd, target: Some(target()) }
53+
}
54+
55+
/// Bare `rustdoc` invocation, no args set.
56+
#[track_caller]
57+
pub fn bare() -> Self {
58+
let cmd = setup_common();
59+
Self { cmd, target: None }
3660
}
3761

3862
/// Specify where an external library is located.
@@ -85,8 +109,9 @@ impl Rustdoc {
85109

86110
/// Specify the target triple, or a path to a custom target json spec file.
87111
pub fn target<S: AsRef<str>>(&mut self, target: S) -> &mut Self {
88-
let target = target.as_ref();
89-
self.cmd.arg(format!("--target={target}"));
112+
// We store the target as a separate field, so that it can be specified multiple times.
113+
// This is in particular useful to override the default target set in `Rustdoc::new()`.
114+
self.target = Some(target.as_ref().to_string());
90115
self
91116
}
92117

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub use llvm::{
6868
};
6969
pub use python::python_command;
7070
pub use rustc::{bare_rustc, rustc, rustc_path, Rustc};
71-
pub use rustdoc::{rustdoc, Rustdoc};
71+
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
7272

7373
/// [`diff`][mod@diff] is implemented in terms of the [similar] library.
7474
///

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

Lines changed: 9 additions & 0 deletions
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
/// In very rare circumstances, you may need a e.g. `bare_rustc()` or `bare_rustdoc()`
3238
/// with host runtime libs configured, but want the underlying raw
@@ -130,12 +136,14 @@ macro_rules! impl_common_helpers {
130136
/// Run the constructed command and assert that it is successfully run.
131137
#[track_caller]
132138
pub fn run(&mut self) -> crate::command::CompletedProcess {
139+
$before_exec(&mut *self);
133140
self.cmd.run()
134141
}
135142

136143
/// Run the constructed command and assert that it does not successfully run.
137144
#[track_caller]
138145
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
146+
$before_exec(&mut *self);
139147
self.cmd.run_fail()
140148
}
141149

@@ -145,6 +153,7 @@ macro_rules! impl_common_helpers {
145153
/// whenever possible.
146154
#[track_caller]
147155
pub fn run_unchecked(&mut self) -> crate::command::CompletedProcess {
156+
$before_exec(&mut *self);
148157
self.cmd.run_unchecked()
149158
}
150159

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

Lines changed: 0 additions & 5 deletions
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

Lines changed: 3 additions & 6 deletions
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/doctests-merge/rmake.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//@ needs-target-std
1+
//@ ignore-cross-compile (needs to run doctests)
2+
23
use std::path::Path;
34

45
use run_make_support::{cwd, diff, rustc, rustdoc};

tests/run-make/doctests-runtool/rmake.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//@ needs-target-std
2-
//
1+
//@ ignore-cross-compile (needs to run host tool binary)
2+
33
// Tests behavior of rustdoc `--test-runtool`.
44

55
use std::path::PathBuf;

tests/run-make/embed-metadata/rmake.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ needs-target-std
2-
//
2+
//@ needs-crate-type: dylib
3+
34
// Tests the -Zembed-metadata compiler flag.
45
// Tracking issue: https://github.com/rust-lang/rust/issues/139165
56

tests/run-make/embed-source-dwarf/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ needs-target-std
22
//@ ignore-windows
33
//@ ignore-apple
4+
//@ ignore-wasm (`object` doesn't handle wasm object files)
45

56
// This test should be replaced with one in tests/debuginfo once we can easily
67
// tell via GDB or LLDB if debuginfo contains source code. Cheap tricks in LLDB

tests/run-make/emit-shared-files/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// `all-shared` should only emit files that can be shared between crates.
66
// See https://github.com/rust-lang/rust/pull/83478
77

8+
//@ needs-target-std
9+
810
use run_make_support::{has_extension, has_prefix, path, rustdoc, shallow_find_files};
911

1012
fn main() {

tests/run-make/emit-stack-sizes/rmake.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// See https://github.com/rust-lang/rust/pull/51946
88

99
//@ needs-target-std
10-
//@ ignore-windows
11-
//@ ignore-apple
10+
//@ only-elf
1211
// Reason: this feature only works when the output object format is ELF.
1312
// This won't be the case on Windows/OSX - for example, OSX produces a Mach-O binary.
1413

tests/run-make/env-dep-info/rmake.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ needs-target-std
2-
//
2+
//@ needs-crate-type: proc-macro
3+
//@ ignore-musl (FIXME: can't find `-lunwind`)
4+
35
// Inside dep-info emit files, #71858 made it so all accessed environment
46
// variables are usefully printed. This test checks that this feature works
57
// as intended by checking if the environment variables used in compilation

tests/run-make/export/disambiguator/rmake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//@ needs-target-std
2+
3+
// NOTE: `sdylib`'s platform support is basically just `dylib`'s platform support.
4+
//@ needs-crate-type: dylib
5+
26
use run_make_support::rustc;
37

48
fn main() {

tests/run-make/export/extern-opt/rmake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//@ needs-target-std
2+
3+
// NOTE: `sdylib`'s platform support is basically that of `dylib`.
4+
//@ needs-crate-type: dylib
5+
26
use run_make_support::{dynamic_lib_name, rustc};
37

48
fn main() {

tests/run-make/export/simple/rmake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//@ needs-target-std
2+
3+
// NOTE: `sdylib`'s platform support is basically that of `dylib`.
4+
//@ needs-crate-type: dylib
5+
26
use run_make_support::rustc;
37

48
fn main() {

tests/run-make/ice-dep-cannot-find-dep/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//@ only-x86_64
1111
//@ only-linux
12+
//@ ignore-cross-compile
1213
// Reason: This is a platform-independent issue, no need to waste time testing
1314
// everywhere.
1415

tests/run-make/include-all-symbols-linking/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// See https://github.com/rust-lang/rust/issues/47384
99

1010
//@ needs-target-std
11+
//@ needs-crate-type: cdylib
12+
//@ needs-dynamic-linking
1113
//@ ignore-wasm differences in object file formats causes errors in the llvm_objdump step.
1214
//@ ignore-windows differences in object file formats causes errors in the llvm_objdump step.
1315

tests/run-make/invalid-so/rmake.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ needs-target-std
2-
//
2+
//@ needs-crate-type: dylib
3+
//@ needs-dynamic-linking
4+
35
// When a fake library was given to the compiler, it would
46
// result in an obscure and unhelpful error message. This test
57
// creates a false "foo" dylib, and checks that the standard error

tests/run-make/link-args-order/rmake.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ needs-target-std
2-
//
2+
//@ ignore-wasm (explicit linker invocations)
3+
34
// Passing linker arguments to the compiler used to be lost or reordered in a messy way
45
// as they were passed further to the linker. This was fixed in #70665, and this test
56
// checks that linker arguments remain intact and in the order they were originally passed in.

0 commit comments

Comments
 (0)