Skip to content

Commit df739a4

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

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+48-2
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,43 @@ pub struct MsvcLinker<'a> {
796796
sess: &'a Session,
797797
}
798798

799+
impl MsvcLinker<'_> {
800+
// FIXME this duplicates rustc_metadata::find_native_static_library,
801+
// as the Meson/MinGW suffix for import libraries can differ
802+
fn find_native_dynamic_library(
803+
name: &str,
804+
verbatim: bool,
805+
search_paths: &[PathBuf],
806+
sess: &Session,
807+
) -> OsString {
808+
let formats = if verbatim {
809+
vec![("".into(), "".into())]
810+
} else {
811+
// While the official naming convention for MSVC import libraries
812+
// is foo.lib...
813+
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
814+
// ... Meson follows the libfoo.dll.a convention to
815+
// disambiguate .a for static libraries
816+
let meson = ("lib".into(), ".dll.a".into());
817+
// and MinGW uses .a altogether
818+
let mingw = ("lib".into(), ".a".into());
819+
vec![os, meson, mingw]
820+
};
821+
822+
for path in search_paths {
823+
for (prefix, suffix) in &formats {
824+
let test = path.join(format!("{prefix}{name}{suffix}"));
825+
if test.exists() {
826+
return OsString::from(test);
827+
}
828+
}
829+
}
830+
831+
// Allow the linker to find CRT libs itself
832+
OsString::from(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
833+
}
834+
}
835+
799836
impl<'a> Linker for MsvcLinker<'a> {
800837
fn cmd(&mut self) -> &mut Command {
801838
&mut self.cmd
@@ -819,8 +856,17 @@ impl<'a> Linker for MsvcLinker<'a> {
819856
}
820857
}
821858

822-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _search_paths: &SearchPaths, _as_needed: bool) {
823-
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
859+
fn link_dylib_by_name(
860+
&mut self,
861+
name: &str,
862+
verbatim: bool,
863+
search_paths: &SearchPaths,
864+
_as_needed: bool,
865+
) {
866+
let search_paths = search_paths.get(self.sess);
867+
let path =
868+
MsvcLinker::<'a>::find_native_dynamic_library(name, verbatim, search_paths, self.sess);
869+
self.cmd.arg(path);
824870
}
825871

826872
fn link_staticlib_by_name(

0 commit comments

Comments
 (0)