Skip to content

Commit 13e5771

Browse files
committed
Generalize LLD usage in rustdoc
1 parent 75d5a96 commit 13e5771

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

src/bootstrap/bin/rustdoc.rs

Lines changed: 23 additions & 3 deletions
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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,29 +1298,35 @@ impl Build {
12981298
};
12991299
}
13001300

1301-
// For tests we want to use only a single thread.
1301+
// For tests, we want to disable multi-threading.
13021302
// If we use an external LLD, we don't know if it's new enough to support the required
13031303
// threads flag. Therefore we invoke it to find it out.
13041304
// The self-contained lld should always be new enough.
13051305
if test {
1306-
let new_flags = if let LldMode::External = self.config.lld_mode {
1307-
util::is_lld_newer_than_10()
1308-
} else {
1309-
true
1310-
};
1311-
1312-
let flag = match (new_flags, target.contains("windows")) {
1313-
(true, true) => "/threads:1",
1314-
(true, false) => "--threads=1",
1315-
(false, true) => "/no-threads",
1316-
(false, false) => "--no-threads",
1317-
};
1306+
let flag = self.lld_single_thread_flag(target);
13181307
flags.push(format!("-Clink-arg=-Wl,{flag}"));
13191308
}
13201309

13211310
flags
13221311
}
13231312

1313+
/// Returns the LLD flag to disable multi-threading based on the given target
1314+
/// and lld version.
1315+
fn lld_single_thread_flag(&self, target: TargetSelection) -> &'static str {
1316+
let new_flags = if let LldMode::External = self.config.lld_mode {
1317+
util::is_lld_newer_than_10()
1318+
} else {
1319+
true
1320+
};
1321+
1322+
match (new_flags, target.contains("windows")) {
1323+
(true, true) => "/threads:1",
1324+
(true, false) => "--threads=1",
1325+
(false, true) => "/no-threads",
1326+
(false, false) => "--no-threads",
1327+
}
1328+
}
1329+
13241330
/// Returns if this target should statically link the C runtime, if specified
13251331
fn crt_static(&self, target: TargetSelection) -> Option<bool> {
13261332
if target.contains("pc-windows-msvc") {

src/bootstrap/test.rs

Lines changed: 17 additions & 7 deletions
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)