Skip to content

Commit 07af938

Browse files
authored
Rollup merge of rust-lang#100200 - petrochenkov:zgccld2, r=lqd,Mark-Simulacrum
Change implementation of `-Z gcc-ld` and `lld-wrapper` again This PR partially reverts rust-lang#97375 and uses the strategy described in rust-lang#97402 (comment) instead, thus fixes rust-lang#97755.
2 parents 1ce0f18 + 6b68921 commit 07af938

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
@@ -2797,20 +2797,24 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
27972797
if let LinkerFlavor::Gcc = flavor {
27982798
match ld_impl {
27992799
LdImpl::Lld => {
2800-
let tools_path = sess.get_tools_search_paths(false);
2801-
let gcc_ld_dir = tools_path
2802-
.into_iter()
2803-
.map(|p| p.join("gcc-ld"))
2804-
.find(|p| {
2805-
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists()
2806-
})
2807-
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2808-
cmd.arg({
2809-
let mut arg = OsString::from("-B");
2810-
arg.push(gcc_ld_dir);
2811-
arg
2812-
});
2813-
cmd.arg(format!("-Wl,-rustc-lld-flavor={}", sess.target.lld_flavor.as_str()));
2800+
// Implement the "self-contained" part of -Zgcc-ld
2801+
// by adding rustc distribution directories to the tool search path.
2802+
for path in sess.get_tools_search_paths(false) {
2803+
cmd.arg({
2804+
let mut arg = OsString::from("-B");
2805+
arg.push(path.join("gcc-ld"));
2806+
arg
2807+
});
2808+
}
2809+
// Implement the "linker flavor" part of -Zgcc-ld
2810+
// by asking cc to use some kind of lld.
2811+
cmd.arg("-fuse-ld=lld");
2812+
if sess.target.lld_flavor != LldFlavor::Ld {
2813+
// Tell clang to use a non-default LLD flavor.
2814+
// Gcc doesn't understand the target option, but we currently assume
2815+
// that gcc is not used for Apple and Wasm targets (#97402).
2816+
cmd.arg(format!("--target={}", sess.target.llvm_target));
2817+
}
28142818
}
28152819
}
28162820
} else {

src/bootstrap/compile.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,9 @@ impl Step for Assemble {
12811281
compiler: build_compiler,
12821282
target: target_compiler.host,
12831283
});
1284-
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe("ld", target_compiler.host)));
1284+
for name in crate::LLD_FILE_NAMES {
1285+
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe(name, target_compiler.host)));
1286+
}
12851287
}
12861288

12871289
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)