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

Commit 893497c

Browse files
committed
Infer the path of toolchain binaries from the linker path
1 parent 037d411 commit 893497c

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

src/archive.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
220220
std::mem::drop(builder);
221221

222222
if self.update_symbols {
223+
let ranlib = crate::toolchain::get_toolchain_binary(self.config.sess, "ranlib");
224+
223225
// Run ranlib to be able to link the archive
224-
let status = std::process::Command::new("ranlib")
226+
let status = std::process::Command::new(ranlib)
225227
.arg(self.config.dst)
226228
.status()
227229
.expect("Couldn't run ranlib");

src/driver/aot.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
277277
return;
278278
}
279279

280+
let assembler = crate::toolchain::get_toolchain_binary(tcx.sess, "as");
281+
let linker = crate::toolchain::get_toolchain_binary(tcx.sess, "ld");
282+
280283
// Remove all LLVM style comments
281284
let global_asm = global_asm.lines().map(|line| {
282285
if let Some(index) = line.find("//") {
@@ -292,7 +295,7 @@ fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
292295

293296
// Assemble `global_asm`
294297
let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm");
295-
let mut child = Command::new("as")
298+
let mut child = Command::new(assembler)
296299
.arg("-o").arg(&global_asm_object_file)
297300
.stdin(Stdio::piped())
298301
.spawn()
@@ -306,7 +309,7 @@ fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
306309
// Link the global asm and main object file together
307310
let main_object_file = add_file_stem_postfix(output_object_file.clone(), ".main");
308311
std::fs::rename(&output_object_file, &main_object_file).unwrap();
309-
let status = Command::new("ld")
312+
let status = Command::new(linker)
310313
.arg("-r") // Create a new object file
311314
.arg("-o").arg(output_object_file)
312315
.arg(&main_object_file)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ mod optimize;
6464
mod pointer;
6565
mod pretty_clif;
6666
mod target_features_whitelist;
67+
mod toolchain;
6768
mod trap;
6869
mod unsize;
6970
mod value_and_place;

src/toolchain.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::path::PathBuf;
2+
3+
use rustc_middle::bug;
4+
use rustc_session::Session;
5+
use rustc_target::spec::LinkerFlavor;
6+
7+
/// Tries to infer the path of a binary for the target toolchain from the linker name.
8+
pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
9+
let (mut linker, _linker_flavor) = linker_and_flavor(sess);
10+
let linker_file_name = linker.file_name().and_then(|name| name.to_str()).unwrap_or_else(|| {
11+
sess.fatal("couldn't extract file name from specified linker")
12+
});
13+
14+
if linker_file_name == "ld.lld" {
15+
if tool != "ld" {
16+
linker.set_file_name(tool)
17+
}
18+
} else {
19+
let tool_file_name = linker_file_name
20+
.replace("ld", tool)
21+
.replace("gcc", tool)
22+
.replace("clang", tool)
23+
.replace("cc", tool);
24+
25+
linker.set_file_name(tool_file_name)
26+
}
27+
28+
linker
29+
}
30+
31+
// Adapted from https://github.com/rust-lang/rust/blob/5db778affee7c6600c8e7a177c48282dab3f6292/src/librustc_codegen_ssa/back/link.rs#L848-L931
32+
fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
33+
fn infer_from(
34+
sess: &Session,
35+
linker: Option<PathBuf>,
36+
flavor: Option<LinkerFlavor>,
37+
) -> Option<(PathBuf, LinkerFlavor)> {
38+
match (linker, flavor) {
39+
(Some(linker), Some(flavor)) => Some((linker, flavor)),
40+
// only the linker flavor is known; use the default linker for the selected flavor
41+
(None, Some(flavor)) => Some((
42+
PathBuf::from(match flavor {
43+
LinkerFlavor::Em => {
44+
if cfg!(windows) {
45+
"emcc.bat"
46+
} else {
47+
"emcc"
48+
}
49+
}
50+
LinkerFlavor::Gcc => {
51+
if cfg!(any(target_os = "solaris", target_os = "illumos")) {
52+
// On historical Solaris systems, "cc" may have
53+
// been Sun Studio, which is not flag-compatible
54+
// with "gcc". This history casts a long shadow,
55+
// and many modern illumos distributions today
56+
// ship GCC as "gcc" without also making it
57+
// available as "cc".
58+
"gcc"
59+
} else {
60+
"cc"
61+
}
62+
}
63+
LinkerFlavor::Ld => "ld",
64+
LinkerFlavor::Msvc => "link.exe",
65+
LinkerFlavor::Lld(_) => "lld",
66+
LinkerFlavor::PtxLinker => "rust-ptx-linker",
67+
}),
68+
flavor,
69+
)),
70+
(Some(linker), None) => {
71+
let stem = linker.file_stem().and_then(|stem| stem.to_str()).unwrap_or_else(|| {
72+
sess.fatal("couldn't extract file stem from specified linker")
73+
});
74+
75+
let flavor = if stem == "emcc" {
76+
LinkerFlavor::Em
77+
} else if stem == "gcc"
78+
|| stem.ends_with("-gcc")
79+
|| stem == "clang"
80+
|| stem.ends_with("-clang")
81+
{
82+
LinkerFlavor::Gcc
83+
} else if stem == "ld" || stem == "ld.lld" || stem.ends_with("-ld") {
84+
LinkerFlavor::Ld
85+
} else if stem == "link" || stem == "lld-link" {
86+
LinkerFlavor::Msvc
87+
} else if stem == "lld" || stem == "rust-lld" {
88+
LinkerFlavor::Lld(sess.target.target.options.lld_flavor)
89+
} else {
90+
// fall back to the value in the target spec
91+
sess.target.target.linker_flavor
92+
};
93+
94+
Some((linker, flavor))
95+
}
96+
(None, None) => None,
97+
}
98+
}
99+
100+
// linker and linker flavor specified via command line have precedence over what the target
101+
// specification specifies
102+
if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), sess.opts.cg.linker_flavor) {
103+
return ret;
104+
}
105+
106+
if let Some(ret) = infer_from(
107+
sess,
108+
sess.target.target.options.linker.clone().map(PathBuf::from),
109+
Some(sess.target.target.linker_flavor),
110+
) {
111+
return ret;
112+
}
113+
114+
bug!("Not enough information provided to determine how to invoke the linker");
115+
}

0 commit comments

Comments
 (0)