Skip to content

Commit 5a77c4a

Browse files
committed
Auto merge of rust-lang#139244 - jieyouxu:exp/auto-cross-run-make, r=<try>
[WIP] [WIP] Enable automatic cross-compilation in run-make tests WIP for the WIP rust-lang#138066. Based on rust-lang#138066 with rust-lang#139242 + rust-lang#139239 cherry-picked in, plus `rustdoc()` cross-compile changes. r? `@ghost` try-job: armhf-gnu try-job: test-various try-job: x86_64-msvc-1 try-job: i686-msvc-1 try-job: x86_64-mingw-1 try-job: aarch64-apple try-job: x86_64-apple-1
2 parents 2ef7858 + 8d6ff0e commit 5a77c4a

File tree

32 files changed

+126
-52
lines changed

32 files changed

+126
-52
lines changed

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

+17-7
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

+29-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,35 @@ 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.
9+
/// Construct a new `rustdoc` invocation with target automatically set to cross-compile target. Use
10+
/// [`bare_rustdoc`] to avoid this.
911
#[track_caller]
1012
pub fn rustdoc() -> Rustdoc {
1113
Rustdoc::new()
1214
}
1315

16+
/// Bare `rustdoc` invocation, no args set.
17+
#[track_caller]
18+
pub fn bare_rustdoc() -> Rustdoc {
19+
Rustdoc::bare()
20+
}
21+
1422
#[derive(Debug)]
1523
#[must_use]
1624
pub struct Rustdoc {
1725
cmd: Command,
26+
target: Option<String>,
1827
}
1928

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

2236
#[track_caller]
2337
fn setup_common() -> Command {
@@ -28,11 +42,19 @@ fn setup_common() -> Command {
2842
}
2943

3044
impl Rustdoc {
31-
/// Construct a bare `rustdoc` invocation.
45+
/// Construct a new `rustdoc` invocation with target automatically set to cross-compile target.
46+
/// Use [`bare_rustdoc`] to avoid this.
3247
#[track_caller]
3348
pub fn new() -> Self {
3449
let cmd = setup_common();
35-
Self { cmd }
50+
Self { cmd, target: Some(target()) }
51+
}
52+
53+
/// Bare `rustdoc` invocation, no args set.
54+
#[track_caller]
55+
pub fn bare() -> Self {
56+
let cmd = setup_common();
57+
Self { cmd, target: None }
3658
}
3759

3860
/// Specify where an external library is located.
@@ -85,8 +107,9 @@ impl Rustdoc {
85107

86108
/// Specify the target triple, or a path to a custom target json spec file.
87109
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}"));
110+
// We store the target as a separate field, so that it can be specified multiple times.
111+
// This is in particular useful to override the default target set in `Rustdoc::new()`.
112+
self.target = Some(target.as_ref().to_string());
90113
self
91114
}
92115

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

+1-1
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

+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/doctests-merge/rmake.rs

+2
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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Tests behavior of rustdoc `--runtool`.
22

3+
//@ ignore-cross-compile (attempts to run the doctests)
4+
35
use std::path::PathBuf;
46

57
use run_make_support::rfs::{create_dir, remove_dir_all};

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Tests the -Zembed-metadata compiler flag.
22
// Tracking issue: https://github.com/rust-lang/rust/issues/139165
33

4+
//@ needs-dynamic-linking
5+
//@ needs-crate-type: dylib
6+
47
use run_make_support::rfs::{create_dir, remove_file, rename};
5-
use run_make_support::{Rustc, dynamic_lib_name, path, run_in_tmpdir, rust_lib_name, rustc};
8+
use run_make_support::{
9+
Rustc, dynamic_lib_name, path, run_in_tmpdir, rust_lib_name, rustc, target,
10+
};
611

712
#[derive(Debug, Copy, Clone)]
813
enum LibraryKind {
@@ -42,7 +47,7 @@ fn main() {
4247
fn lookup_rmeta_in_lib_dir(kind: LibraryKind) {
4348
run_in_tmpdir(|| {
4449
build_dep_rustc(kind).run();
45-
rustc().input("foo.rs").run();
50+
rustc().target(target()).input("foo.rs").run();
4651
});
4752
}
4853

@@ -54,6 +59,7 @@ fn lookup_rmeta_through_extern(kind: LibraryKind) {
5459
build_dep_rustc(kind).out_dir("deps").run();
5560

5661
let mut rustc = rustc();
62+
rustc.target(target());
5763
kind.add_extern(&mut rustc, "dep1", "deps");
5864
rustc.extern_("dep1", path("deps").join("libdep1.rmeta"));
5965
rustc.input("foo.rs").run();
@@ -67,13 +73,15 @@ fn lookup_rmeta_missing(kind: LibraryKind) {
6773
build_dep_rustc(kind).out_dir("deps").run();
6874

6975
let mut rustc = rustc();
76+
rustc.target(target());
7077
kind.add_extern(&mut rustc, "dep1", "deps");
7178
rustc.input("foo.rs").run_fail().assert_stderr_contains("only metadata stub found");
7279
});
7380
}
7481

7582
fn build_dep_rustc(kind: LibraryKind) -> Rustc {
7683
let mut dep_rustc = rustc();
84+
dep_rustc.target(target());
7785
dep_rustc
7886
.arg("-Zembed-metadata=no")
7987
.crate_type(kind.crate_type())

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
//@ ignore-windows
2-
//@ ignore-apple
1+
//@ ignore-apple (needs DWARF)
2+
//@ ignore-windows-msvc (needs DWARF)
3+
//@ ignore-cross-compile (cross-compiled target can produce artifacts `object` can't yet parse)
34

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

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
// this diagnostics information should be located.
77
// See https://github.com/rust-lang/rust/pull/51946
88

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

14-
use run_make_support::{llvm_readobj, rustc};
11+
use run_make_support::{llvm_readobj, rustc, target};
1512

1613
fn main() {
17-
rustc().opt_level("3").arg("-Zemit-stack-sizes").emit("obj").input("foo.rs").run();
14+
rustc()
15+
.target(target())
16+
.opt_level("3")
17+
.arg("-Zemit-stack-sizes")
18+
.emit("obj")
19+
.input("foo.rs")
20+
.run();
1821
llvm_readobj()
1922
.arg("--section-headers")
2023
.input("foo.o")

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

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// appear in the output dep-info files.
55
// See https://github.com/rust-lang/rust/issues/40364
66

7+
//@ ignore-cross-compile (cross-compile targets can introduce complicated env differences)
8+
79
use run_make_support::{diff, rustc};
810

911
fn main() {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
// explains that the file exists, but that its metadata is incorrect.
55
// See https://github.com/rust-lang/rust/pull/88368
66

7-
use run_make_support::{dynamic_lib_name, rfs, rustc};
7+
//@ needs-crate-type: dylib
8+
9+
use run_make_support::{dynamic_lib_name, rfs, rustc, target};
810

911
fn main() {
1012
rfs::create_file(dynamic_lib_name("foo"));
1113
rustc()
14+
.target(target())
1215
.crate_type("lib")
1316
.extern_("foo", dynamic_lib_name("foo"))
1417
.input("bar.rs")

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// checks that linker arguments remain intact and in the order they were originally passed in.
44
// See https://github.com/rust-lang/rust/pull/70665
55

6+
//@ ignore-cross-compile (exercises host linker)
7+
68
use run_make_support::{is_msvc, rustc};
79

810
fn main() {

0 commit comments

Comments
 (0)