Skip to content

Commit ff80d0e

Browse files
committed
Generalize LLD usage in rustdoc
1 parent c035a08 commit ff80d0e

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

src/bootstrap/bin/rustdoc.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,30 @@ fn main() {
5353
arg.push(&linker);
5454
cmd.arg(arg);
5555
}
56-
if let Ok(no_threads) = env::var("RUSTDOC_LLD_NO_THREADS") {
57-
cmd.arg("-Clink-arg=-fuse-ld=lld");
58-
cmd.arg(format!("-Clink-arg=-Wl,{no_threads}"));
56+
57+
if let Ok(lld) = env::var("RUSTDOC_HOST_LLD") {
58+
match lld.as_str() {
59+
"external" => {
60+
cmd.arg("-Clink-args=-fuse-ld=lld");
61+
cmd.arg(format!(
62+
"-Clink-arg=-Wl,{}",
63+
env::var("RUSTC_LLD_NO_THREADS")
64+
.expect("RUSTC_LLD_NO_THREADS env. var missing")
65+
));
66+
}
67+
"self-contained" => {
68+
let mut arg = std::ffi::OsString::from("-Clink-arg=-B");
69+
arg.push(
70+
env::var_os("RUSTDOC_LLD_ROOT").expect("RUSTDOC_LLD_ROOT env. var missing"),
71+
);
72+
cmd.arg(arg);
73+
74+
cmd.arg("-Clink-args=-fuse-ld=lld");
75+
}
76+
_ => panic!("Unknown host lld value {lld}"),
77+
}
5978
}
79+
6080
// Cargo doesn't pass RUSTDOCFLAGS to proc_macros:
6181
// https://github.com/rust-lang/cargo/issues/4423
6282
// Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.

src/bootstrap/lib.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -1306,29 +1306,35 @@ impl Build {
13061306
};
13071307
}
13081308

1309-
// For tests we want to use only a single thread.
1309+
// For tests, we want to disable multi-threading.
13101310
// If we use an external LLD, we don't know if it's new enough to support the required
13111311
// threads flag. Therefore we invoke it to find it out.
13121312
// The self-contained lld should always be new enough.
13131313
if test {
1314-
let new_flags = if let LldMode::External = self.config.lld_mode {
1315-
util::is_lld_newer_than_10()
1316-
} else {
1317-
true
1318-
};
1319-
1320-
let flag = match (new_flags, target.contains("windows")) {
1321-
(true, true) => "/threads:1",
1322-
(true, false) => "--threads=1",
1323-
(false, true) => "/no-threads",
1324-
(false, false) => "--no-threads",
1325-
};
1314+
let flag = self.lld_single_thread_flag(target);
13261315
flags.push(format!("-Clink-arg=-Wl,{flag}"));
13271316
}
13281317

13291318
flags
13301319
}
13311320

1321+
/// Returns the LLD flag to disable multi-threading based on the given target
1322+
/// and lld version.
1323+
fn lld_single_thread_flag(&self, target: TargetSelection) -> &'static str {
1324+
let new_flags = if let LldMode::External = self.config.lld_mode {
1325+
util::is_lld_newer_than_10()
1326+
} else {
1327+
true
1328+
};
1329+
1330+
match (new_flags, target.contains("windows")) {
1331+
(true, true) => "/threads:1",
1332+
(true, false) => "--threads=1",
1333+
(false, true) => "/no-threads",
1334+
(false, false) => "--no-threads",
1335+
}
1336+
}
1337+
13321338
/// Returns if this target should statically link the C runtime, if specified
13331339
fn crt_static(&self, target: TargetSelection) -> Option<bool> {
13341340
if target.contains("pc-windows-msvc") {

src/bootstrap/test.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
1818
use crate::cache::Interned;
1919
use crate::cache::INTERNER;
2020
use crate::compile;
21-
use crate::config::TargetSelection;
21+
use crate::config::{LldMode, TargetSelection};
2222
use crate::dist;
2323
use crate::doc::DocumentationFormat;
2424
use crate::flags::Subcommand;
@@ -856,12 +856,22 @@ impl Step for RustdocTheme {
856856
if let Some(linker) = builder.linker(self.compiler.host) {
857857
cmd.env("RUSTDOC_LINKER", linker);
858858
}
859-
// if builder.is_fuse_ld_lld(self.compiler.host) {
860-
// cmd.env(
861-
// "RUSTDOC_LLD_NO_THREADS",
862-
// util::lld_flag_no_threads(self.compiler.host.contains("windows")),
863-
// );
864-
// }
859+
if !builder.is_lld_direct_linker(self.compiler.host) {
860+
match builder.config.lld_mode {
861+
LldMode::External => {
862+
cmd.env("RUSTDOC_HOST_LLD", "external");
863+
cmd.env(
864+
"RUSTDOC_LLD_NO_THREADS",
865+
builder.lld_single_thread_flag(self.compiler.host),
866+
);
867+
}
868+
LldMode::SelfContained => {
869+
cmd.env("RUSTDOC_HOST_LLD", "self-contained");
870+
cmd.env("RUSTDOC_HOST_LLD_ROOT", builder.initial_lld_root());
871+
}
872+
LldMode::Unused => {}
873+
}
874+
}
865875
builder.run_delaying_failure(&mut cmd);
866876
}
867877
}

0 commit comments

Comments
 (0)