diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index f7bdf517976..3ec00e08ef7 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -120,26 +120,19 @@ pub(crate) fn build_sysroot( channel, host_compiler.clone(), &cg_clif_dylib_path, - None, ); if host_compiler.triple != target_triple { - // When cross-compiling it is often necessary to manually pick the right linker - let linker = match target_triple { - "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"), - "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu-gcc"), - _ => None, - }; build_clif_sysroot_for_triple( dirs, channel, { let mut target_compiler = host_compiler.clone(); target_compiler.triple = target_triple.to_owned(); + target_compiler.set_cross_linker_and_runner(); target_compiler }, &cg_clif_dylib_path, - linker, ); } @@ -167,7 +160,6 @@ fn build_clif_sysroot_for_triple( channel: &str, mut compiler: Compiler, cg_clif_dylib_path: &Path, - linker: Option<&str>, ) { match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { Err(e) => { @@ -204,10 +196,6 @@ fn build_clif_sysroot_for_triple( if channel == "release" { rustflags.push_str(" -Zmir-opt-level=3"); } - if let Some(linker) = linker { - use std::fmt::Write; - write!(rustflags, " -Clinker={}", linker).unwrap(); - } compiler.rustflags += &rustflags; let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs); if channel == "release" { diff --git a/build_system/tests.rs b/build_system/tests.rs index 9139b3ccecc..6993fdee405 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -448,50 +448,25 @@ impl TestRunner { let jit_supported = is_native && host_triple.contains("x86_64") && !host_triple.contains("windows"); - let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); - let mut runner = vec![]; - - if !is_native { - match target_triple.as_str() { - "aarch64-unknown-linux-gnu" => { - // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. - rustflags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rustflags); - runner = vec![ - "qemu-aarch64".to_owned(), - "-L".to_owned(), - "/usr/aarch64-linux-gnu".to_owned(), - ]; - } - "s390x-unknown-linux-gnu" => { - // We are cross-compiling for s390x. Use the correct linker and run tests in qemu. - rustflags = format!("-Clinker=s390x-linux-gnu-gcc{}", rustflags); - runner = vec![ - "qemu-s390x".to_owned(), - "-L".to_owned(), - "/usr/s390x-linux-gnu".to_owned(), - ]; - } - "x86_64-pc-windows-gnu" => { - // We are cross-compiling for Windows. Run tests in wine. - runner = vec!["wine".to_owned()]; - } - _ => { - println!("Unknown non-native platform"); - } - } - } - - // FIXME fix `#[linkage = "extern_weak"]` without this - if target_triple.contains("darwin") { - rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags); - } - let host_compiler = Compiler::clif_with_triple(&dirs, host_triple); let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); - target_compiler.rustflags = rustflags.clone(); - target_compiler.rustdocflags = rustflags; - target_compiler.runner = runner; + if !is_native { + target_compiler.set_cross_linker_and_runner(); + } + if let Ok(rustflags) = env::var("RUSTFLAGS") { + target_compiler.rustflags.push(' '); + target_compiler.rustflags.push_str(&rustflags); + } + if let Ok(rustdocflags) = env::var("RUSTDOCFLAGS") { + target_compiler.rustdocflags.push(' '); + target_compiler.rustdocflags.push_str(&rustdocflags); + } + + // FIXME fix `#[linkage = "extern_weak"]` without this + if target_compiler.triple.contains("darwin") { + target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup"); + } Self { is_native, jit_supported, dirs, host_compiler, target_compiler } } diff --git a/build_system/utils.rs b/build_system/utils.rs index d244da1b2e0..77cbf9b26fd 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -47,6 +47,38 @@ impl Compiler { runner: vec![], } } + + pub(crate) fn set_cross_linker_and_runner(&mut self) { + match self.triple.as_str() { + "aarch64-unknown-linux-gnu" => { + // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. + self.rustflags += " -Clinker=aarch64-linux-gnu-gcc"; + self.rustdocflags += " -Clinker=aarch64-linux-gnu-gcc"; + self.runner = vec![ + "qemu-aarch64".to_owned(), + "-L".to_owned(), + "/usr/aarch64-linux-gnu".to_owned(), + ]; + } + "s390x-unknown-linux-gnu" => { + // We are cross-compiling for s390x. Use the correct linker and run tests in qemu. + self.rustflags += " -Clinker=s390x-linux-gnu-gcc"; + self.rustdocflags += " -Clinker=s390x-linux-gnu-gcc"; + self.runner = vec![ + "qemu-s390x".to_owned(), + "-L".to_owned(), + "/usr/s390x-linux-gnu".to_owned(), + ]; + } + "x86_64-pc-windows-gnu" => { + // We are cross-compiling for Windows. Run tests in wine. + self.runner = vec!["wine".to_owned()]; + } + _ => { + println!("Unknown non-native platform"); + } + } + } } pub(crate) struct CargoProject {