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

Commit 437b441

Browse files
committed
Use get_file_name everywhere for better cross compilation support
1 parent d059935 commit 437b441

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

build_system/build_sysroot.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) fn build_sysroot(
2323
fs::create_dir_all(target_dir.join("lib")).unwrap();
2424

2525
// Copy the backend
26-
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
26+
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib", target_triple);
2727
let cg_clif_dylib_path = target_dir
2828
.join(if cfg!(windows) {
2929
// 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(
3737

3838
// Build and copy rustc and cargo wrappers
3939
for wrapper in ["rustc-clif", "cargo-clif"] {
40-
let wrapper_name = if cfg!(windows) {
41-
format!("{wrapper}.exe")
42-
} else {
43-
wrapper.to_string()
44-
};
40+
let crate_name = wrapper.replace('-', "_");
41+
let wrapper_name = get_file_name(&crate_name, "bin", target_triple);
42+
let wrapper_name = wrapper_name.replace('_', "-");
43+
4544

4645
let mut build_cargo_wrapper_cmd = Command::new("rustc");
4746
build_cargo_wrapper_cmd

build_system/mod.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,42 @@ pub fn main() {
4848
// The target dir is expected in the default location. Guard against the user changing it.
4949
env::set_var("CARGO_TARGET_DIR", "target");
5050

51+
52+
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
53+
host_triple
54+
} else if let Some(host_triple) = config::get_value("host") {
55+
host_triple
56+
} else {
57+
rustc_info::get_host_triple()
58+
};
59+
let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
60+
if target_triple != "" {
61+
target_triple
62+
} else {
63+
host_triple.clone() // Empty target triple can happen on GHA
64+
}
65+
} else if let Some(target_triple) = config::get_value("target") {
66+
target_triple
67+
} else {
68+
host_triple.clone()
69+
};
70+
71+
if target_triple.ends_with("-msvc") {
72+
eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
73+
eprintln!("Switch to the MinGW toolchain for Windows support.");
74+
eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
75+
eprintln!("set the global default target to MinGW");
76+
// process::exit(1);
77+
}
78+
79+
5180
let mut args = env::args().skip(1);
5281
let command = match args.next().as_deref() {
5382
Some("prepare") => {
5483
if args.next().is_some() {
55-
arg_error!("./x.rs prepare doesn't expect arguments");
84+
arg_error!("./y.rs prepare doesn't expect arguments");
5685
}
57-
prepare::prepare();
86+
prepare::prepare(&target_triple);
5887
process::exit(0);
5988
}
6089
Some("build") => Command::Build,
@@ -95,35 +124,7 @@ pub fn main() {
95124
}
96125
target_dir = std::env::current_dir().unwrap().join(target_dir);
97126

98-
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
99-
host_triple
100-
} else if let Some(host_triple) = config::get_value("host") {
101-
host_triple
102-
} else {
103-
rustc_info::get_host_triple()
104-
};
105-
let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
106-
if target_triple != "" {
107-
target_triple
108-
} else {
109-
host_triple.clone() // Empty target triple can happen on GHA
110-
}
111-
} else if let Some(target_triple) = config::get_value("target") {
112-
target_triple
113-
} else {
114-
host_triple.clone()
115-
};
116-
117-
if target_triple.ends_with("-msvc") {
118-
eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
119-
eprintln!("Switch to the MinGW toolchain for Windows support.");
120-
eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
121-
eprintln!("set the global default target to MinGW");
122-
process::exit(1);
123-
}
124-
125127
let cg_clif_build_dir = build_backend::build_backend(channel, &host_triple, use_unstable_features);
126-
127128
match command {
128129
Command::Test => {
129130
tests::run_tests(

build_system/prepare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::process::Command;
88
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
99
use super::utils::{copy_dir_recursively, spawn_and_wait};
1010

11-
pub(crate) fn prepare() {
11+
pub(crate) fn prepare(target_triple: &str) {
1212
prepare_sysroot();
1313

1414
eprintln!("[INSTALL] hyperfine");
@@ -49,8 +49,8 @@ pub(crate) fn prepare() {
4949
build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
5050
spawn_and_wait(build_cmd);
5151
fs::copy(
52-
Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
53-
Path::new("simple-raytracer").join(get_file_name("raytracer_cg_llvm", "bin")),
52+
Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin", target_triple)),
53+
Path::new("simple-raytracer").join(get_file_name("raytracer_cg_llvm", "bin", target_triple)),
5454
)
5555
.unwrap();
5656
}

build_system/rustc_info.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ pub(crate) fn get_default_sysroot() -> PathBuf {
4343
Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
4444
}
4545

46-
pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
46+
pub(crate) fn get_file_name(crate_name: &str, crate_type: &str, target: &str) -> String {
4747
let file_name = Command::new("rustc")
4848
.stderr(Stdio::inherit())
4949
.args(&[
5050
"--crate-name",
5151
crate_name,
5252
"--crate-type",
5353
crate_type,
54+
"--target",
55+
target,
5456
"--print",
5557
"file-names",
5658
"-",

0 commit comments

Comments
 (0)