Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions bootstrap.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,8 @@
# Currently, the only standard options supported here are `"llvm"`, `"cranelift"` and `"gcc"`.
#rust.codegen-backends = ["llvm"]

# Indicates whether LLD will be compiled and made available in the sysroot for rustc to execute, and
# whether to set it as rustc's default linker on `x86_64-unknown-linux-gnu`. This will also only be
# when *not* building an external LLVM (so only when using `download-ci-llvm` or building LLVM from
# the in-tree source): setting `llvm-config` in the `[target.x86_64-unknown-linux-gnu]` section will
# make this default to false.
#rust.lld = false in all cases, except on `x86_64-unknown-linux-gnu` as described above, where it is true
# Indicates whether LLD will be compiled and made available in the sysroot for rustc to execute,
#rust.lld = false, except for targets that opt into LLD (see `target.default-linker-linux-override`)

# Indicates if we should override the linker used to link Rust crates during bootstrap to be LLD.
# If set to `true` or `"external"`, a global `lld` binary that has to be in $PATH
Expand Down Expand Up @@ -1067,3 +1063,17 @@
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.
# This overrides the global `rust.jemalloc` option. See that option for more info.
#jemalloc = rust.jemalloc (bool)

# The linker configuration that will *override* the default linker used for Linux
# targets in the built compiler.
#
# The following values are supported:
# - `off` => do not apply any override and use the default linker. This can be used to opt out of
# linker overrides set by bootstrap for specific targets (see below).
# - `self-contained-lld-cc` => override the default linker to be self-contained LLD (`rust-lld`)
# that is invoked through `cc`.
#
# Currently, the following targets automatically opt into the self-contained LLD linker, unless you
# pass `off`:
# - x86_64-unknown-linux-gnu
#default-linker-linux-override = "off" (for most targets)
13 changes: 11 additions & 2 deletions compiler/rustc_target/src/spec/base/linux_gnu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use crate::spec::{TargetOptions, base};
use crate::spec::{Cc, LinkerFlavor, Lld, TargetOptions, base};

pub(crate) fn opts() -> TargetOptions {
TargetOptions { env: "gnu".into(), ..base::linux::opts() }
let mut base = TargetOptions { env: "gnu".into(), ..base::linux::opts() };

// When we're asked to use the `rust-lld` linker by default, set the appropriate lld-using
// linker flavor, and self-contained linker component.
if option_env!("CFG_DEFAULT_LINKER_SELF_CONTAINED_LLD_CC").is_some() {
base.linker_flavor = LinkerFlavor::Gnu(Cc::Yes, Lld::Yes);
base.link_self_contained = crate::spec::LinkSelfContainedDefault::with_linker();
}

base
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ pub(crate) fn target() -> Target {
| SanitizerSet::THREAD;
base.supports_xray = true;

// When we're asked to use the `rust-lld` linker by default, set the appropriate lld-using
// linker flavor, and self-contained linker component.
if option_env!("CFG_USE_SELF_CONTAINED_LINKER").is_some() {
base.linker_flavor = LinkerFlavor::Gnu(Cc::Yes, Lld::Yes);
base.link_self_contained = crate::spec::LinkSelfContainedDefault::with_linker();
}

Target {
llvm_target: "x86_64-unknown-linux-gnu".into(),
metadata: TargetMetadata {
Expand Down
12 changes: 9 additions & 3 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::core::builder;
use crate::core::builder::{
Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
};
use crate::core::config::toml::target::DefaultLinuxLinkerOverride;
use crate::core::config::{
CompilerBuiltins, DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection,
};
Expand Down Expand Up @@ -1355,9 +1356,14 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
cargo.env("CFG_DEFAULT_LINKER", s);
}

// Enable rustc's env var for `rust-lld` when requested.
if builder.config.lld_enabled {
cargo.env("CFG_USE_SELF_CONTAINED_LINKER", "1");
// Enable rustc's env var to use a linker override on Linux when requested.
if let Some(linker) = target_config.map(|c| c.default_linker_linux_override) {
match linker {
DefaultLinuxLinkerOverride::Off => {}
DefaultLinuxLinkerOverride::SelfContainedLldCc => {
cargo.env("CFG_DEFAULT_LINKER_SELF_CONTAINED_LLD_CC", "1");
}
}
}

if builder.config.rust_verify_llvm_ir {
Expand Down
33 changes: 28 additions & 5 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,9 @@ mod snapshot {
};
use crate::core::builder::{Builder, Kind, StepDescription, StepMetadata};
use crate::core::config::TargetSelection;
use crate::core::config::toml::rust::with_lld_opt_in_targets;
use crate::core::config::toml::target::{
DefaultLinuxLinkerOverride, with_default_linux_linker_overrides,
};
use crate::utils::cache::Cache;
use crate::utils::helpers::get_host_target;
use crate::utils::tests::{ConfigBuilder, TestCtx};
Expand Down Expand Up @@ -782,17 +784,38 @@ mod snapshot {

#[test]
fn build_compiler_lld_opt_in() {
with_lld_opt_in_targets(vec![host_target()], || {
let ctx = TestCtx::new();
insta::assert_snapshot!(
with_default_linux_linker_overrides(
[(host_target(), DefaultLinuxLinkerOverride::SelfContainedLldCc)].into(),
|| {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx.config("build")
.path("compiler")
.render_steps(), @r"
[build] llvm <host>
[build] rustc 0 <host> -> rustc 1 <host>
[build] rustc 0 <host> -> LldWrapper 1 <host>
");
});
},
);
}

#[test]
fn build_compiler_lld_opt_in_lld_disabled() {
with_default_linux_linker_overrides(
[(host_target(), DefaultLinuxLinkerOverride::SelfContainedLldCc)].into(),
|| {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx.config("build")
.path("compiler")
.args(&["--set", "rust.lld=false"])
.render_steps(), @r"
[build] llvm <host>
[build] rustc 0 <host> -> rustc 1 <host>
");
},
);
}

#[test]
Expand Down
Loading
Loading