Skip to content

Commit 0129b01

Browse files
committed
rustc: Tweak default linker selection
This commit refactors how the path to the linker that we're going to invoke is selected. Previously all targets listed *both* a `LinkerFlavor` and a `linker` (path) option, but this meant that whenever you changed one you had to change the other. The purpose of this commit is to avoid coupling these where possible. Target specifications now only unconditionally define the *flavor* of the linker that they're using by default. If not otherwise specified each flavor now implies a particular default linker to run. As a result, this means that if you'd like to test out `ld` for example you should be able to do: rustc -Z linker-flavor=ld foo.rs whereas previously you had to do rustc -Z linker-flavor=ld -C linker=ld foo.rs This will hopefully make it a bit easier to tinker around with variants that should otherwise be well known to work, for example with LLD, `ld` on OSX, etc.
1 parent d69b248 commit 0129b01

26 files changed

+83
-118
lines changed

src/bootstrap/dist.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,10 @@ impl Step for Std {
606606
let mut src = builder.sysroot_libdir(compiler, target).to_path_buf();
607607
src.pop(); // Remove the trailing /lib folder from the sysroot_libdir
608608
cp_filtered(&src, &dst, &|path| {
609-
path.file_name().and_then(|s| s.to_str()) !=
610-
Some(build.config.rust_codegen_backends_dir.as_str())
609+
let name = path.file_name().and_then(|s| s.to_str());
610+
name != Some(build.config.rust_codegen_backends_dir.as_str()) &&
611+
name != Some("bin")
612+
611613
});
612614

613615
let mut cmd = rust_installer(builder);

src/bootstrap/native.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl Step for Llvm {
208208
cfg.define("LLVM_NATIVE_BUILD", build.llvm_out(build.build).join("build"));
209209
}
210210

211-
configure_cmake(build, target, &mut cfg);
211+
configure_cmake(build, target, &mut cfg, false);
212212

213213
// FIXME: we don't actually need to build all LLVM tools and all LLVM
214214
// libraries here, e.g. we just want a few components and a few
@@ -241,7 +241,8 @@ fn check_llvm_version(build: &Build, llvm_config: &Path) {
241241

242242
fn configure_cmake(build: &Build,
243243
target: Interned<String>,
244-
cfg: &mut cmake::Config) {
244+
cfg: &mut cmake::Config,
245+
building_dist_binaries: bool) {
245246
if build.config.ninja {
246247
cfg.generator("Ninja");
247248
}
@@ -294,8 +295,10 @@ fn configure_cmake(build: &Build,
294295
cfg.build_arg("-j").build_arg(build.jobs().to_string());
295296
cfg.define("CMAKE_C_FLAGS", build.cflags(target).join(" "));
296297
let mut cxxflags = build.cflags(target).join(" ");
297-
if build.config.llvm_static_stdcpp && !target.contains("windows") {
298-
cxxflags.push_str(" -static-libstdc++");
298+
if building_dist_binaries {
299+
if build.config.llvm_static_stdcpp && !target.contains("windows") {
300+
cxxflags.push_str(" -static-libstdc++");
301+
}
299302
}
300303
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
301304
if let Some(ar) = build.ar(target) {
@@ -350,7 +353,7 @@ impl Step for Lld {
350353
t!(fs::create_dir_all(&out_dir));
351354

352355
let mut cfg = cmake::Config::new(build.src.join("src/tools/lld"));
353-
configure_cmake(build, target, &mut cfg);
356+
configure_cmake(build, target, &mut cfg, true);
354357

355358
cfg.out_dir(&out_dir)
356359
.profile("Release")

src/librustc_back/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub enum LinkerFlavor {
5757
RustcEncodable, RustcDecodable)]
5858
pub enum LldFlavor {
5959
Wasm,
60+
Ld64,
61+
Ld,
62+
Link,
6063
}
6164

6265
impl ToJson for LinkerFlavor {
@@ -94,6 +97,9 @@ flavor_mappings! {
9497
((LinkerFlavor::Ld), "ld"),
9598
((LinkerFlavor::Msvc), "msvc"),
9699
((LinkerFlavor::Lld(LldFlavor::Wasm)), "wasm-ld"),
100+
((LinkerFlavor::Lld(LldFlavor::Ld64)), "ld64.lld"),
101+
((LinkerFlavor::Lld(LldFlavor::Ld)), "ld.lld"),
102+
((LinkerFlavor::Lld(LldFlavor::Link)), "lld-link"),
97103
}
98104

99105
#[derive(Clone, Copy, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]

src/librustc_back/target/aarch64_unknown_cloudabi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn target() -> TargetResult {
1515
let mut base = super::cloudabi_base::opts();
1616
base.max_atomic_width = Some(128);
1717
base.abi_blacklist = super::arm_base::abi_blacklist();
18-
base.linker = "aarch64-unknown-cloudabi-cc".to_string();
18+
base.linker = Some("aarch64-unknown-cloudabi-cc".to_string());
1919

2020
Ok(Target {
2121
llvm_target: "aarch64-unknown-cloudabi".to_string(),

src/librustc_back/target/armv7_unknown_cloudabi_eabihf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn target() -> TargetResult {
1717
base.max_atomic_width = Some(64);
1818
base.features = "+v7,+vfp3,+neon".to_string();
1919
base.abi_blacklist = super::arm_base::abi_blacklist();
20-
base.linker = "armv7-unknown-cloudabi-eabihf-cc".to_string();
20+
base.linker = Some("armv7-unknown-cloudabi-eabihf-cc".to_string());
2121

2222
Ok(Target {
2323
llvm_target: "armv7-unknown-cloudabi-eabihf".to_string(),

src/librustc_back/target/asmjs_unknown_emscripten.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use LinkerFlavor;
1212
use super::{LinkArgs, Target, TargetOptions};
13-
use super::emscripten_base::{cmd};
1413

1514
pub fn target() -> Result<Target, String> {
1615
let mut args = LinkArgs::new();
@@ -19,8 +18,6 @@ pub fn target() -> Result<Target, String> {
1918
"ERROR_ON_UNDEFINED_SYMBOLS=1".to_string()]);
2019

2120
let opts = TargetOptions {
22-
linker: cmd("emcc"),
23-
2421
dynamic_linking: false,
2522
executables: true,
2623
exe_suffix: ".js".to_string(),

src/librustc_back/target/emscripten_base.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/librustc_back/target/haiku_base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::default::Default;
1313

1414
pub fn opts() -> TargetOptions {
1515
TargetOptions {
16-
linker: "cc".to_string(),
1716
dynamic_linking: true,
1817
executables: true,
1918
has_rpath: false,

src/librustc_back/target/i686_unknown_cloudabi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn target() -> TargetResult {
1515
let mut base = super::cloudabi_base::opts();
1616
base.cpu = "pentium4".to_string();
1717
base.max_atomic_width = Some(64);
18-
base.linker = "i686-unknown-cloudabi-cc".to_string();
18+
base.linker = Some("i686-unknown-cloudabi-cc".to_string());
1919
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m32".to_string());
2020
base.stack_probes = true;
2121

src/librustc_back/target/l4re_base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub fn opts() -> Result<TargetOptions, String> {
7373
has_elf_tls: false,
7474
exe_allocation_crate: None,
7575
panic_strategy: PanicStrategy::Abort,
76-
linker: "ld".to_string(),
7776
pre_link_args,
7877
post_link_args,
7978
target_family: Some("unix".to_string()),

0 commit comments

Comments
 (0)