Skip to content

Commit 93415ff

Browse files
authored
Rollup merge of rust-lang#88250 - rusticstuff:macos-lld, r=nagisa
Make `-Z gcc-ld=lld` work for Apple targets `-Z gcc-ld=lld` was introduced in rust-lang#85961. It does not work on Macos because lld needs be either named `ld64` or passed `-flavor darwin` as the first two arguments in order to select the Mach-O flavor. Rust invokes cc (=clang) on Macos for linking which calls `ld` as linker binary and not `ld64`, so just creating an `ld64` binary and modifying the search path with `-B` does not work. In order to solve this patch does: * Set the `lld_flavor` for all Apple-derived targets to `LldFlavor::Ld64`. As far as I can see this actually works towards fixing `-Xlinker=rust-lld` as all those targets use the Mach-O object format. * Copy/hardlink rust-lld to the gcc-ld subdirectory as ld64 next to ld. * If `-Z gcc-ld=lld` is used and the target lld flavor is Ld64 add `-fuse-ld=/path/to/ld64` to the linker invocation. Fixes rust-lang#86945.
2 parents 9920b30 + 0f7702e commit 93415ff

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+33-14
Original file line numberDiff line numberDiff line change
@@ -2516,20 +2516,39 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
25162516
if let LinkerFlavor::Gcc = flavor {
25172517
match ld_impl {
25182518
LdImpl::Lld => {
2519-
let tools_path =
2520-
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
2521-
let lld_path = tools_path
2522-
.into_iter()
2523-
.map(|p| p.join("gcc-ld"))
2524-
.find(|p| {
2525-
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists()
2526-
})
2527-
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2528-
cmd.cmd().arg({
2529-
let mut arg = OsString::from("-B");
2530-
arg.push(lld_path);
2531-
arg
2532-
});
2519+
if sess.target.lld_flavor == LldFlavor::Ld64 {
2520+
let tools_path =
2521+
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
2522+
let ld64_exe = tools_path
2523+
.into_iter()
2524+
.map(|p| p.join("gcc-ld"))
2525+
.map(|p| {
2526+
p.join(if sess.host.is_like_windows { "ld64.exe" } else { "ld64" })
2527+
})
2528+
.find(|p| p.exists())
2529+
.unwrap_or_else(|| sess.fatal("rust-lld (as ld64) not found"));
2530+
cmd.cmd().arg({
2531+
let mut arg = OsString::from("-fuse-ld=");
2532+
arg.push(ld64_exe);
2533+
arg
2534+
});
2535+
} else {
2536+
let tools_path =
2537+
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
2538+
let lld_path = tools_path
2539+
.into_iter()
2540+
.map(|p| p.join("gcc-ld"))
2541+
.find(|p| {
2542+
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" })
2543+
.exists()
2544+
})
2545+
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2546+
cmd.cmd().arg({
2547+
let mut arg = OsString::from("-B");
2548+
arg.push(lld_path);
2549+
arg
2550+
});
2551+
}
25332552
}
25342553
}
25352554
} else {

compiler/rustc_target/src/spec/apple_base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::env;
22

3-
use crate::spec::{FramePointer, SplitDebuginfo, TargetOptions};
3+
use crate::spec::{FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
44

55
pub fn opts(os: &str) -> TargetOptions {
66
// ELF TLS is only available in macOS 10.7+. If you try to compile for 10.6
@@ -35,6 +35,7 @@ pub fn opts(os: &str) -> TargetOptions {
3535
abi_return_struct_as_int: true,
3636
emit_debug_gdb_scripts: false,
3737
eh_frame_header: false,
38+
lld_flavor: LldFlavor::Ld64,
3839

3940
// The historical default for macOS targets is to run `dsymutil` which
4041
// generates a packed version of debuginfo split from the main file.

src/bootstrap/compile.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,10 @@ impl Step for Assemble {
11211121
&lld_install.join("bin").join(&src_exe),
11221122
&gcc_ld_dir.join(exe("ld", target_compiler.host)),
11231123
);
1124+
builder.copy(
1125+
&lld_install.join("bin").join(&src_exe),
1126+
&gcc_ld_dir.join(exe("ld64", target_compiler.host)),
1127+
);
11241128
}
11251129

11261130
// Similarly, copy `llvm-dwp` into libdir for Split DWARF. Only copy it when the LLVM

src/bootstrap/dist.rs

+2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ impl Step for Rustc {
412412
let gcc_lld_dir = dst_dir.join("gcc-ld");
413413
t!(fs::create_dir(&gcc_lld_dir));
414414
builder.copy(&src_dir.join(&rust_lld), &gcc_lld_dir.join(exe("ld", compiler.host)));
415+
builder
416+
.copy(&src_dir.join(&rust_lld), &gcc_lld_dir.join(exe("ld64", compiler.host)));
415417
}
416418

417419
// Copy over llvm-dwp if it's there

0 commit comments

Comments
 (0)