Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Infer linker-flavor from the selected linker #50359

Closed
wants to merge 2 commits into from
Closed
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
50 changes: 45 additions & 5 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use syntax::{ast, codemap};
use syntax::feature_gate::AttributeType;
use syntax_pos::{MultiSpan, Span};

use rustc_target::spec::{LinkerFlavor, PanicStrategy};
use rustc_target::spec::{LldFlavor, LinkerFlavor, PanicStrategy};
use rustc_target::spec::{Target, TargetTriple};
use rustc_data_structures::flock;
use jobserver::Client;
Expand Down Expand Up @@ -600,11 +600,51 @@ impl Session {
.panic
.unwrap_or(self.target.target.options.panic_strategy)
}
pub fn linker_flavor(&self) -> LinkerFlavor {

fn linker(&self) -> &str {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK there's not actually any reason these methods have to live in librustc, if you'd like feel free to delete them here and move the wholesale into librustc_trans

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this method looks mostly correct except for the last part. We'll probably want to rename this to something like linker_file_stem and then make sure you use the file_stem method of Path at the end to remove bat/exe suffixes

self.opts
.debugging_opts
.linker_flavor
.unwrap_or(self.target.target.linker_flavor)
.cg
.linker
.as_ref()
.and_then(|l| l.file_name())
.and_then(|l| l.to_str())
.unwrap_or_else(|| {
self.target.target.options.linker.as_ref().map(|l| &**l).unwrap_or_else(|| {
"cc"
})
})
}

pub fn linker_flavor(&self) -> LinkerFlavor {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change but the logic probably doesn't work correctly on Windows because of the .exe suffix.

self.opts.debugging_opts.linker_flavor.unwrap_or_else(|| {
let linker = self.linker();

if linker.ends_with("gcc") { // e.g. `arm-none-eabi-gcc`
LinkerFlavor::Gcc
} else if linker == "lld" || linker == "rustc-lld" {
let flavor = if self.target.target.target_os == "macos" {
LldFlavor::Ld64
} else if self.target.target.target_env == "msvc" {
LldFlavor::Link
} else if self.target.target.arch.starts_with("wasm") {
LldFlavor::Wasm
} else {
// most likely ELF output
LldFlavor::Ld
};

LinkerFlavor::Lld(flavor)
} else if linker.ends_with("ld") || // e.g. `arm-none-eabi-ld`
linker.starts_with("ld.") { // e.g. `ld.lld` or `ld.gold`
LinkerFlavor::Ld
} else if linker == "emcc" {
LinkerFlavor::Em
} else if linker == "link.exe" {
Copy link
Member

@retep998 retep998 May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lld-link.exe is the LLD equivalent here. Might also be worth handling both with and without the .exe suffix?

LinkerFlavor::Msvc
} else {
LinkerFlavor::Gcc
}
})
}

pub fn fewer_names(&self) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
Expand All @@ -23,7 +23,6 @@ pub fn target() -> TargetResult {
target_os: "ios".to_string(),
target_env: "".to_string(),
target_vendor: "apple".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
features: "+neon,+fp-armv8,+cyclone".to_string(),
eliminate_frame_pointer: false,
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_linux_android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

// See https://developer.android.com/ndk/guides/abis.html#arm64-v8a
// for target ABI requirements.
Expand All @@ -29,7 +29,6 @@ pub fn target() -> TargetResult {
target_os: "android".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_unknown_cloudabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetResult};
use spec::{Target, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::cloudabi_base::opts();
Expand All @@ -26,7 +26,6 @@ pub fn target() -> TargetResult {
target_os: "cloudabi".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: base,
})
}
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_unknown_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::freebsd_base::opts();
Expand All @@ -27,7 +27,6 @@ pub fn target() -> TargetResult {
target_os: "freebsd".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_unknown_fuchsia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::fuchsia_base::opts();
Expand All @@ -24,7 +24,6 @@ pub fn target() -> TargetResult {
target_os: "fuchsia".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_unknown_linux_gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::linux_base::opts();
Expand All @@ -27,7 +27,6 @@ pub fn target() -> TargetResult {
arch: "aarch64".to_string(),
target_os: "linux".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/aarch64_unknown_linux_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::linux_musl_base::opts();
Expand All @@ -27,7 +27,6 @@ pub fn target() -> TargetResult {
arch: "aarch64".to_string(),
target_os: "linux".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/arm_linux_androideabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::android_base::opts();
Expand All @@ -26,7 +26,6 @@ pub fn target() -> TargetResult {
target_os: "android".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/arm_unknown_linux_gnueabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::linux_base::opts();
Expand All @@ -23,7 +23,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "gnu".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,

options: TargetOptions {
features: "+strict-align,+v6".to_string(),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::linux_base::opts();
Expand All @@ -23,7 +23,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "gnu".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,

options: TargetOptions {
features: "+strict-align,+v6,+vfp2".to_string(),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/arm_unknown_linux_musleabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::linux_musl_base::opts();
Expand All @@ -30,7 +30,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "musl".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/arm_unknown_linux_musleabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::linux_musl_base::opts();
Expand All @@ -30,7 +30,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "musl".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let base = super::linux_base::opts();
Expand All @@ -22,7 +22,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "gnu".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,

options: TargetOptions {
features: "+soft-float,+strict-align".to_string(),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let base = super::linux_base::opts();
Expand All @@ -22,7 +22,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "gnu".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,

options: TargetOptions {
features: "+soft-float,+strict-align".to_string(),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv7_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
Expand All @@ -23,7 +23,6 @@ pub fn target() -> TargetResult {
target_os: "ios".to_string(),
target_env: "".to_string(),
target_vendor: "apple".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
features: "+v7,+vfp3,+neon".to_string(),
max_atomic_width: Some(64),
Expand Down
1 change: 0 additions & 1 deletion src/librustc_target/spec/armv7_linux_androideabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub fn target() -> TargetResult {
target_os: "android".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
abi_blacklist: super::arm_base::abi_blacklist(),
.. base
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetResult};
use spec::{Target, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::cloudabi_base::opts();
Expand All @@ -28,7 +28,6 @@ pub fn target() -> TargetResult {
target_os: "cloudabi".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: base,
})
}
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let base = super::linux_base::opts();
Expand All @@ -22,7 +22,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "gnu".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,

options: TargetOptions {
// Info about features at https://wiki.debian.org/ArmHardFloatPort
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};

pub fn target() -> TargetResult {
let base = super::linux_musl_base::opts();
Expand All @@ -25,7 +25,6 @@ pub fn target() -> TargetResult {
target_os: "linux".to_string(),
target_env: "musl".to_string(),
target_vendor: "unknown".to_string(),
linker_flavor: LinkerFlavor::Gcc,

// Most of these settings are copied from the armv7_unknown_linux_gnueabihf
// target.
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_target/spec/armv7s_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
use spec::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
Expand All @@ -23,7 +23,6 @@ pub fn target() -> TargetResult {
target_os: "ios".to_string(),
target_env: "".to_string(),
target_vendor: "apple".to_string(),
linker_flavor: LinkerFlavor::Gcc,
options: TargetOptions {
features: "+v7,+vfp4,+neon".to_string(),
max_atomic_width: Some(64),
Expand Down
Loading