@@ -232,7 +232,7 @@ use std::io::{self, Write};
232
232
use std:: path:: { Component , Path , PathBuf } ;
233
233
#[ cfg( feature = "parallel" ) ]
234
234
use std:: process:: Child ;
235
- use std:: process:: Command ;
235
+ use std:: process:: { Command , Stdio } ;
236
236
use std:: sync:: {
237
237
atomic:: { AtomicU8 , Ordering :: Relaxed } ,
238
238
Arc , RwLock ,
@@ -3226,7 +3226,6 @@ impl Build {
3226
3226
}
3227
3227
} ) ;
3228
3228
3229
- let default = tool. to_string ( ) ;
3230
3229
let tool = match tool_opt {
3231
3230
Some ( t) => t,
3232
3231
None => {
@@ -3287,32 +3286,39 @@ impl Build {
3287
3286
self . cmd ( & name)
3288
3287
} else if self . get_is_cross_compile ( ) ? {
3289
3288
match self . prefix_for_target ( & self . get_raw_target ( ) ?) {
3290
- Some ( p ) => {
3289
+ Some ( prefix ) => {
3291
3290
// GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both.
3292
3291
// Prefer -ar if it exists, as builds of `-gcc-ar` have been observed to be
3293
3292
// outright broken (such as when targeting freebsd with `--disable-lto`
3294
3293
// toolchain where the archiver attempts to load the LTO plugin anyway but
3295
3294
// fails to find one).
3296
3295
//
3297
3296
// The same applies to ranlib.
3298
- let mut chosen = default;
3299
- for & infix in & [ "" , "-gcc" ] {
3300
- let target_p = format ! ( "{}{}-{}" , p, infix, tool) ;
3301
- if Command :: new ( & target_p) . output ( ) . is_ok ( ) {
3302
- chosen = target_p;
3303
- break ;
3304
- }
3305
- }
3297
+ let chosen = [ "" , "-gcc" ]
3298
+ . iter ( )
3299
+ . filter_map ( |infix| {
3300
+ let target_p = format ! ( "{prefix}{infix}-{tool}" ) ;
3301
+ let status = Command :: new ( & target_p)
3302
+ . arg ( "--version" )
3303
+ . stdin ( Stdio :: null ( ) )
3304
+ . stdout ( Stdio :: null ( ) )
3305
+ . stderr ( Stdio :: null ( ) )
3306
+ . status ( )
3307
+ . ok ( ) ?;
3308
+ status. success ( ) . then_some ( target_p)
3309
+ } )
3310
+ . next ( )
3311
+ . unwrap_or_else ( || tool. to_string ( ) ) ;
3306
3312
name = chosen. into ( ) ;
3307
3313
self . cmd ( & name)
3308
3314
}
3309
3315
None => {
3310
- name = default . into ( ) ;
3316
+ name = tool . into ( ) ;
3311
3317
self . cmd ( & name)
3312
3318
}
3313
3319
}
3314
3320
} else {
3315
- name = default . into ( ) ;
3321
+ name = tool . into ( ) ;
3316
3322
self . cmd ( & name)
3317
3323
}
3318
3324
}
0 commit comments