Skip to content

Commit

Permalink
Refactor rust(do)c linker flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Dec 10, 2023
1 parent b3c9ffd commit d9f9e67
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 65 deletions.
14 changes: 7 additions & 7 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_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};
Expand Down Expand Up @@ -277,7 +277,7 @@ impl Step for Cargotest {
.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);
}
}
Expand Down Expand Up @@ -863,7 +863,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, LldThreads::No);
cmd.args(linker_args(builder, self.compiler.host, LldThreads::No));

builder.run_delaying_failure(&mut cmd);
}
Expand Down Expand Up @@ -1039,7 +1039,7 @@ 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, 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) {
Expand Down Expand Up @@ -1746,14 +1746,14 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the

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);
}
Expand Down
25 changes: 13 additions & 12 deletions src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use crate::core::build_steps::{check, clean, compile, dist, doc, install, run, s
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;
Expand Down Expand Up @@ -1174,7 +1176,7 @@ impl<'a> Builder<'a> {
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
}

Expand Down Expand Up @@ -1673,21 +1675,20 @@ impl<'a> Builder<'a> {
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);
});

Expand Down
15 changes: 0 additions & 15 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,21 +1271,6 @@ impl Build {
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") {
Expand Down
62 changes: 31 additions & 31 deletions src/bootstrap/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()));
}

args
}

pub fn add_rustdoc_cargo_lld_flags(
/// 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 })
Expand All @@ -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
}

0 comments on commit d9f9e67

Please sign in to comment.