Skip to content

Commit 6b68921

Browse files
committed
Change implementation of -Z gcc-ld and lld-wrapper again
1 parent 76b0484 commit 6b68921

File tree

5 files changed

+48
-33
lines changed

5 files changed

+48
-33
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -2777,20 +2777,24 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
27772777
if let LinkerFlavor::Gcc = flavor {
27782778
match ld_impl {
27792779
LdImpl::Lld => {
2780-
let tools_path = sess.get_tools_search_paths(false);
2781-
let gcc_ld_dir = tools_path
2782-
.into_iter()
2783-
.map(|p| p.join("gcc-ld"))
2784-
.find(|p| {
2785-
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists()
2786-
})
2787-
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2788-
cmd.arg({
2789-
let mut arg = OsString::from("-B");
2790-
arg.push(gcc_ld_dir);
2791-
arg
2792-
});
2793-
cmd.arg(format!("-Wl,-rustc-lld-flavor={}", sess.target.lld_flavor.as_str()));
2780+
// Implement the "self-contained" part of -Zgcc-ld
2781+
// by adding rustc distribution directories to the tool search path.
2782+
for path in sess.get_tools_search_paths(false) {
2783+
cmd.arg({
2784+
let mut arg = OsString::from("-B");
2785+
arg.push(path.join("gcc-ld"));
2786+
arg
2787+
});
2788+
}
2789+
// Implement the "linker flavor" part of -Zgcc-ld
2790+
// by asking cc to use some kind of lld.
2791+
cmd.arg("-fuse-ld=lld");
2792+
if sess.target.lld_flavor != LldFlavor::Ld {
2793+
// Tell clang to use a non-default LLD flavor.
2794+
// Gcc doesn't understand the target option, but we currently assume
2795+
// that gcc is not used for Apple and Wasm targets (#97402).
2796+
cmd.arg(format!("--target={}", sess.target.llvm_target));
2797+
}
27942798
}
27952799
}
27962800
} else {

src/bootstrap/compile.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,9 @@ impl Step for Assemble {
12761276
compiler: build_compiler,
12771277
target: target_compiler.host,
12781278
});
1279-
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe("ld", target_compiler.host)));
1279+
for name in crate::LLD_FILE_NAMES {
1280+
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe(name, target_compiler.host)));
1281+
}
12801282
}
12811283

12821284
if builder.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) {

src/bootstrap/dist.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,11 @@ impl Step for Rustc {
423423
let gcc_lld_src_dir = src_dir.join("gcc-ld");
424424
let gcc_lld_dst_dir = dst_dir.join("gcc-ld");
425425
t!(fs::create_dir(&gcc_lld_dst_dir));
426-
let exe_name = exe("ld", compiler.host);
427-
builder.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
426+
for name in crate::LLD_FILE_NAMES {
427+
let exe_name = exe(name, compiler.host);
428+
builder
429+
.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
430+
}
428431
}
429432

430433
// Man pages

src/bootstrap/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ const LLVM_TOOLS: &[&str] = &[
186186
"opt", // used to optimize LLVM bytecode
187187
];
188188

189+
/// LLD file names for all flavors.
190+
const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
191+
189192
pub const VERSION: usize = 2;
190193

191194
/// Extra --check-cfg to add when building

src/tools/lld-wrapper/src/main.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//! make gcc/clang pass `-flavor <flavor>` as the first two arguments in the linker invocation
99
//! and since Windows does not support symbolic links for files this wrapper is used in place of a
1010
//! symbolic link. It execs `../rust-lld -flavor <flavor>` by propagating the flavor argument
11-
//! passed to the wrapper as the first two arguments. On Windows it spawns a `..\rust-lld.exe`
12-
//! child process.
11+
//! obtained from the wrapper's name as the first two arguments.
12+
//! On Windows it spawns a `..\rust-lld.exe` child process.
1313
1414
use std::fmt::Display;
1515
use std::path::{Path, PathBuf};
@@ -53,29 +53,32 @@ fn get_rust_lld_path(current_exe_path: &Path) -> PathBuf {
5353
rust_lld_path
5454
}
5555

56+
/// Extract LLD flavor name from the lld-wrapper executable name.
57+
fn get_lld_flavor(current_exe_path: &Path) -> Result<&'static str, String> {
58+
let stem = current_exe_path.file_stem();
59+
Ok(match stem.and_then(|s| s.to_str()) {
60+
Some("ld.lld") => "gnu",
61+
Some("ld64.lld") => "darwin",
62+
Some("lld-link") => "link",
63+
Some("wasm-ld") => "wasm",
64+
_ => return Err(format!("{:?}", stem)),
65+
})
66+
}
67+
5668
/// Returns the command for invoking rust-lld with the correct flavor.
57-
/// LLD only accepts the flavor argument at the first two arguments, so move it there.
69+
/// LLD only accepts the flavor argument at the first two arguments, so pass it there.
5870
///
5971
/// Exits on error.
6072
fn get_rust_lld_command(current_exe_path: &Path) -> process::Command {
6173
let rust_lld_path = get_rust_lld_path(current_exe_path);
6274
let mut command = process::Command::new(rust_lld_path);
6375

64-
let mut flavor = None;
65-
let args = env::args_os()
66-
.skip(1)
67-
.filter(|arg| match arg.to_str().and_then(|s| s.strip_prefix("-rustc-lld-flavor=")) {
68-
Some(suffix) => {
69-
flavor = Some(suffix.to_string());
70-
false
71-
}
72-
None => true,
73-
})
74-
.collect::<Vec<_>>();
76+
let flavor =
77+
get_lld_flavor(current_exe_path).unwrap_or_exit_with("executable has unexpected name");
7578

7679
command.arg("-flavor");
77-
command.arg(flavor.unwrap_or_exit_with("-rustc-lld-flavor=<flavor> is not passed"));
78-
command.args(args);
80+
command.arg(flavor);
81+
command.args(env::args_os().skip(1));
7982
command
8083
}
8184

0 commit comments

Comments
 (0)