Skip to content

Commit 0e6ec72

Browse files
Rollup merge of rust-lang#147626 - Kobzol:lld-generalize2, r=jieyouxu
Generalize configuring LLD as the default linker in bootstrap Reopen of rust-lang#147157, because apparently bors can't deal with it for some reason. r? ``@ghost``
2 parents 8ced599 + da86710 commit 0e6ec72

File tree

9 files changed

+263
-103
lines changed

9 files changed

+263
-103
lines changed

bootstrap.example.toml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,8 @@
758758
# Currently, the only standard options supported here are `"llvm"`, `"cranelift"` and `"gcc"`.
759759
#rust.codegen-backends = ["llvm"]
760760

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

768764
# Indicates if we should override the linker used to link Rust crates during bootstrap to be LLD.
769765
# If set to `true` or `"external"`, a global `lld` binary that has to be in $PATH
@@ -1067,3 +1063,17 @@
10671063
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.
10681064
# This overrides the global `rust.jemalloc` option. See that option for more info.
10691065
#jemalloc = rust.jemalloc (bool)
1066+
1067+
# The linker configuration that will *override* the default linker used for Linux
1068+
# targets in the built compiler.
1069+
#
1070+
# The following values are supported:
1071+
# - `off` => do not apply any override and use the default linker. This can be used to opt out of
1072+
# linker overrides set by bootstrap for specific targets (see below).
1073+
# - `self-contained-lld-cc` => override the default linker to be self-contained LLD (`rust-lld`)
1074+
# that is invoked through `cc`.
1075+
#
1076+
# Currently, the following targets automatically opt into the self-contained LLD linker, unless you
1077+
# pass `off`:
1078+
# - x86_64-unknown-linux-gnu
1079+
#default-linker-linux-override = "off" (for most targets)
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
use crate::spec::{TargetOptions, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, TargetOptions, base};
22

33
pub(crate) fn opts() -> TargetOptions {
4-
TargetOptions { env: "gnu".into(), ..base::linux::opts() }
4+
let mut base = TargetOptions { env: "gnu".into(), ..base::linux::opts() };
5+
6+
// When we're asked to use the `rust-lld` linker by default, set the appropriate lld-using
7+
// linker flavor, and self-contained linker component.
8+
if option_env!("CFG_DEFAULT_LINKER_SELF_CONTAINED_LLD_CC").is_some() {
9+
base.linker_flavor = LinkerFlavor::Gnu(Cc::Yes, Lld::Yes);
10+
base.link_self_contained = crate::spec::LinkSelfContainedDefault::with_linker();
11+
}
12+
13+
base
514
}

compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_gnu.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ pub(crate) fn target() -> Target {
2020
| SanitizerSet::THREAD;
2121
base.supports_xray = true;
2222

23-
// When we're asked to use the `rust-lld` linker by default, set the appropriate lld-using
24-
// linker flavor, and self-contained linker component.
25-
if option_env!("CFG_USE_SELF_CONTAINED_LINKER").is_some() {
26-
base.linker_flavor = LinkerFlavor::Gnu(Cc::Yes, Lld::Yes);
27-
base.link_self_contained = crate::spec::LinkSelfContainedDefault::with_linker();
28-
}
29-
3023
Target {
3124
llvm_target: "x86_64-unknown-linux-gnu".into(),
3225
metadata: TargetMetadata {

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::core::builder;
2626
use crate::core::builder::{
2727
Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
2828
};
29+
use crate::core::config::toml::target::DefaultLinuxLinkerOverride;
2930
use crate::core::config::{
3031
CompilerBuiltins, DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection,
3132
};
@@ -1373,9 +1374,14 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
13731374
cargo.env("CFG_DEFAULT_LINKER", s);
13741375
}
13751376

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

13811387
if builder.config.rust_verify_llvm_ir {

src/bootstrap/src/core/builder/tests.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,9 @@ mod snapshot {
514514
};
515515
use crate::core::builder::{Builder, Kind, StepDescription, StepMetadata};
516516
use crate::core::config::TargetSelection;
517-
use crate::core::config::toml::rust::with_lld_opt_in_targets;
517+
use crate::core::config::toml::target::{
518+
DefaultLinuxLinkerOverride, with_default_linux_linker_overrides,
519+
};
518520
use crate::utils::cache::Cache;
519521
use crate::utils::helpers::get_host_target;
520522
use crate::utils::tests::{ConfigBuilder, TestCtx};
@@ -782,17 +784,38 @@ mod snapshot {
782784

783785
#[test]
784786
fn build_compiler_lld_opt_in() {
785-
with_lld_opt_in_targets(vec![host_target()], || {
786-
let ctx = TestCtx::new();
787-
insta::assert_snapshot!(
787+
with_default_linux_linker_overrides(
788+
[(host_target(), DefaultLinuxLinkerOverride::SelfContainedLldCc)].into(),
789+
|| {
790+
let ctx = TestCtx::new();
791+
insta::assert_snapshot!(
788792
ctx.config("build")
789793
.path("compiler")
790794
.render_steps(), @r"
791795
[build] llvm <host>
792796
[build] rustc 0 <host> -> rustc 1 <host>
793797
[build] rustc 0 <host> -> LldWrapper 1 <host>
794798
");
795-
});
799+
},
800+
);
801+
}
802+
803+
#[test]
804+
fn build_compiler_lld_opt_in_lld_disabled() {
805+
with_default_linux_linker_overrides(
806+
[(host_target(), DefaultLinuxLinkerOverride::SelfContainedLldCc)].into(),
807+
|| {
808+
let ctx = TestCtx::new();
809+
insta::assert_snapshot!(
810+
ctx.config("build")
811+
.path("compiler")
812+
.args(&["--set", "rust.lld=false"])
813+
.render_steps(), @r"
814+
[build] llvm <host>
815+
[build] rustc 0 <host> -> rustc 1 <host>
816+
");
817+
},
818+
);
796819
}
797820

798821
#[test]

0 commit comments

Comments
 (0)