about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAfonso Bordado <afonsobordado@az8.co>2022-07-30 13:06:37 +0100
committerAfonso Bordado <afonsobordado@az8.co>2022-07-30 13:06:37 +0100
commit437b441ff5e9fd5e9d837db1cb03529344b41db1 (patch)
tree2f3cf63cfb390479dd744ae3393e0e27a2328e1f
parentd0599350a742dfee6f1ff9e60ac91fc2b673a972 (diff)
downloadrust-437b441ff5e9fd5e9d837db1cb03529344b41db1.tar.gz
rust-437b441ff5e9fd5e9d837db1cb03529344b41db1.zip
Use get_file_name everywhere for better cross compilation support
-rw-r--r--build_system/build_sysroot.rs11
-rw-r--r--build_system/mod.rs61
-rw-r--r--build_system/prepare.rs6
-rw-r--r--build_system/rustc_info.rs4
4 files changed, 42 insertions, 40 deletions
diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs
index dddd5a673c5..14e82a77f86 100644
--- a/build_system/build_sysroot.rs
+++ b/build_system/build_sysroot.rs
@@ -23,7 +23,7 @@ pub(crate) fn build_sysroot(
     fs::create_dir_all(target_dir.join("lib")).unwrap();
 
     // Copy the backend
-    let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
+    let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib", target_triple);
     let cg_clif_dylib_path = target_dir
         .join(if cfg!(windows) {
             // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
@@ -37,11 +37,10 @@ pub(crate) fn build_sysroot(
 
     // Build and copy rustc and cargo wrappers
     for wrapper in ["rustc-clif", "cargo-clif"] {
-        let wrapper_name = if cfg!(windows) {
-            format!("{wrapper}.exe")
-        } else {
-            wrapper.to_string()
-        };
+        let crate_name = wrapper.replace('-', "_");
+        let wrapper_name = get_file_name(&crate_name, "bin", target_triple);
+        let wrapper_name = wrapper_name.replace('_', "-");
+
 
         let mut build_cargo_wrapper_cmd = Command::new("rustc");
         build_cargo_wrapper_cmd
diff --git a/build_system/mod.rs b/build_system/mod.rs
index af75cef04e0..0124d47de75 100644
--- a/build_system/mod.rs
+++ b/build_system/mod.rs
@@ -48,13 +48,42 @@ pub fn main() {
     // The target dir is expected in the default location. Guard against the user changing it.
     env::set_var("CARGO_TARGET_DIR", "target");
 
+
+    let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
+        host_triple
+    } else if let Some(host_triple) = config::get_value("host") {
+        host_triple
+    } else {
+        rustc_info::get_host_triple()
+    };
+    let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
+        if target_triple != "" {
+            target_triple
+        } else {
+            host_triple.clone() // Empty target triple can happen on GHA
+        }
+    } else if let Some(target_triple) = config::get_value("target") {
+        target_triple
+    } else {
+        host_triple.clone()
+    };
+
+    if target_triple.ends_with("-msvc") {
+        eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
+        eprintln!("Switch to the MinGW toolchain for Windows support.");
+        eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
+        eprintln!("set the global default target to MinGW");
+        // process::exit(1);
+    }
+
+
     let mut args = env::args().skip(1);
     let command = match args.next().as_deref() {
         Some("prepare") => {
             if args.next().is_some() {
-                arg_error!("./x.rs prepare doesn't expect arguments");
+                arg_error!("./y.rs prepare doesn't expect arguments");
             }
-            prepare::prepare();
+            prepare::prepare(&target_triple);
             process::exit(0);
         }
         Some("build") => Command::Build,
@@ -95,35 +124,7 @@ pub fn main() {
     }
     target_dir = std::env::current_dir().unwrap().join(target_dir);
 
-    let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
-        host_triple
-    } else if let Some(host_triple) = config::get_value("host") {
-        host_triple
-    } else {
-        rustc_info::get_host_triple()
-    };
-    let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
-        if target_triple != "" {
-            target_triple
-        } else {
-            host_triple.clone() // Empty target triple can happen on GHA
-        }
-    } else if let Some(target_triple) = config::get_value("target") {
-        target_triple
-    } else {
-        host_triple.clone()
-    };
-
-    if target_triple.ends_with("-msvc") {
-        eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
-        eprintln!("Switch to the MinGW toolchain for Windows support.");
-        eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
-        eprintln!("set the global default target to MinGW");
-        process::exit(1);
-    }
-
     let cg_clif_build_dir = build_backend::build_backend(channel, &host_triple, use_unstable_features);
-
     match command {
         Command::Test => {
             tests::run_tests(
diff --git a/build_system/prepare.rs b/build_system/prepare.rs
index 7e0fd182d98..4580c4b6549 100644
--- a/build_system/prepare.rs
+++ b/build_system/prepare.rs
@@ -8,7 +8,7 @@ use std::process::Command;
 use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
 use super::utils::{copy_dir_recursively, spawn_and_wait};
 
-pub(crate) fn prepare() {
+pub(crate) fn prepare(target_triple: &str) {
     prepare_sysroot();
 
     eprintln!("[INSTALL] hyperfine");
@@ -49,8 +49,8 @@ pub(crate) fn prepare() {
     build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
     spawn_and_wait(build_cmd);
     fs::copy(
-        Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
-        Path::new("simple-raytracer").join(get_file_name("raytracer_cg_llvm", "bin")),
+        Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin", target_triple)),
+        Path::new("simple-raytracer").join(get_file_name("raytracer_cg_llvm", "bin", target_triple)),
     )
     .unwrap();
 }
diff --git a/build_system/rustc_info.rs b/build_system/rustc_info.rs
index 9206bb02bd3..f9689eb2a8c 100644
--- a/build_system/rustc_info.rs
+++ b/build_system/rustc_info.rs
@@ -43,7 +43,7 @@ pub(crate) fn get_default_sysroot() -> PathBuf {
     Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
 }
 
-pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
+pub(crate) fn get_file_name(crate_name: &str, crate_type: &str, target: &str) -> String {
     let file_name = Command::new("rustc")
         .stderr(Stdio::inherit())
         .args(&[
@@ -51,6 +51,8 @@ pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
             crate_name,
             "--crate-type",
             crate_type,
+            "--target",
+            target,
             "--print",
             "file-names",
             "-",