Skip to content

Commit b792741

Browse files
Rollup merge of rust-lang#97375 - petrochenkov:zgccld, r=bjorn3
Simplify implementation of `-Z gcc-ld` - The logic is now unified for all targets (wasm targets should also be supported now) - Additional "symlink" files like `ld64` are eliminated - lld-wrapper is used for propagating the correct lld flavor - Cleanup "unwrap or exit" logic in lld-wrapper cc rust-lang#97352 r? `@bjorn3`
2 parents 02b630f + 2984bf6 commit b792741

File tree

8 files changed

+71
-122
lines changed

8 files changed

+71
-122
lines changed

compiler/rustc_codegen_ssa/src/back/command.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,7 @@ impl Command {
105105
}
106106
Program::Lld(ref p, flavor) => {
107107
let mut c = process::Command::new(p);
108-
c.arg("-flavor").arg(match flavor {
109-
LldFlavor::Wasm => "wasm",
110-
LldFlavor::Ld => "gnu",
111-
LldFlavor::Link => "link",
112-
LldFlavor::Ld64 => "darwin",
113-
});
108+
c.arg("-flavor").arg(flavor.as_str());
114109
if let LldFlavor::Wasm = flavor {
115110
// LLVM expects host-specific formatting for @file
116111
// arguments, but we always generate posix formatted files

compiler/rustc_codegen_ssa/src/back/link.rs

+14-31
Original file line numberDiff line numberDiff line change
@@ -2698,37 +2698,20 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
26982698
if let LinkerFlavor::Gcc = flavor {
26992699
match ld_impl {
27002700
LdImpl::Lld => {
2701-
if sess.target.lld_flavor == LldFlavor::Ld64 {
2702-
let tools_path = sess.get_tools_search_paths(false);
2703-
let ld64_exe = tools_path
2704-
.into_iter()
2705-
.map(|p| p.join("gcc-ld"))
2706-
.map(|p| {
2707-
p.join(if sess.host.is_like_windows { "ld64.exe" } else { "ld64" })
2708-
})
2709-
.find(|p| p.exists())
2710-
.unwrap_or_else(|| sess.fatal("rust-lld (as ld64) not found"));
2711-
cmd.cmd().arg({
2712-
let mut arg = OsString::from("-fuse-ld=");
2713-
arg.push(ld64_exe);
2714-
arg
2715-
});
2716-
} else {
2717-
let tools_path = sess.get_tools_search_paths(false);
2718-
let lld_path = tools_path
2719-
.into_iter()
2720-
.map(|p| p.join("gcc-ld"))
2721-
.find(|p| {
2722-
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" })
2723-
.exists()
2724-
})
2725-
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2726-
cmd.cmd().arg({
2727-
let mut arg = OsString::from("-B");
2728-
arg.push(lld_path);
2729-
arg
2730-
});
2731-
}
2701+
let tools_path = sess.get_tools_search_paths(false);
2702+
let gcc_ld_dir = tools_path
2703+
.into_iter()
2704+
.map(|p| p.join("gcc-ld"))
2705+
.find(|p| {
2706+
p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists()
2707+
})
2708+
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2709+
cmd.arg({
2710+
let mut arg = OsString::from("-B");
2711+
arg.push(gcc_ld_dir);
2712+
arg
2713+
});
2714+
cmd.arg(format!("-Wl,-rustc-lld-flavor={}", sess.target.lld_flavor.as_str()));
27322715
}
27332716
}
27342717
} else {

compiler/rustc_target/src/spec/mod.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ pub enum LldFlavor {
108108
}
109109

110110
impl LldFlavor {
111+
pub fn as_str(&self) -> &'static str {
112+
match self {
113+
LldFlavor::Wasm => "wasm",
114+
LldFlavor::Ld64 => "darwin",
115+
LldFlavor::Ld => "gnu",
116+
LldFlavor::Link => "link",
117+
}
118+
}
119+
111120
fn from_str(s: &str) -> Option<Self> {
112121
Some(match s {
113122
"darwin" => LldFlavor::Ld64,
@@ -121,13 +130,7 @@ impl LldFlavor {
121130

122131
impl ToJson for LldFlavor {
123132
fn to_json(&self) -> Json {
124-
match *self {
125-
LldFlavor::Ld64 => "darwin",
126-
LldFlavor::Ld => "gnu",
127-
LldFlavor::Link => "link",
128-
LldFlavor::Wasm => "wasm",
129-
}
130-
.to_json()
133+
self.as_str().to_json()
131134
}
132135
}
133136

src/bootstrap/compile.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1164,14 +1164,11 @@ impl Step for Assemble {
11641164
// for `-Z gcc-ld=lld`
11651165
let gcc_ld_dir = libdir_bin.join("gcc-ld");
11661166
t!(fs::create_dir(&gcc_ld_dir));
1167-
for flavor in ["ld", "ld64"] {
1168-
let lld_wrapper_exe = builder.ensure(crate::tool::LldWrapper {
1169-
compiler: build_compiler,
1170-
target: target_compiler.host,
1171-
flavor_feature: flavor,
1172-
});
1173-
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe(flavor, target_compiler.host)));
1174-
}
1167+
let lld_wrapper_exe = builder.ensure(crate::tool::LldWrapper {
1168+
compiler: build_compiler,
1169+
target: target_compiler.host,
1170+
});
1171+
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe("ld", target_compiler.host)));
11751172
}
11761173

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

src/bootstrap/dist.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,8 @@ impl Step for Rustc {
407407
let gcc_lld_src_dir = src_dir.join("gcc-ld");
408408
let gcc_lld_dst_dir = dst_dir.join("gcc-ld");
409409
t!(fs::create_dir(&gcc_lld_dst_dir));
410-
for flavor in ["ld", "ld64"] {
411-
let exe_name = exe(flavor, compiler.host);
412-
builder
413-
.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
414-
}
410+
let exe_name = exe("ld", compiler.host);
411+
builder.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
415412
}
416413

417414
// Man pages

src/bootstrap/tool.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,6 @@ impl Step for Cargo {
656656
pub struct LldWrapper {
657657
pub compiler: Compiler,
658658
pub target: TargetSelection,
659-
pub flavor_feature: &'static str,
660659
}
661660

662661
impl Step for LldWrapper {
@@ -676,7 +675,7 @@ impl Step for LldWrapper {
676675
path: "src/tools/lld-wrapper",
677676
is_optional_tool: false,
678677
source_type: SourceType::InTree,
679-
extra_features: vec![self.flavor_feature.to_owned()],
678+
extra_features: Vec::new(),
680679
})
681680
.expect("expected to build -- essential tool");
682681

src/tools/lld-wrapper/Cargo.toml

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@ name = "lld-wrapper"
33
version = "0.1.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
6-
7-
[dependencies]
8-
9-
[features]
10-
ld = []
11-
ld64 = []

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

+38-57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//! Script to invoke the bundled rust-lld with the correct flavor. The flavor is selected by
2-
//! feature.
1+
//! Script to invoke the bundled rust-lld with the correct flavor.
32
//!
43
//! lld supports multiple command line interfaces. If `-flavor <flavor>` are passed as the first
54
//! two arguments the `<flavor>` command line interface is used to process the remaining arguments.
@@ -8,59 +7,33 @@
87
//! In Rust with `-Z gcc-ld=lld` we have gcc or clang invoke rust-lld. Since there is no way to
98
//! make gcc/clang pass `-flavor <flavor>` as the first two arguments in the linker invocation
109
//! and since Windows does not support symbolic links for files this wrapper is used in place of a
11-
//! symbolic link. It execs `../rust-lld -flavor ld` if the feature `ld` is enabled and
12-
//! `../rust-lld -flavor ld64` if `ld64` is enabled. On Windows it spawns a `..\rust-lld.exe`
10+
//! 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`
1312
//! child process.
1413
15-
#[cfg(not(any(feature = "ld", feature = "ld64")))]
16-
compile_error!("One of the features ld and ld64 must be enabled.");
17-
18-
#[cfg(all(feature = "ld", feature = "ld64"))]
19-
compile_error!("Only one of the feature ld or ld64 can be enabled.");
20-
21-
#[cfg(feature = "ld")]
22-
const FLAVOR: &str = "ld";
23-
24-
#[cfg(feature = "ld64")]
25-
const FLAVOR: &str = "ld64";
26-
27-
use std::env;
2814
use std::fmt::Display;
2915
use std::path::{Path, PathBuf};
30-
use std::process;
16+
use std::{env, process};
3117

32-
trait ResultExt<T, E> {
18+
trait UnwrapOrExitWith<T> {
3319
fn unwrap_or_exit_with(self, context: &str) -> T;
3420
}
3521

36-
impl<T, E> ResultExt<T, E> for Result<T, E>
37-
where
38-
E: Display,
39-
{
22+
impl<T> UnwrapOrExitWith<T> for Option<T> {
4023
fn unwrap_or_exit_with(self, context: &str) -> T {
41-
match self {
42-
Ok(t) => t,
43-
Err(e) => {
44-
eprintln!("lld-wrapper: {}: {}", context, e);
45-
process::exit(1);
46-
}
47-
}
24+
self.unwrap_or_else(|| {
25+
eprintln!("lld-wrapper: {}", context);
26+
process::exit(1);
27+
})
4828
}
4929
}
5030

51-
trait OptionExt<T> {
52-
fn unwrap_or_exit_with(self, context: &str) -> T;
53-
}
54-
55-
impl<T> OptionExt<T> for Option<T> {
31+
impl<T, E: Display> UnwrapOrExitWith<T> for Result<T, E> {
5632
fn unwrap_or_exit_with(self, context: &str) -> T {
57-
match self {
58-
Some(t) => t,
59-
None => {
60-
eprintln!("lld-wrapper: {}", context);
61-
process::exit(1);
62-
}
63-
}
33+
self.unwrap_or_else(|err| {
34+
eprintln!("lld-wrapper: {}: {}", context, err);
35+
process::exit(1);
36+
})
6437
}
6538
}
6639

@@ -81,14 +54,28 @@ fn get_rust_lld_path(current_exe_path: &Path) -> PathBuf {
8154
}
8255

8356
/// 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.
8458
///
8559
/// Exits on error.
8660
fn get_rust_lld_command(current_exe_path: &Path) -> process::Command {
8761
let rust_lld_path = get_rust_lld_path(current_exe_path);
8862
let mut command = process::Command::new(rust_lld_path);
63+
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<_>>();
75+
8976
command.arg("-flavor");
90-
command.arg(FLAVOR);
91-
command.args(env::args_os().skip(1));
77+
command.arg(flavor.unwrap_or_exit_with("-rustc-lld-flavor=<flavor> is not passed"));
78+
command.args(args);
9279
command
9380
}
9481

@@ -101,20 +88,14 @@ fn exec_lld(mut command: process::Command) {
10188

10289
#[cfg(not(unix))]
10390
fn exec_lld(mut command: process::Command) {
104-
// Windows has no exec(), spawn a child process and wait for it
91+
// Windows has no exec(), spawn a child process and wait for it.
10592
let exit_status = command.status().unwrap_or_exit_with("error running rust-lld child process");
106-
if !exit_status.success() {
107-
match exit_status.code() {
108-
Some(code) => {
109-
// return the original lld exit code
110-
process::exit(code)
111-
}
112-
None => {
113-
eprintln!("lld-wrapper: rust-lld child process exited with error: {}", exit_status,);
114-
process::exit(1);
115-
}
116-
}
117-
}
93+
let code = exit_status
94+
.code()
95+
.ok_or(exit_status)
96+
.unwrap_or_exit_with("rust-lld child process exited with error");
97+
// Return the original lld exit code.
98+
process::exit(code);
11899
}
119100

120101
fn main() {

0 commit comments

Comments
 (0)