Skip to content

Commit 1c1c6ed

Browse files
Rollup merge of #89288 - rusticstuff:lld_wrapper, r=Mark-Simulacrum
Wrapper for `-Z gcc-ld=lld` to invoke rust-lld with the correct flavor This PR adds an `lld-wrapper` tool which is installed as `ld` and `ld64` in `lib\rustlib\<host_target>\bin\gcc-ld` directory and whose sole purpose is to invoke `rust-lld` in the parent directory with the correct flavor. Lld decides which flavor to use from either the first two commandline arguments or from the name of the executable (`ld` for GNU/ld flavor, `ld64` for Darwin/Macos/ld64 flavor and so on). Symbolic links could not be used as they are not supported by rustup and on Windows. The wrapper replaces full copies of rust-lld which added some significant bloat. On UNIXish operating systems it exec rust-lld, on Windows it spawns it as a child process. Fixes #88869. r? ```@Mark-Simulacrum``` cc ```@nagisa``` ```@petrochenkov``` ```@1000teslas```
2 parents 37f17bc + 6162fc0 commit 1c1c6ed

File tree

7 files changed

+189
-13
lines changed

7 files changed

+189
-13
lines changed

Cargo.lock

+4
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,10 @@ dependencies = [
19651965
"walkdir",
19661966
]
19671967

1968+
[[package]]
1969+
name = "lld-wrapper"
1970+
version = "0.1.0"
1971+
19681972
[[package]]
19691973
name = "lock_api"
19701974
version = "0.4.1"

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ members = [
3636
"src/tools/jsondocck",
3737
"src/tools/html-checker",
3838
"src/tools/bump-stage0",
39+
"src/tools/lld-wrapper",
3940
]
4041

4142
exclude = [

src/bootstrap/compile.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1136,14 +1136,14 @@ impl Step for Assemble {
11361136
// for `-Z gcc-ld=lld`
11371137
let gcc_ld_dir = libdir_bin.join("gcc-ld");
11381138
t!(fs::create_dir(&gcc_ld_dir));
1139-
builder.copy(
1140-
&lld_install.join("bin").join(&src_exe),
1141-
&gcc_ld_dir.join(exe("ld", target_compiler.host)),
1142-
);
1143-
builder.copy(
1144-
&lld_install.join("bin").join(&src_exe),
1145-
&gcc_ld_dir.join(exe("ld64", target_compiler.host)),
1146-
);
1139+
for flavor in ["ld", "ld64"] {
1140+
let lld_wrapper_exe = builder.ensure(crate::tool::LldWrapper {
1141+
compiler: build_compiler,
1142+
target: target_compiler.host,
1143+
flavor_feature: flavor,
1144+
});
1145+
builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe(flavor, target_compiler.host)));
1146+
}
11471147
}
11481148

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

src/bootstrap/dist.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,14 @@ impl Step for Rustc {
409409
let rust_lld = exe("rust-lld", compiler.host);
410410
builder.copy(&src_dir.join(&rust_lld), &dst_dir.join(&rust_lld));
411411
// for `-Z gcc-ld=lld`
412-
let gcc_lld_dir = dst_dir.join("gcc-ld");
413-
t!(fs::create_dir(&gcc_lld_dir));
414-
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)));
412+
let gcc_lld_src_dir = src_dir.join("gcc-ld");
413+
let gcc_lld_dst_dir = dst_dir.join("gcc-ld");
414+
t!(fs::create_dir(&gcc_lld_dst_dir));
415+
for flavor in ["ld", "ld64"] {
416+
let exe_name = exe(flavor, compiler.host);
417+
builder
418+
.copy(&gcc_lld_src_dir.join(&exe_name), &gcc_lld_dst_dir.join(&exe_name));
419+
}
417420
}
418421

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

src/bootstrap/tool.rs

+32
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,38 @@ impl Step for Cargo {
664664
}
665665
}
666666

