Skip to content

Commit 33da95a

Browse files
committed
MsvcLinker: allow linking dynamically to Meson and MinGW-style named libraries
Fixes #122455
1 parent 2f4006c commit 33da95a

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
@@ -810,6 +810,43 @@ pub struct MsvcLinker<'a> {
810810
sess: &'a Session,
811811
}
812812

813+
impl MsvcLinker<'_> {
814+
// FIXME this duplicates rustc_metadata::find_native_static_library,
815+
// as the Meson/MinGW suffix for import libraries can differ
816+
fn find_native_dynamic_library(
817+
name: &str,
818+
verbatim: bool,
819+
search_paths: &[PathBuf],
820+
sess: &Session,
821+
) -> OsString {
822+
let formats = if verbatim {
823+
vec![("".into(), "".into())]
824+
} else {
825+
// While the official naming convention for MSVC import libraries
826+
// is foo.lib...
827+
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
828+
// ... Meson follows the libfoo.dll.a convention to
829+
// disambiguate .a for static libraries
830+
let meson = ("lib".into(), ".dll.a".into());
831+
// and MinGW uses .a altogether
832+
let mingw = ("lib".into(), ".a".into());
833+
vec![os, meson, mingw]
834+
};
835+
836+
for path in search_paths {
837+
for (prefix, suffix) in &formats {
838+
let test = path.join(format!("{prefix}{name}{suffix}"));
839+
if test.exists() {
840+
return OsString::from(test);
841+
}
842+
}
843+
}
844+
845+
// Allow the linker to find CRT libs itself
846+
OsString::from(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
847+
}
848+
}
849+
813850
impl<'a> Linker for MsvcLinker<'a> {
814851
fn cmd(&mut self) -> &mut Command {
815852
&mut self.cmd
@@ -833,8 +870,17 @@ impl<'a> Linker for MsvcLinker<'a> {
833870
}
834871
}
835872

836-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _search_paths: &SearchPaths, _as_needed: bool) {
837-
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
873+
fn link_dylib_by_name(
874+
&mut self,
875+
name: &str,
876+
verbatim: bool,
877+
search_paths: &SearchPaths,
878+
_as_needed: bool,
879+
) {
880+
let search_paths = search_paths.get(self.sess);
881+
let path =
882+
MsvcLinker::<'a>::find_native_dynamic_library(name, verbatim, search_paths, self.sess);
883+
self.cmd.arg(path);
838884
}
839885

840886
fn link_staticlib_by_name(

0 commit comments

Comments
 (0)