]> git.lizzy.rs Git - rust.git/commitdiff
Share cross-compilation code between building and testing
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>
Fri, 13 Jan 2023 12:32:20 +0000 (12:32 +0000)
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>
Fri, 13 Jan 2023 12:32:20 +0000 (12:32 +0000)
build_system/build_sysroot.rs
build_system/tests.rs
build_system/utils.rs

index f7bdf517976a3f4dcf0fb8a7229dbf1133d25e99..3ec00e08ef79780b0dccdaa7c2023a4b46c79879 100644 (file)
@@ -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" {
index 9139b3ccecc1cb1604b7d27ed4a466403cd4318a..6993fdee40524ac680b37e768244f057f6f8ffa5 100644 (file)
@@ -448,51 +448,26 @@ pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self {
         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![];
+        let host_compiler = Compiler::clif_with_triple(&dirs, host_triple);
 
+        let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
         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");
-                }
-            }
+            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_triple.contains("darwin") {
-            rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags);
+        if target_compiler.triple.contains("darwin") {
+            target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup");
         }
 
-        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;
-
         Self { is_native, jit_supported, dirs, host_compiler, target_compiler }
     }
 
index d244da1b2e04eafe897a38fa9369fc792d032abf..77cbf9b26fdc0e2334556122d2baf024ff482e80 100644 (file)
@@ -47,6 +47,38 @@ pub(crate) fn clif_with_triple(dirs: &Dirs, triple: String) -> 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 {