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

infer linker flavor by linker name if it's sufficiently specific #136473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 27 additions & 16 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,32 @@ impl LinkerFlavor {
}
}

fn infer_linker_hints(linker_stem: &str) -> (Option<Cc>, Option<Lld>) {
fn infer_linker_hints(linker_stem: &str) -> Result<Self, (Option<Cc>, Option<Lld>)> {
// Remove any version postfix.
let stem = linker_stem
.rsplit_once('-')
.and_then(|(lhs, rhs)| rhs.chars().all(char::is_numeric).then_some(lhs))
.unwrap_or(linker_stem);

// GCC/Clang can have an optional target prefix.
if stem == "emcc"
|| stem == "gcc"
if stem == "ld.lld" {
Ok(Self::Gnu(Cc::No, Lld::Yes))
} else if stem == "ld64.lld" {
Ok(Self::Darwin(Cc::No, Lld::Yes))
} else if stem == "lld-link" {
Ok(Self::Msvc(Lld::Yes))
} else if stem == "wasm-ld" || stem == "wasm-component-ld" {
Ok(Self::WasmLld(Cc::No))
} else if stem == "lld" || stem == "rust-lld" {
Err((Some(Cc::No), Some(Lld::Yes)))
} else if stem == "emcc" {
Ok(Self::EmCc)
} else if stem == "bpf-linker" {
Ok(Self::Bpf)
} else if stem == "llvm-bitcode-linker" {
Ok(Self::Llbc)
} else if stem == "rust-ptx-linker" {
Ok(Self::Ptx)
} else if stem == "gcc" // GCC/Clang can have an optional target prefix.
|| stem.ends_with("-gcc")
|| stem == "g++"
|| stem.ends_with("-g++")
Expand All @@ -329,19 +345,11 @@ impl LinkerFlavor {
|| stem == "clang++"
|| stem.ends_with("-clang++")
{
(Some(Cc::Yes), Some(Lld::No))
} else if stem == "wasm-ld"
|| stem.ends_with("-wasm-ld")
|| stem == "ld.lld"
|| stem == "lld"
|| stem == "rust-lld"
|| stem == "lld-link"
{
(Some(Cc::No), Some(Lld::Yes))
Err((Some(Cc::Yes), Some(Lld::No)))
} else if stem == "ld" || stem.ends_with("-ld") || stem == "link" {
(Some(Cc::No), Some(Lld::No))
Err((Some(Cc::No), Some(Lld::No)))
} else {
(None, None)
Err((None, None))
}
}

Expand All @@ -365,7 +373,10 @@ impl LinkerFlavor {
}

pub fn with_linker_hints(self, linker_stem: &str) -> LinkerFlavor {
self.with_hints(LinkerFlavor::infer_linker_hints(linker_stem))
match LinkerFlavor::infer_linker_hints(linker_stem) {
Ok(linker_flavor) => linker_flavor,
Err(hints) => self.with_hints(hints),
}
}

pub fn check_compatibility(self, cli: LinkerFlavorCli) -> Option<String> {
Expand Down
Loading