Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f311ef5

Browse files
committed
Share cross-compilation code between building and testing
1 parent 70a1cb9 commit f311ef5

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

build_system/build_sysroot.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,19 @@ pub(crate) fn build_sysroot(
120120
channel,
121121
host_compiler.clone(),
122122
&cg_clif_dylib_path,
123-
None,
124123
);
125124

126125
if host_compiler.triple != target_triple {
127-
// When cross-compiling it is often necessary to manually pick the right linker
128-
let linker = match target_triple {
129-
"aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"),
130-
"s390x-unknown-linux-gnu" => Some("s390x-linux-gnu-gcc"),
131-
_ => None,
132-
};
133126
build_clif_sysroot_for_triple(
134127
dirs,
135128
channel,
136129
{
137130
let mut target_compiler = host_compiler.clone();
138131
target_compiler.triple = target_triple.to_owned();
132+
target_compiler.set_cross_linker_and_runner();
139133
target_compiler
140134
},
141135
&cg_clif_dylib_path,
142-
linker,
143136
);
144137
}
145138

@@ -167,7 +160,6 @@ fn build_clif_sysroot_for_triple(
167160
channel: &str,
168161
mut compiler: Compiler,
169162
cg_clif_dylib_path: &Path,
170-
linker: Option<&str>,
171163
) {
172164
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
173165
Err(e) => {
@@ -204,10 +196,6 @@ fn build_clif_sysroot_for_triple(
204196
if channel == "release" {
205197
rustflags.push_str(" -Zmir-opt-level=3");
206198
}
207-
if let Some(linker) = linker {
208-
use std::fmt::Write;
209-
write!(rustflags, " -Clinker={}", linker).unwrap();
210-
}
211199
compiler.rustflags += &rustflags;
212200
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
213201
if channel == "release" {

build_system/tests.rs

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -448,51 +448,26 @@ impl TestRunner {
448448
let jit_supported =
449449
is_native && host_triple.contains("x86_64") && !host_triple.contains("windows");
450450

451-
let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string());
452-
let mut runner = vec![];
451+
let host_compiler = Compiler::clif_with_triple(&dirs, host_triple);
453452

453+
let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
454454
if !is_native {
455-
match target_triple.as_str() {
456-
"aarch64-unknown-linux-gnu" => {
457-
// We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
458-
rustflags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rustflags);
459-
runner = vec![
460-
"qemu-aarch64".to_owned(),
461-
"-L".to_owned(),
462-
"/usr/aarch64-linux-gnu".to_owned(),
463-
];
464-
}
465-
"s390x-unknown-linux-gnu" => {
466-
// We are cross-compiling for s390x. Use the correct linker and run tests in qemu.
467-
rustflags = format!("-Clinker=s390x-linux-gnu-gcc{}", rustflags);
468-
runner = vec![
469-
"qemu-s390x".to_owned(),
470-
"-L".to_owned(),
471-
"/usr/s390x-linux-gnu".to_owned(),
472-
];
473-
}
474-
"x86_64-pc-windows-gnu" => {
475-
// We are cross-compiling for Windows. Run tests in wine.
476-
runner = vec!["wine".to_owned()];
477-
}
478-
_ => {
479-
println!("Unknown non-native platform");
480-
}
481-
}
455+
target_compiler.set_cross_linker_and_runner();
456+
}
457+
if let Ok(rustflags) = env::var("RUSTFLAGS") {
458+
target_compiler.rustflags.push(' ');
459+
target_compiler.rustflags.push_str(&rustflags);
460+
}
461+
if let Ok(rustdocflags) = env::var("RUSTDOCFLAGS") {
462+
target_compiler.rustdocflags.push(' ');
463+
target_compiler.rustdocflags.push_str(&rustdocflags);
482464
}
483465

484466
// FIXME fix `#[linkage = "extern_weak"]` without this
485-
if target_triple.contains("darwin") {
486-
rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags);
467+
if target_compiler.triple.contains("darwin") {
468+
target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup");
487469
}
488470

489-
let host_compiler = Compiler::clif_with_triple(&dirs, host_triple);
490-
491-
let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
492-
target_compiler.rustflags = rustflags.clone();
493-
target_compiler.rustdocflags = rustflags;
494-
target_compiler.runner = runner;
495-
496471
Self { is_native, jit_supported, dirs, host_compiler, target_compiler }
497472
}
498473

build_system/utils.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ impl Compiler {
4747
runner: vec![],
4848
}
4949
}
50+
51+
pub(crate) fn set_cross_linker_and_runner(&mut self) {
52+
match self.triple.as_str() {
53+
"aarch64-unknown-linux-gnu" => {
54+
// We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
55+
self.rustflags += " -Clinker=aarch64-linux-gnu-gcc";
56+
self.rustdocflags += " -Clinker=aarch64-linux-gnu-gcc";
57+
self.runner = vec![
58+
"qemu-aarch64".to_owned(),
59+
"-L".to_owned(),
60+
"/usr/aarch64-linux-gnu".to_owned(),
61+
];
62+
}
63+
"s390x-unknown-linux-gnu" => {
64+
// We are cross-compiling for s390x. Use the correct linker and run tests in qemu.
65+
self.rustflags += " -Clinker=s390x-linux-gnu-gcc";
66+
self.rustdocflags += " -Clinker=s390x-linux-gnu-gcc";
67+
self.runner = vec![
68+
"qemu-s390x".to_owned(),
69+
"-L".to_owned(),
70+
"/usr/s390x-linux-gnu".to_owned(),
71+
];
72+
}
73+
"x86_64-pc-windows-gnu" => {
74+
// We are cross-compiling for Windows. Run tests in wine.
75+
self.runner = vec!["wine".to_owned()];
76+
}
77+
_ => {
78+
println!("Unknown non-native platform");
79+
}
80+
}
81+
}
5082
}
5183

5284
pub(crate) struct CargoProject {

0 commit comments

Comments
 (0)