@@ -17,6 +17,7 @@ use rustc_middle::middle::exported_symbols;
17
17
use rustc_middle:: middle:: exported_symbols:: { ExportedSymbol , SymbolExportInfo , SymbolExportKind } ;
18
18
use rustc_middle:: ty:: TyCtxt ;
19
19
use rustc_session:: config:: { self , CrateType , DebugInfo , LinkerPluginLto , Lto , OptLevel , Strip } ;
20
+ use rustc_session:: search_paths:: PathKind ;
20
21
use rustc_session:: Session ;
21
22
use rustc_target:: spec:: { Cc , LinkOutputKind , LinkerFlavor , Lld } ;
22
23
@@ -784,6 +785,38 @@ pub struct MsvcLinker<'a> {
784
785
sess : & ' a Session ,
785
786
}
786
787
788
+ impl MsvcLinker < ' _ > {
789
+ // FIXME this duplicates rustc_metadata::find_native_static_library,
790
+ // as the Meson/MinGW suffix for import libraries can differ
791
+ fn find_native_dynamic_library ( name : & str , verbatim : bool , sess : & Session ) -> OsString {
792
+ let formats = if verbatim {
793
+ vec ! [ ( "" . into( ) , "" . into( ) ) ]
794
+ } else {
795
+ // While the official naming convention for MSVC import libraries
796
+ // is foo.lib...
797
+ let os = ( sess. target . staticlib_prefix . clone ( ) , sess. target . staticlib_suffix . clone ( ) ) ;
798
+ // ... Meson follows the libfoo.dll.a convention to
799
+ // disambiguate .a for static libraries
800
+ let meson = ( "lib" . into ( ) , ".dll.a" . into ( ) ) ;
801
+ // and MinGW uses .a altogether
802
+ let mingw = ( "lib" . into ( ) , ".a" . into ( ) ) ;
803
+ vec ! [ os, meson, mingw]
804
+ } ;
805
+
806
+ for path in sess. target_filesearch ( PathKind :: Native ) . search_paths ( ) {
807
+ for ( prefix, suffix) in & formats {
808
+ let test = path. join ( format ! ( "{prefix}{name}{suffix}" ) ) ;
809
+ if test. exists ( ) {
810
+ return OsString :: from ( test) ;
811
+ }
812
+ }
813
+ }
814
+
815
+ // Allow the linker to find CRT libs itself
816
+ OsString :: from ( format ! ( "{}{}" , name, if verbatim { "" } else { ".lib" } ) )
817
+ }
818
+ }
819
+
787
820
impl < ' a > Linker for MsvcLinker < ' a > {
788
821
fn cmd ( & mut self ) -> & mut Command {
789
822
& mut self . cmd
@@ -808,7 +841,8 @@ impl<'a> Linker for MsvcLinker<'a> {
808
841
}
809
842
810
843
fn link_dylib_by_name ( & mut self , name : & str , verbatim : bool , _as_needed : bool ) {
811
- self . cmd . arg ( format ! ( "{}{}" , name, if verbatim { "" } else { ".lib" } ) ) ;
844
+ let path = MsvcLinker :: < ' a > :: find_native_dynamic_library ( name, verbatim, self . sess ) ;
845
+ self . cmd . arg ( path) ;
812
846
}
813
847
814
848
fn link_staticlib_by_name ( & mut self , name : & str , verbatim : bool , whole_archive : bool ) {
0 commit comments