@@ -232,7 +232,7 @@ use std::io::{self, Write};
232232use std:: path:: { Component , Path , PathBuf } ;
233233#[ cfg( feature = "parallel" ) ]
234234use std:: process:: Child ;
235- use std:: process:: Command ;
235+ use std:: process:: { Command , Stdio } ;
236236use std:: sync:: {
237237 atomic:: { AtomicU8 , Ordering :: Relaxed } ,
238238 Arc , RwLock ,
@@ -3226,7 +3226,6 @@ impl Build {
32263226 }
32273227 } ) ;
32283228
3229- let default = tool. to_string ( ) ;
32303229 let tool = match tool_opt {
32313230 Some ( t) => t,
32323231 None => {
@@ -3287,32 +3286,39 @@ impl Build {
32873286 self . cmd ( & name)
32883287 } else if self . get_is_cross_compile ( ) ? {
32893288 match self . prefix_for_target ( & self . get_raw_target ( ) ?) {
3290- Some ( p ) => {
3289+ Some ( prefix ) => {
32913290 // GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both.
32923291 // Prefer -ar if it exists, as builds of `-gcc-ar` have been observed to be
32933292 // outright broken (such as when targeting freebsd with `--disable-lto`
32943293 // toolchain where the archiver attempts to load the LTO plugin anyway but
32953294 // fails to find one).
32963295 //
32973296 // 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 ( ) ) ;
33063312 name = chosen. into ( ) ;
33073313 self . cmd ( & name)
33083314 }
33093315 None => {
3310- name = default . into ( ) ;
3316+ name = tool . into ( ) ;
33113317 self . cmd ( & name)
33123318 }
33133319 }
33143320 } else {
3315- name = default . into ( ) ;
3321+ name = tool . into ( ) ;
33163322 self . cmd ( & name)
33173323 }
33183324 }
0 commit comments