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

Commit 70a1cb9

Browse files
committed
Pass around Compiler instead of target triples
1 parent cf22470 commit 70a1cb9

File tree

7 files changed

+64
-84
lines changed

7 files changed

+64
-84
lines changed

build_system/abi_cafe.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,28 @@ pub(crate) fn run(
1717
sysroot_kind: SysrootKind,
1818
dirs: &Dirs,
1919
cg_clif_dylib: &Path,
20-
host_triple: &str,
21-
target_triple: &str,
20+
host_compiler: &Compiler,
2221
) {
2322
if !config::get_bool("testsuite.abi-cafe") {
2423
eprintln!("[SKIP] abi-cafe");
2524
return;
2625
}
2726

28-
if host_triple != target_triple {
29-
eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
30-
return;
31-
}
32-
3327
eprintln!("Building sysroot for abi-cafe");
3428
build_sysroot::build_sysroot(
3529
dirs,
3630
channel,
3731
sysroot_kind,
3832
cg_clif_dylib,
39-
host_triple,
40-
target_triple,
33+
host_compiler,
34+
&host_compiler.triple,
4135
);
4236

4337
eprintln!("Running abi-cafe");
4438

4539
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
4640

47-
let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs);
41+
let mut cmd = ABI_CAFE.run(host_compiler, dirs);
4842
cmd.arg("--");
4943
cmd.arg("--pairs");
5044
cmd.args(pairs);

build_system/bench.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@ pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject =
2121
pub(crate) static SIMPLE_RAYTRACER: CargoProject =
2222
CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer");
2323

