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

linker: Allow MSVC to use Meson and MinGW-style libraries #123436

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 42 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_middle::middle::exported_symbols;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportKind};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
use rustc_session::search_paths::PathKind;
use rustc_session::Session;
use rustc_target::spec::{Cc, LinkOutputKind, LinkerFlavor, Lld};

Expand Down Expand Up @@ -784,6 +785,38 @@ pub struct MsvcLinker<'a> {
sess: &'a Session,
}

impl MsvcLinker<'_> {
// FIXME this duplicates rustc_metadata::find_native_static_library,
// as the Meson/MinGW suffix for import libraries can differ
fn find_native_dynamic_library(name: &str, verbatim: bool, sess: &Session) -> OsString {
let formats = if verbatim {
vec![("".into(), "".into())]
} else {
// While the official naming convention for MSVC import libraries
// is foo.lib...
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
// ... Meson follows the libfoo.dll.a convention to
// disambiguate .a for static libraries
let meson = ("lib".into(), ".dll.a".into());
// and MinGW uses .a altogether
let mingw = ("lib".into(), ".a".into());
vec![os, meson, mingw]
};

for path in sess.target_filesearch(PathKind::Native).search_paths() {
for (prefix, suffix) in &formats {
let test = path.dir.join(format!("{prefix}{name}{suffix}"));
if test.exists() {
return OsString::from(test);
}
}
}

// Allow the linker to find CRT libs itself
OsString::from(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
}
}

impl<'a> Linker for MsvcLinker<'a> {
fn cmd(&mut self) -> &mut Command {
&mut self.cmd
Expand All @@ -808,13 +841,19 @@ impl<'a> Linker for MsvcLinker<'a> {
}

fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
let path = MsvcLinker::<'a>::find_native_dynamic_library(name, verbatim, self.sess);
self.cmd.arg(path);
}

fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
// Static libraries built by MSVC are usually called foo.lib.
// However, under MinGW and build systems such as Meson, they are
// called libfoo.a
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
let suffix = if verbatim { "" } else { ".lib" };
self.cmd.arg(format!("{prefix}{name}{suffix}"));
let path = find_native_static_library(name, verbatim, self.sess);
let mut arg = OsString::from(prefix);
arg.push(path);
self.cmd.arg(arg);
}

fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
Expand Down