Skip to content

Commit 9394804

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 68ac5ab + b674cb7 commit 9394804

File tree

121 files changed

+297
-69
lines changed

Some content is hidden

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

121 files changed

+297
-69
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/CURRENT_RUSTC_VERSION/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ needs-target-std
12
// ignore-tidy-linelength
23

34
// Check that the `CURRENT_RUSTC_VERSION` placeholder is correctly replaced by the current

tests/run-make/allow-warnings-cmdline-stability/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ needs-target-std
12
// Test that `-Awarnings` suppresses warnings for unstable APIs.
23

34
use run_make_support::rustc;

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/artifact-incr-cache-no-obj/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// emitting an object file is not necessary if user didn't ask for one
24
//
35
// This test is similar to run-make/artifact-incr-cache but it doesn't

tests/run-make/artifact-incr-cache/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// rustc should be able to emit required files (asm, llvm-*, etc) during incremental
24
// compilation on the first pass by running the code gen as well as on subsequent runs -
35
// extracting them from the cache

tests/run-make/bin-emit-no-symbols/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// When setting the crate type as a "bin" (in app.rs),
24
// this could cause a bug where some symbols would not be
35
// emitted in the object files. This has been fixed, and

tests/run-make/box-struct-no-segfault/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// The crate "foo" tied to this test executes a very specific function,
24
// which involves boxing an instance of the struct Foo. However,
35
// this once caused a segmentation fault in cargo release builds due to an LLVM

tests/run-make/checksum-freshness/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ needs-target-std
12
use run_make_support::{rfs, rustc};
23

34
fn main() {

tests/run-make/compiler-lookup-paths-2/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// This test checks that extern crate declarations in Cargo without a corresponding declaration
24
// in the manifest of a dependency are NOT allowed. The last rustc call does it anyways, which
35
// should result in a compilation failure.

tests/run-make/compiler-lookup-paths/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// Since #19941, rustc can accept specifications on its library search paths.
24
// This test runs Rust programs with varied library dependencies, expecting them
35
// to succeed or fail depending on the situation.

tests/run-make/const-trait-stable-toolchain/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// Test output of const super trait errors in both stable and nightly.
24
// We don't want to provide suggestions on stable that only make sense in nightly.
35

tests/run-make/crate-circular-deps-link/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// Test that previously triggered a linker failure with root cause
24
// similar to one found in the issue #69368.
35
//

tests/run-make/cross-lang-lto-upstream-rlibs/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// When using the flag -C linker-plugin-lto, static libraries could lose their upstream object
24
// files during compilation. This bug was fixed in #53031, and this test compiles a staticlib
35
// dependent on upstream, checking that the upstream object file still exists after no LTO and

tests/run-make/cross-lang-lto/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// This test checks that the object files we generate are actually
24
// LLVM bitcode files (as used by linker LTO plugins) when compiling with
35
// -Clinker-plugin-lto.

tests/run-make/debugger-visualizer-dep-info/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// This test checks that files referenced via #[debugger_visualizer] are
24
// included in `--emit dep-info` output.
35
// See https://github.com/rust-lang/rust/pull/111641

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// This is a simple smoke test for rustc's `--emit dep-info` feature. It prints out
24
// information about dependencies in a Makefile-compatible format, as a `.d` file.
35
// Note that this test does not check that the `.d` file is Makefile-compatible.

tests/run-make/diagnostics-traits-from-duplicate-crates/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// Non-regression test for issue #132920 where multiple versions of the same crate are present in
24
// the dependency graph, and an unexpected error in a dependent crate caused an ICE in the
35
// unsatisfied bounds diagnostics for traits present in multiple crate versions.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ ignore-cross-compile (needs to run doctests)
2+
13
use std::path::Path;
24

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

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

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

35
use std::path::PathBuf;

tests/run-make/dump-mono-stats/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ needs-target-std
2+
//
13
// A flag named dump-mono-stats was added to the compiler in 2022, which
24
// collects stats on instantiation of items and their associated costs.
35
// This test checks that the output stat file exists, and that it contains

tests/run-make/duplicate-output-flavors/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ needs-target-std
12
use run_make_support::rustc;
23

34
fn main() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ needs-target-std
2+
//@ needs-crate-type: dylib
3+
14
// Tests the -Zembed-metadata compiler flag.
25
// Tracking issue: https://github.com/rust-lang/rust/issues/139165
36

0 commit comments

Comments
 (0)