@@ -974,8 +974,22 @@ pub fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary
974
974
// (e.g. *-sys crates).
975
975
// We prefer system mingw-w64 libraries if they are available to avoid this issue.
976
976
fn get_crt_libs_path ( sess : & Session ) -> Option < PathBuf > {
977
- use std:: sync:: Mutex ;
977
+ fn find_exe_in_path < P > ( exe_name : P ) -> Option < PathBuf >
978
+ where P : AsRef < Path > ,
979
+ {
980
+ env:: var_os ( "PATH" ) . and_then ( |paths| {
981
+ env:: split_paths ( & paths) . filter_map ( |dir| {
982
+ let full_path = dir. join ( & exe_name) ;
983
+ if full_path. is_file ( ) {
984
+ Some ( fix_windows_verbatim_for_gcc ( & full_path) )
985
+ } else {
986
+ None
987
+ }
988
+ } ) . next ( )
989
+ } )
990
+ }
978
991
992
+ use std:: sync:: Mutex ;
979
993
lazy_static:: lazy_static! {
980
994
static ref SYSTEM_LIBS : Mutex <Option <Result <PathBuf , ( ) >>> = Mutex :: new( None ) ;
981
995
}
@@ -985,21 +999,36 @@ fn get_crt_libs_path(sess: &Session) -> Option<PathBuf> {
985
999
Some ( Ok ( compiler_libs_path) ) => Some ( compiler_libs_path) ,
986
1000
Some ( Err ( _) ) => None ,
987
1001
_ => {
988
- let cc = if let ( linker, LinkerFlavor :: Gcc ) = linker_and_flavor ( & sess) {
989
- linker
990
- } else {
991
- * SYSTEM_LIBS . lock ( ) . unwrap ( ) = Some ( Err ( ( ) ) ) ;
992
- return None ;
993
- } ;
994
- if let Ok ( output) = Command :: new ( cc) . arg ( "-print-file-name=crt2.o" ) . output ( ) {
995
- if let Some ( compiler_libs_path) =
996
- PathBuf :: from ( std:: str:: from_utf8 ( & output. stdout ) . unwrap ( ) ) . parent ( )
997
- {
998
- let compiler_libs_path = fix_windows_verbatim_for_gcc ( compiler_libs_path) ;
999
- * SYSTEM_LIBS . lock ( ) . unwrap ( ) = Some ( Ok ( compiler_libs_path. clone ( ) ) ) ;
1000
- return Some ( compiler_libs_path) ;
1001
- }
1002
+ if let ( linker, LinkerFlavor :: Gcc ) = linker_and_flavor ( & sess) {
1003
+ let linker_path = if cfg ! ( windows) {
1004
+ linker. with_extension ( "exe" )
1005
+ } else {
1006
+ linker
1007
+ } ;
1008
+ if let Some ( linker_path) = find_exe_in_path ( linker_path) {
1009
+ let mingw_arch = match & sess. target . target . arch {
1010
+ x if x == "x86" => "i686" ,
1011
+ x => x
1012
+ } ;
1013
+ let mingw_dir = [ mingw_arch, "-w64-mingw32" ] . concat ( ) ;
1014
+ // Here we have path/bin/gcc but we need path/
1015
+ let mut path = linker_path;
1016
+ path. pop ( ) ;
1017
+ path. pop ( ) ;
1018
+ // Based on Clang MinGW driver
1019
+ let probe_path = path. join ( [ & mingw_dir, "lib" ] . concat ( ) ) ;
1020
+ if probe_path. exists ( ) {
1021
+ * SYSTEM_LIBS . lock ( ) . unwrap ( ) = Some ( Ok ( probe_path. clone ( ) ) ) ;
1022
+ return Some ( probe_path) ;
1023
+ } ;
1024
+ let probe_path = path. join ( [ & mingw_dir, "sys-root/mingw/lib" ] . concat ( ) ) ;
1025
+ if probe_path. exists ( ) {
1026
+ * SYSTEM_LIBS . lock ( ) . unwrap ( ) = Some ( Ok ( probe_path. clone ( ) ) ) ;
1027
+ return Some ( probe_path) ;
1028
+ } ;
1029
+ } ;
1002
1030
} ;
1031
+ * SYSTEM_LIBS . lock ( ) . unwrap ( ) = Some ( Err ( ( ) ) ) ;
1003
1032
None
1004
1033
}
1005
1034
}
0 commit comments