Skip to content

Commit

Permalink
Add LLD flags to rustdoc cargo invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Nov 12, 2023
1 parent e1763c8 commit fa86c78
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
31 changes: 22 additions & 9 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::utils;
use crate::utils::cache::{Interned, INTERNER};
use crate::utils::exec::BootstrapCommand;
use crate::utils::helpers::{
self, add_link_lib_path, add_rustdoc_lld_flags, dylib_path, dylib_path_var, output, t,
target_supports_cranelift_backend, up_to_date,
self, add_link_lib_path, add_rustdoc_cargo_lld_flags, add_rustdoc_lld_flags, dylib_path,
dylib_path_var, output, t, target_supports_cranelift_backend, up_to_date, LldThreadMode,
};
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
use crate::{envify, CLang, DocTests, GitRepo, Mode};
Expand Down Expand Up @@ -271,13 +271,19 @@ impl Step for Cargotest {

let _time = helpers::timeit(&builder);
let mut cmd = builder.tool_cmd(Tool::CargoTest);
builder.run_delaying_failure(
cmd.arg(&cargo)
.arg(&out_dir)
.args(builder.config.test_args())
.env("RUSTC", builder.rustc(compiler))
.env("RUSTDOC", builder.rustdoc(compiler)),
let mut cmd = cmd
.arg(&cargo)
.arg(&out_dir)
.args(builder.config.test_args())
.env("RUSTC", builder.rustc(compiler))
.env("RUSTDOC", builder.rustdoc(compiler));
add_rustdoc_cargo_lld_flags(
&mut cmd,
builder,
compiler.host,
LldThreadMode::SingleThreaded,
);
builder.run_delaying_failure(cmd);
}
}

Expand Down Expand Up @@ -862,7 +868,7 @@ impl Step for RustdocTheme {
.env("CFG_RELEASE_CHANNEL", &builder.config.channel)
.env("RUSTDOC_REAL", builder.rustdoc(self.compiler))
.env("RUSTC_BOOTSTRAP", "1");
add_rustdoc_lld_flags(&mut cmd, builder, self.compiler.host, true);
add_rustdoc_lld_flags(&mut cmd, builder, self.compiler.host, LldThreadMode::SingleThreaded);

builder.run_delaying_failure(&mut cmd);
}
Expand Down Expand Up @@ -1037,6 +1043,13 @@ impl Step for RustdocGUI {
cmd.env("RUSTDOC", builder.rustdoc(self.compiler))
.env("RUSTC", builder.rustc(self.compiler));

add_rustdoc_cargo_lld_flags(
&mut cmd,
builder,
self.compiler.host,
LldThreadMode::SingleThreaded,
);

for path in &builder.paths {
if let Some(p) = helpers::is_valid_test_suite_arg(path, "tests/rustdoc-gui", builder) {
if !p.ends_with(".goml") {
Expand Down
3 changes: 2 additions & 1 deletion src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::core::config::{DryRun, SplitDebuginfo, TargetSelection};
use crate::utils::cache::{Cache, Interned, INTERNER};
use crate::utils::helpers::{
self, add_dylib_path, add_link_lib_path, add_rustdoc_lld_flags, exe, libdir, output, t,
LldThreadMode,
};
use crate::Crate;
use crate::EXTRA_CHECK_CFGS;
Expand Down Expand Up @@ -1175,7 +1176,7 @@ impl<'a> Builder<'a> {
cmd.env_remove("MAKEFLAGS");
cmd.env_remove("MFLAGS");

add_rustdoc_lld_flags(&mut cmd, self, compiler.host, false);
add_rustdoc_lld_flags(&mut cmd, self, compiler.host, LldThreadMode::MultiThreaded);
cmd
}

Expand Down
55 changes: 49 additions & 6 deletions src/bootstrap/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use build_helper::util::fail;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -471,21 +472,63 @@ pub fn extract_beta_rev(version: &str) -> Option<String> {
count
}

pub enum LldThreadMode {
SingleThreaded,
MultiThreaded,
}

pub fn add_rustdoc_lld_flags(
cmd: &mut Command,
builder: &Builder,
builder: &Builder<'_>,
target: TargetSelection,
lld_thread_mode: LldThreadMode,
) {
let args = build_rustdoc_lld_flags(builder, target, lld_thread_mode);
cmd.args(args);
}

pub fn add_rustdoc_cargo_lld_flags(
cmd: &mut Command,
builder: &Builder<'_>,
target: TargetSelection,
single_threaded: bool,
lld_thread_mode: LldThreadMode,
) {
let args = build_rustdoc_lld_flags(builder, target, lld_thread_mode);
let mut flags = cmd
.get_envs()
.find(|(k, _)| *k == OsStr::new("RUSTDOCFLAGS"))
.and_then(|(_, v)| v)
.unwrap_or(OsStr::new(""))
.to_os_string();
for arg in args {
if !flags.is_empty() {
flags.push(" ");
}
flags.push(arg);
}
cmd.env("RUSTDOCFLAGS", flags);
}

fn build_rustdoc_lld_flags(
builder: &Builder<'_>,
target: TargetSelection,
lld_thread_mode: LldThreadMode,
) -> Vec<OsString> {
let mut args = vec![];

if let Some(linker) = builder.linker(target) {
let mut flag = std::ffi::OsString::from("-Clinker=");
flag.push(linker);
cmd.arg(flag);
args.push(flag);
}
if builder.is_fuse_ld_lld(target) {
cmd.arg("-Clink-arg=-fuse-ld=lld");
if single_threaded {
cmd.arg(format!("-Clink-arg=-Wl,{}", lld_flag_no_threads(target.contains("windows"))));
args.push(OsString::from("-Clink-arg=-fuse-ld=lld"));
if matches!(lld_thread_mode, LldThreadMode::SingleThreaded) {
args.push(OsString::from(format!(
"-Clink-arg=-Wl,{}",
lld_flag_no_threads(target.contains("windows"))
)));
}
}
args
}

0 comments on commit fa86c78

Please sign in to comment.