667+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
668+
pub struct LldWrapper {
669+
pub compiler: Compiler,
670+
pub target: TargetSelection,
671+
pub flavor_feature: &'static str,
672+
}
673+
674+
impl Step for LldWrapper {
675+
type Output = PathBuf;
676+
677+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
678+
run.never()
679+
}
680+
681+
fn run(self, builder: &Builder<'_>) -> PathBuf {
682+
let src_exe = builder
683+
.ensure(ToolBuild {
684+
compiler: self.compiler,
685+
target: self.target,
686+
tool: "lld-wrapper",
687+
mode: Mode::ToolStd,
688+
path: "src/tools/lld-wrapper",
689+
is_optional_tool: false,
690+
source_type: SourceType::InTree,
691+
extra_features: vec![self.flavor_feature.to_owned()],
692+
})
693+
.expect("expected to build -- essential tool");
694+
695+
src_exe
696+
}
697+
}
698+
667699
macro_rules! tool_extended {
668700
(($sel:ident, $builder:ident),
669701
$($name:ident,

src/tools/lld-wrapper/Cargo.toml

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

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

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//! Script to invoke the bundled rust-lld with the correct flavor. The flavor is selected by
2+
//! feature.
3+
//!
4+
//! lld supports multiple command line interfaces. If `-flavor <flavor>` are passed as the first
5+
//! two arguments the `<flavor>` command line interface is used to process the remaining arguments.
6+
//! If no `-flavor` argument is present the flavor is determined by the executable name.
7+
//!
8+
//! In Rust with `-Z gcc-ld=lld` we have gcc or clang invoke rust-lld. Since there is no way to
9+
//! make gcc/clang pass `-flavor <flavor>` as the first two arguments in the linker invocation
10+
//! and since Windows does not support symbolic links for files this wrapper is used in place of a
11+
//! symblic 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`
13+
//! child process.
14+
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;
28+
use std::fmt::Display;
29+
use std::path::{Path, PathBuf};
30+
use std::process;
31+
32+
trait ResultExt<T, E> {
33+
fn unwrap_or_exit_with(self, context: &str) -> T;
34+
}
35+
36+
impl<T, E> ResultExt<T, E> for Result<T, E>
37+
where
38+
E: Display,
39+
{
40+
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+
}
48+
}
49+
}
50+
51+
trait OptionExt<T> {
52+
fn unwrap_or_exit_with(self, context: &str) -> T;
53+
}
54+
55+
impl<T> OptionExt<T> for Option<T> {
56+
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+
}
64+
}
65+
}
66+
67+
/// Returns the path to rust-lld in the parent directory.
68+
///
69+
/// Exits if the parent directory cannot be determined.
70+
fn get_rust_lld_path(current_exe_path: &Path) -> PathBuf {
71+
let mut rust_lld_exe_name = "rust-lld".to_owned();
72+
rust_lld_exe_name.push_str(env::consts::EXE_SUFFIX);
73+
let mut rust_lld_path = current_exe_path
74+
.parent()
75+
.unwrap_or_exit_with("directory containing current executable could not be determined")
76+
.parent()
77+
.unwrap_or_exit_with("parent directory could not be determined")
78+
.to_owned();
79+
rust_lld_path.push(rust_lld_exe_name);
80+
rust_lld_path
81+
}
82+
83+
/// Returns the command for invoking rust-lld with the correct flavor.
84+
///
85+
/// Exits on error.
86+
fn get_rust_lld_command(current_exe_path: &Path) -> process::Command {
87+
let rust_lld_path = get_rust_lld_path(current_exe_path);
88+
let mut command = process::Command::new(rust_lld_path);
89+
command.arg("-flavor");
90+
command.arg(FLAVOR);
91+
command.args(env::args_os().skip(1));
92+
command
93+
}
94+
95+
#[cfg(unix)]
96+
fn exec_lld(mut command: process::Command) {
97+
use std::os::unix::prelude::CommandExt;
98+
Result::<(), _>::Err(command.exec()).unwrap_or_exit_with("could not exec rust-lld");
99+
unreachable!("lld-wrapper: after exec without error");
100+
}
101+
102+
#[cfg(not(unix))]
103+
fn exec_lld(mut command: process::Command) {
104+
// Windows has no exec(), spawn a child process and wait for it
105+
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+
}
118+
}
119+
120+
fn main() {
121+
let current_exe_path =
122+
env::current_exe().unwrap_or_exit_with("could not get the path of the current executable");
123+
124+
exec_lld(get_rust_lld_command(current_exe_path.as_ref()));
125+
}

0 commit comments

Comments
 (0)