Refactor rust(do)c linker flags

This commit is contained in:
Jakub Beránek 2023-12-02 23:21:23 +01:00
parent b3c9ffdc77
commit d9f9e67bc1
No known key found for this signature in database
GPG Key ID: 909CD0D26483516B
4 changed files with 51 additions and 65 deletions

View File

@ -29,8 +29,8 @@
use crate::utils::cache::{Interned, INTERNER};
use crate::utils::exec::BootstrapCommand;
use crate::utils::helpers::{
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, LldThreads,
self, add_link_lib_path, add_rustdoc_cargo_linker_args, dylib_path, dylib_path_var,
linker_args, output, t, target_supports_cranelift_backend, up_to_date, LldThreads,
};
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
use crate::{envify, CLang, DocTests, GitRepo, Mode};
@ -277,7 +277,7 @@ fn run(self, builder: &Builder<'_>) {
.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, LldThreads::No);
add_rustdoc_cargo_linker_args(&mut cmd, builder, compiler.host, LldThreads::No);
builder.run_delaying_failure(cmd);
}
}
@ -863,7 +863,7 @@ fn run(self, builder: &Builder<'_>) {
.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, LldThreads::No);
cmd.args(linker_args(builder, self.compiler.host, LldThreads::No));
builder.run_delaying_failure(&mut cmd);
}
@ -1039,7 +1039,7 @@ fn run(self, builder: &Builder<'_>) {
cmd.env("RUSTDOC", builder.rustdoc(self.compiler))
.env("RUSTC", builder.rustc(self.compiler));
add_rustdoc_cargo_lld_flags(&mut cmd, builder, self.compiler.host, LldThreads::No);
add_rustdoc_cargo_linker_args(&mut cmd, builder, self.compiler.host, LldThreads::No);
for path in &builder.paths {
if let Some(p) = helpers::is_valid_test_suite_arg(path, "tests/rustdoc-gui", builder) {
@ -1746,14 +1746,14 @@ fn run(self, builder: &Builder<'_>) {
let mut hostflags = flags.clone();
hostflags.push(format!("-Lnative={}", builder.test_helpers_out(compiler.host).display()));
hostflags.extend(builder.lld_flags(compiler.host));
hostflags.extend(linker_args(builder, compiler.host, LldThreads::No));
for flag in hostflags {
cmd.arg("--host-rustcflags").arg(flag);
}
let mut targetflags = flags;
targetflags.push(format!("-Lnative={}", builder.test_helpers_out(target).display()));
targetflags.extend(builder.lld_flags(target));
targetflags.extend(linker_args(builder, compiler.host, LldThreads::No));
for flag in targetflags {
cmd.arg("--target-rustcflags").arg(flag);
}

View File

@ -19,7 +19,9 @@
use crate::core::config::flags::{Color, Subcommand};
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};
use crate::utils::helpers::{
self, add_dylib_path, add_link_lib_path, exe, linker_args, linker_flags,
};
use crate::utils::helpers::{libdir, output, t, LldThreads};
use crate::Crate;
use crate::EXTRA_CHECK_CFGS;
@ -1174,7 +1176,7 @@ pub fn rustdoc_cmd(&self, compiler: Compiler) -> Command {
cmd.env_remove("MAKEFLAGS");
cmd.env_remove("MFLAGS");
add_rustdoc_lld_flags(&mut cmd, self, compiler.host, LldThreads::Yes);
cmd.args(linker_args(self, compiler.host, LldThreads::Yes));
cmd
}
@ -1673,21 +1675,20 @@ pub fn cargo(
rustflags.arg(&format!("-Zstack-protector={stack_protector}"));
}
if let Some(host_linker) = self.linker(compiler.host) {
hostflags.arg(format!("-Clinker={}", host_linker.display()));
}
if self.is_fuse_ld_lld(compiler.host) {
hostflags.arg("-Clink-args=-fuse-ld=lld");
}
linker_args(self, compiler.host, LldThreads::Yes).into_iter().for_each(|flag| {
hostflags.arg(flag);
});
if let Some(target_linker) = self.linker(target) {
let target = crate::envify(&target.triple);
cargo.env(&format!("CARGO_TARGET_{target}_LINKER"), target_linker);
}
if self.is_fuse_ld_lld(target) {
rustflags.arg("-Clink-args=-fuse-ld=lld");
}
self.lld_flags(target).for_each(|flag| {
// We want to set -Clinker using Cargo, therefore we only call `linker_flags` and not
// `linker_args` here.
linker_flags(self, target, LldThreads::Yes).into_iter().for_each(|flag| {
rustflags.arg(&flag);
});
linker_args(self, target, LldThreads::Yes).into_iter().for_each(|flag| {
rustdocflags.arg(&flag);
});

View File

@ -1271,21 +1271,6 @@ fn is_fuse_ld_lld(&self, target: TargetSelection) -> bool {
self.config.use_lld && !target.is_msvc()
}
fn lld_flags(&self, target: TargetSelection) -> impl Iterator<Item = String> {
let mut options = [None, None];
if self.config.use_lld {
if self.is_fuse_ld_lld(target) {
options[0] = Some("-Clink-arg=-fuse-ld=lld".to_string());
}
let no_threads = helpers::lld_flag_no_threads(target.contains("windows"));
options[1] = Some(format!("-Clink-arg=-Wl,{no_threads}"));
}
IntoIterator::into_iter(options).flatten()
}
/// Returns if this target should statically link the C runtime, if specified
fn crt_static(&self, target: TargetSelection) -> Option<bool> {
if target.contains("pc-windows-msvc") {

View File

@ -5,7 +5,7 @@
use build_helper::util::fail;
use std::env;
use std::ffi::{OsStr, OsString};
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
@ -476,22 +476,46 @@ pub enum LldThreads {
No,
}
pub fn add_rustdoc_lld_flags(
cmd: &mut Command,
/// Returns the linker arguments for rustc/rustdoc for the given builder and target.
pub fn linker_args(
builder: &Builder<'_>,
target: TargetSelection,
lld_threads: LldThreads,
) {
cmd.args(build_rustdoc_lld_flags(builder, target, lld_threads));
) -> Vec<String> {
let mut args = linker_flags(builder, target, lld_threads);
if let Some(linker) = builder.linker(target) {
args.push(format!("-Clinker={}", linker.display()));
}
pub fn add_rustdoc_cargo_lld_flags(
args
}
/// Returns the linker arguments for rustc/rustdoc for the given builder and target, without the
/// -Clinker flag.
pub fn linker_flags(
builder: &Builder<'_>,
target: TargetSelection,
lld_threads: LldThreads,
) -> Vec<String> {
let mut args = vec![];
if builder.is_fuse_ld_lld(target) {
args.push(String::from("-Clink-arg=-fuse-ld=lld"));
if matches!(lld_threads, LldThreads::No) {
args.push(format!("-Clink-arg=-Wl,{}", lld_flag_no_threads(target.is_msvc())));
}
}
args
}
pub fn add_rustdoc_cargo_linker_args(
cmd: &mut Command,
builder: &Builder<'_>,
target: TargetSelection,
lld_threads: LldThreads,
) {
let args = build_rustdoc_lld_flags(builder, target, lld_threads);
let args = linker_args(builder, target, lld_threads);
let mut flags = cmd
.get_envs()
.find_map(|(k, v)| if k == OsStr::new("RUSTDOCFLAGS") { v } else { None })
@ -507,27 +531,3 @@ pub fn add_rustdoc_cargo_lld_flags(
cmd.env("RUSTDOCFLAGS", flags);
}
}
fn build_rustdoc_lld_flags(
builder: &Builder<'_>,
target: TargetSelection,
lld_threads: LldThreads,
) -> Vec<OsString> {
let mut args = vec![];
if let Some(linker) = builder.linker(target) {
let mut flag = std::ffi::OsString::from("-Clinker=");
flag.push(linker);
args.push(flag);
}
if builder.is_fuse_ld_lld(target) {
args.push(OsString::from("-Clink-arg=-fuse-ld=lld"));
if matches!(lld_threads, LldThreads::No) {
args.push(OsString::from(format!(
"-Clink-arg=-Wl,{}",
lld_flag_no_threads(target.contains("windows"))
)));
}
}
args
}