Skip to content

Commit c44e94f

Browse files
committed
MsvcLinker: allow linking dynamically to Meson and MinGW-style named libraries
Fixes #122455
1 parent 291a31d commit c44e94f

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,38 @@ pub struct MsvcLinker<'a> {
784784
sess: &'a Session,
785785
}
786786

787+
impl MsvcLinker<'_> {
788+
// FIXME this duplicates rustc_metadata::find_native_static_library,
789+
// as the Meson/MinGW suffix for import libraries can differ
790+
fn find_native_dynamic_library(name: &str, verbatim: bool, sess: &Session) -> OsString {
791+
let formats = if verbatim {
792+
vec![("".into(), "".into())]
793+
} else {
794+
// While the official naming convention for MSVC import libraries
795+
// is foo.lib...
796+
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
797+
// ... Meson follows the libfoo.dll.a convention to
798+
// disambiguate .a for static libraries
799+
let meson = ("lib".into(), ".dll.a".into());
800+
// and MinGW uses .a altogether
801+
let mingw = ("lib".into(), ".a".into());
802+
vec![os, meson, mingw]
803+
};
804+
805+
for path in sess.target_filesearch(PathKind::Native).search_paths() {
806+
for (prefix, suffix) in &formats {
807+
let test = path.join(format!("{prefix}{name}{suffix}"));
808+
if test.exists() {
809+
return OsString::from(test);
810+
}
811+
}
812+
}
813+
814+
// Allow the linker to find CRT libs itself
815+
OsString::from(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
816+
}
817+
}
818+
787819
impl<'a> Linker for MsvcLinker<'a> {
788820
fn cmd(&mut self) -> &mut Command {
789821
&mut self.cmd
@@ -808,7 +840,8 @@ impl<'a> Linker for MsvcLinker<'a> {
808840
}
809841

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

814847
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {

0 commit comments

Comments
 (0)