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

Merged
merged 1 commit into from
Feb 21, 2025
Merged
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
20 changes: 12 additions & 8 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,16 @@ 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"
if stem == "llvm-bitcode-linker" {
Ok(Self::Llbc)
} else if stem == "emcc" // GCC/Clang can have an optional target prefix.
|| stem == "gcc"
|| stem.ends_with("-gcc")
|| stem == "g++"
Expand All @@ -321,19 +322,19 @@ impl LinkerFlavor {
|| stem == "clang++"
|| stem.ends_with("-clang++")
{
(Some(Cc::Yes), Some(Lld::No))
Err((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::No), Some(Lld::Yes)))
} 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 @@ -357,7 +358,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