24-
pub(crate) fn benchmark(dirs: &Dirs) {
25-
benchmark_simple_raytracer(dirs);
24+
pub(crate) fn benchmark(dirs: &Dirs, host_compiler: &Compiler) {
25+
benchmark_simple_raytracer(dirs, host_compiler);
2626
}
2727

28-
fn benchmark_simple_raytracer(dirs: &Dirs) {
28+
fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) {
2929
if std::process::Command::new("hyperfine").output().is_err() {
3030
eprintln!("Hyperfine not installed");
3131
eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
3232
std::process::exit(1);
3333
}
3434

3535
eprintln!("[LLVM BUILD] simple-raytracer");
36-
let host_compiler = Compiler::host();
37-
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(&host_compiler, dirs);
36+
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(host_compiler, dirs);
3837
spawn_and_wait(build_cmd);
3938
fs::copy(
4039
SIMPLE_RAYTRACER_LLVM

build_system/build_backend.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
1010
pub(crate) fn build_backend(
1111
dirs: &Dirs,
1212
channel: &str,
13-
host_triple: &str,
13+
host_compiler: &Compiler,
1414
use_unstable_features: bool,
1515
) -> PathBuf {
16-
let mut cmd = CG_CLIF.build(&Compiler::host(), dirs);
16+
let mut cmd = CG_CLIF.build(&host_compiler, dirs);
1717

1818
cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode
1919

@@ -48,7 +48,7 @@ pub(crate) fn build_backend(
4848

4949
CG_CLIF
5050
.target_dir(dirs)
51-
.join(host_triple)
51+
.join(&host_compiler.triple)
5252
.join(channel)
5353
.join(get_file_name("rustc_codegen_cranelift", "dylib"))
5454
}

build_system/build_sysroot.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) fn build_sysroot(
1717
channel: &str,
1818
sysroot_kind: SysrootKind,
1919
cg_clif_dylib_src: &Path,
20-
host_triple: &str,
20+
host_compiler: &Compiler,
2121
target_triple: &str,
2222
) {
2323
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
@@ -53,7 +53,7 @@ pub(crate) fn build_sysroot(
5353

5454
let default_sysroot = super::rustc_info::get_default_sysroot();
5555

56-
let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib");
56+
let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&host_compiler.triple).join("lib");
5757
let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib");
5858
fs::create_dir_all(&host_rustlib_lib).unwrap();
5959
fs::create_dir_all(&target_rustlib_lib).unwrap();
@@ -83,7 +83,7 @@ pub(crate) fn build_sysroot(
8383
SysrootKind::None => {} // Nothing to do
8484
SysrootKind::Llvm => {
8585
for file in fs::read_dir(
86-
default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
86+
default_sysroot.join("lib").join("rustlib").join(&host_compiler.triple).join("lib"),
8787
)
8888
.unwrap()
8989
{
@@ -103,7 +103,7 @@ pub(crate) fn build_sysroot(
103103
try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
104104
}
105105

106-
if target_triple != host_triple {
106+
if target_triple != host_compiler.triple {
107107
for file in fs::read_dir(
108108
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
109109
)
@@ -115,9 +115,15 @@ pub(crate) fn build_sysroot(
115115
}
116116
}
117117
SysrootKind::Clif => {
118-
build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None);
118+
build_clif_sysroot_for_triple(
119+
dirs,
120+
channel,
121+
host_compiler.clone(),
122+
&cg_clif_dylib_path,
123+
None,
124+
);
119125

120-
if host_triple != target_triple {
126+
if host_compiler.triple != target_triple {
121127
// When cross-compiling it is often necessary to manually pick the right linker
122128
let linker = match target_triple {
123129
"aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"),
@@ -127,7 +133,11 @@ pub(crate) fn build_sysroot(
127133
build_clif_sysroot_for_triple(
128134
dirs,
129135
channel,
130-
target_triple,
136+
{
137+
let mut target_compiler = host_compiler.clone();
138+
target_compiler.triple = target_triple.to_owned();
139+
target_compiler
140+
},
131141
&cg_clif_dylib_path,
132142
linker,
133143
);
@@ -155,7 +165,7 @@ static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build
155165
fn build_clif_sysroot_for_triple(
156166
dirs: &Dirs,
157167
channel: &str,
158-
triple: &str,
168+
mut compiler: Compiler,
159169
cg_clif_dylib_path: &Path,
160170
linker: Option<&str>,
161171
) {
@@ -177,7 +187,7 @@ fn build_clif_sysroot_for_triple(
177187
}
178188
}
179189

180-
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel);
190+
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
181191

182192
if !super::config::get_bool("keep_sysroot") {
183193
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@@ -188,7 +198,7 @@ fn build_clif_sysroot_for_triple(
188198
}
189199

190200
// Build sysroot
191-
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
201+
let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
192202
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
193203
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
194204
if channel == "release" {
@@ -198,8 +208,7 @@ fn build_clif_sysroot_for_triple(
198208
use std::fmt::Write;
199209
write!(rustflags, " -Clinker={}", linker).unwrap();
200210
}
201-
let mut compiler = Compiler::with_triple(triple.to_owned());
202-
compiler.rustflags = rustflags;
211+
compiler.rustflags += &rustflags;
203212
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
204213
if channel == "release" {
205214
build_cmd.arg("--release");
@@ -219,7 +228,7 @@ fn build_clif_sysroot_for_triple(
219228
};
220229
try_hard_link(
221230
entry.path(),
222-
RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()),
231+
RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()),
223232
);
224233
}
225234
}

build_system/mod.rs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::path::PathBuf;
33
use std::process;
44

5-
use self::utils::is_ci;
5+
use self::utils::{is_ci, Compiler};
66

77
mod abi_cafe;
88
mod bench;
@@ -121,24 +121,16 @@ pub fn main() {
121121
}
122122
}
123123

124-
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
125-
host_triple
126-
} else if let Some(host_triple) = config::get_value("host") {
127-
host_triple
128-
} else {
129-
rustc_info::get_host_triple()
130-
};
131-
let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
132-
if target_triple != "" {
133-
target_triple
134-
} else {
135-
host_triple.clone() // Empty target triple can happen on GHA
136-
}
137-
} else if let Some(target_triple) = config::get_value("target") {
138-
target_triple
139-
} else {
140-
host_triple.clone()
141-
};
124+
let host_compiler = Compiler::llvm_with_triple(
125+
std::env::var("HOST_TRIPLE")
126+
.ok()
127+
.or_else(|| config::get_value("host"))
128+
.unwrap_or_else(|| rustc_info::get_host_triple()),
129+
);
130+
let target_triple = std::env::var("TARGET_TRIPLE")
131+
.ok()
132+
.or_else(|| config::get_value("target"))
133+
.unwrap_or_else(|| host_compiler.triple.clone());
142134

143135
// FIXME allow changing the location of these dirs using cli arguments
144136
let current_dir = std::env::current_dir().unwrap();
@@ -167,7 +159,7 @@ pub fn main() {
167159
}
168160

169161
let cg_clif_dylib =
170-
build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features);
162+
build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features);
171163
match command {
172164
Command::Prepare => {
173165
// Handled above
@@ -178,26 +170,24 @@ pub fn main() {
178170
channel,
179171
sysroot_kind,
180172
&cg_clif_dylib,
181-
&host_triple,
173+
&host_compiler,
182174
&target_triple,
183175
);
184176

185-
abi_cafe::run(
186-
channel,
187-
sysroot_kind,
188-
&dirs,
189-
&cg_clif_dylib,
190-
&host_triple,
191-
&target_triple,
192-
);
177+
if host_compiler.triple == target_triple {
178+
abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler);
179+
} else {
180+
eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
181+
return;
182+
}
193183
}
194184
Command::Build => {
195185
build_sysroot::build_sysroot(
196186
&dirs,
197187
channel,
198188
sysroot_kind,
199189
&cg_clif_dylib,
200-
&host_triple,
190+
&host_compiler,
201191
&target_triple,
202192
);
203193
}
@@ -207,10 +197,10 @@ pub fn main() {
207197
channel,
208198
sysroot_kind,
209199
&cg_clif_dylib,
210-
&host_triple,
200+
&host_compiler,
211201
&target_triple,
212202
);
213-
bench::benchmark(&dirs);
203+
bench::benchmark(&dirs, &host_compiler);
214204
}
215205
}
216206
}

build_system/tests.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,18 +385,19 @@ pub(crate) fn run_tests(
385385
channel: &str,
386386
sysroot_kind: SysrootKind,
387387
cg_clif_dylib: &Path,
388-
host_triple: &str,
388+
host_compiler: &Compiler,
389389
target_triple: &str,
390390
) {
391-
let runner = TestRunner::new(dirs.clone(), host_triple.to_string(), target_triple.to_string());
391+
let runner =
392+
TestRunner::new(dirs.clone(), host_compiler.triple.clone(), target_triple.to_string());
392393

393394
if config::get_bool("testsuite.no_sysroot") {
394395
build_sysroot::build_sysroot(
395396
dirs,
396397
channel,
397398
SysrootKind::None,
398399
cg_clif_dylib,
399-
&host_triple,
400+
host_compiler,
400401
&target_triple,
401402
);
402403

@@ -415,7 +416,7 @@ pub(crate) fn run_tests(
415416
channel,
416417
sysroot_kind,
417418
cg_clif_dylib,
418-
&host_triple,
419+
host_compiler,
419420
&target_triple,
420421
);
421422
}
@@ -445,7 +446,7 @@ impl TestRunner {
445446
pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self {
446447
let is_native = host_triple == target_triple;
447448
let jit_supported =
448-
target_triple.contains("x86_64") && is_native && !host_triple.contains("windows");
449+
is_native && host_triple.contains("x86_64") && !host_triple.contains("windows");
449450

450451
let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string());
451452
let mut runner = vec![];

build_system/utils.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use std::path::{Path, PathBuf};
55
use std::process::{self, Command, Stdio};
66

77
use super::path::{Dirs, RelPath};
8-
use super::rustc_info::{
9-
get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path, get_wrapper_file_name,
10-
};
8+
use super::rustc_info::{get_cargo_path, get_rustc_path, get_rustdoc_path, get_wrapper_file_name};
119

10+
#[derive(Clone, Debug)]
1211
pub(crate) struct Compiler {
1312
pub(crate) cargo: PathBuf,
1413
pub(crate) rustc: PathBuf,
@@ -20,19 +19,7 @@ pub(crate) struct Compiler {
2019
}
2120

2221
impl Compiler {
23-
pub(crate) fn host() -> Compiler {
24-
Compiler {
25-
cargo: get_cargo_path(),
26-
rustc: get_rustc_path(),
27-
rustdoc: get_rustdoc_path(),
28-
rustflags: String::new(),
29-
rustdocflags: String::new(),
30-
triple: get_host_triple(),
31-
runner: vec![],
32-
}
33-
}
34-
35-
pub(crate) fn with_triple(triple: String) -> Compiler {
22+
pub(crate) fn llvm_with_triple(triple: String) -> Compiler {
3623
Compiler {
3724
cargo: get_cargo_path(),
3825
rustc: get_rustc_path(),

0 commit comments

Comments
 (0)