Skip to content

Commit b1fbafd

Browse files
ApteryksMark-Simulacrum
authored andcommitted
rustbuild: Add support for a per-target default-linker option.
1 parent a09f775 commit b1fbafd

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

config.toml.example

+6-3
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,12 @@ changelog-seen = 2
488488
# FIXME(#75760): Some UI tests fail when this option is enabled.
489489
#parallel-compiler = false
490490

491-
# The default linker that will be hard-coded into the generated compiler for
492-
# targets that don't specify linker explicitly in their target specifications.
493-
# Note that this is not the linker used to link said compiler.
491+
# The default linker that will be hard-coded into the generated
492+
# compiler for targets that don't specify a default linker explicitly
493+
# in their target specifications. Note that this is not the linker
494+
# used to link said compiler. It can also be set per-target (via the
495+
# `[target.<triple>]` block), which may be useful in a cross-compilation
496+
# setting.
494497
#
495498
# See https://doc.rust-lang.org/rustc/codegen-options/index.html#linker for more information.
496499
#default-linker = <none> (path)

src/bootstrap/compile.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
662662
.env("CFG_VERSION", builder.rust_version());
663663

664664
let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
665+
let target_config = builder.config.target_config.get(&target);
666+
665667
cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative);
666668

667669
if let Some(ref ver_date) = builder.rust_info.commit_date() {
@@ -673,9 +675,15 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
673675
if !builder.unstable_features() {
674676
cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
675677
}
676-
if let Some(ref s) = builder.config.rustc_default_linker {
678+
679+
// Prefer the current target's own default_linker, else a globally
680+
// specified one.
681+
if let Some(s) = target_config.and_then(|c| c.default_linker.as_ref()) {
682+
cargo.env("CFG_DEFAULT_LINKER", s);
683+
} else if let Some(ref s) = builder.config.rustc_default_linker {
677684
cargo.env("CFG_DEFAULT_LINKER", s);
678685
}
686+
679687
if builder.config.rustc_parallel {
680688
cargo.rustflag("--cfg=parallel_compiler");
681689
cargo.rustdocflag("--cfg=parallel_compiler");
@@ -700,7 +708,6 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
700708
}
701709
let llvm_config = builder.ensure(native::Llvm { target });
702710
cargo.env("LLVM_CONFIG", &llvm_config);
703-
let target_config = builder.config.target_config.get(&target);
704711
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
705712
cargo.env("CFG_LLVM_ROOT", s);
706713
}

src/bootstrap/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ pub struct Target {
294294
pub cxx: Option<PathBuf>,
295295
pub ar: Option<PathBuf>,
296296
pub ranlib: Option<PathBuf>,
297+
pub default_linker: Option<PathBuf>,
297298
pub linker: Option<PathBuf>,
298299
pub ndk: Option<PathBuf>,
299300
pub sanitizers: Option<bool>,
@@ -531,6 +532,7 @@ struct TomlTarget {
531532
cxx: Option<String>,
532533
ar: Option<String>,
533534
ranlib: Option<String>,
535+
default_linker: Option<PathBuf>,
534536
linker: Option<String>,
535537
llvm_config: Option<String>,
536538
llvm_filecheck: Option<String>,

0 commit comments

Comments
 (0)