@@ -133,6 +133,7 @@ pub struct Build {
133
133
env_cache : Arc < Mutex < HashMap < String , Option < Arc < str > > > > > ,
134
134
apple_sdk_root_cache : Arc < Mutex < HashMap < String , OsString > > > ,
135
135
emit_rerun_if_env_changed : bool ,
136
+ cached_compiler_family : Arc < Mutex < HashMap < Box < Path > , ToolFamily > > > ,
136
137
}
137
138
138
139
/// Represents the types of errors that may occur while using cc-rs.
@@ -339,6 +340,7 @@ impl Build {
339
340
env_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
340
341
apple_sdk_root_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
341
342
emit_rerun_if_env_changed : true ,
343
+ cached_compiler_family : Arc :: default ( ) ,
342
344
}
343
345
}
344
346
@@ -2533,7 +2535,11 @@ impl Build {
2533
2535
2534
2536
fn get_base_compiler ( & self ) -> Result < Tool , Error > {
2535
2537
if let Some ( c) = & self . compiler {
2536
- return Ok ( Tool :: new ( ( * * c) . to_owned ( ) , & self . cargo_output ) ) ;
2538
+ return Ok ( Tool :: new (
2539
+ ( * * c) . to_owned ( ) ,
2540
+ & self . cached_compiler_family ,
2541
+ & self . cargo_output ,
2542
+ ) ) ;
2537
2543
}
2538
2544
let host = self . get_host ( ) ?;
2539
2545
let target = self . get_target ( ) ?;
@@ -2569,7 +2575,12 @@ impl Build {
2569
2575
// semi-buggy build scripts which are shared in
2570
2576
// makefiles/configure scripts (where spaces are far more
2571
2577
// lenient)
2572
- let mut t = Tool :: with_clang_driver ( tool, driver_mode, & self . cargo_output ) ;
2578
+ let mut t = Tool :: with_clang_driver (
2579
+ tool,
2580
+ driver_mode,
2581
+ & self . cached_compiler_family ,
2582
+ & self . cargo_output ,
2583
+ ) ;
2573
2584
if let Some ( cc_wrapper) = wrapper {
2574
2585
t. cc_wrapper_path = Some ( PathBuf :: from ( cc_wrapper) ) ;
2575
2586
}
@@ -2583,12 +2594,20 @@ impl Build {
2583
2594
let tool = if self . cpp { "em++" } else { "emcc" } ;
2584
2595
// Windows uses bat file so we have to be a bit more specific
2585
2596
if cfg ! ( windows) {
2586
- let mut t = Tool :: new ( PathBuf :: from ( "cmd" ) , & self . cargo_output ) ;
2597
+ let mut t = Tool :: new (
2598
+ PathBuf :: from ( "cmd" ) ,
2599
+ & self . cached_compiler_family ,
2600
+ & self . cargo_output ,
2601
+ ) ;
2587
2602
t. args . push ( "/c" . into ( ) ) ;
2588
2603
t. args . push ( format ! ( "{}.bat" , tool) . into ( ) ) ;
2589
2604
Some ( t)
2590
2605
} else {
2591
- Some ( Tool :: new ( PathBuf :: from ( tool) , & self . cargo_output ) )
2606
+ Some ( Tool :: new (
2607
+ PathBuf :: from ( tool) ,
2608
+ & self . cached_compiler_family ,
2609
+ & self . cargo_output ,
2610
+ ) )
2592
2611
}
2593
2612
} else {
2594
2613
None
@@ -2643,7 +2662,11 @@ impl Build {
2643
2662
default. to_string ( )
2644
2663
} ;
2645
2664
2646
- let mut t = Tool :: new ( PathBuf :: from ( compiler) , & self . cargo_output ) ;
2665
+ let mut t = Tool :: new (
2666
+ PathBuf :: from ( compiler) ,
2667
+ & self . cached_compiler_family ,
2668
+ & self . cargo_output ,
2669
+ ) ;
2647
2670
if let Some ( cc_wrapper) = Self :: rustc_wrapper_fallback ( ) {
2648
2671
t. cc_wrapper_path = Some ( PathBuf :: from ( cc_wrapper) ) ;
2649
2672
}
@@ -2660,7 +2683,13 @@ impl Build {
2660
2683
Err ( _) => PathBuf :: from ( "nvcc" ) ,
2661
2684
Ok ( nvcc) => PathBuf :: from ( & * nvcc) ,
2662
2685
} ;
2663
- let mut nvcc_tool = Tool :: with_features ( nvcc, None , self . cuda , & self . cargo_output ) ;
2686
+ let mut nvcc_tool = Tool :: with_features (
2687
+ nvcc,
2688
+ None ,
2689
+ self . cuda ,
2690
+ & self . cached_compiler_family ,
2691
+ & self . cargo_output ,
2692
+ ) ;
2664
2693
nvcc_tool
2665
2694
. args
2666
2695
. push ( format ! ( "-ccbin={}" , tool. path. display( ) ) . into ( ) ) ;
@@ -3553,16 +3582,27 @@ impl Default for Build {
3553
3582
}
3554
3583
3555
3584
impl Tool {
3556
- fn new ( path : PathBuf , cargo_output : & CargoOutput ) -> Self {
3557
- Tool :: with_features ( path, None , false , cargo_output)
3585
+ fn new (
3586
+ path : PathBuf ,
3587
+ cached_compiler_family : & Mutex < HashMap < Box < Path > , ToolFamily > > ,
3588
+ cargo_output : & CargoOutput ,
3589
+ ) -> Self {
3590
+ Self :: with_features ( path, None , false , cached_compiler_family, cargo_output)
3558
3591
}
3559
3592
3560
3593
fn with_clang_driver (
3561
3594
path : PathBuf ,
3562
3595
clang_driver : Option < & str > ,
3596
+ cached_compiler_family : & Mutex < HashMap < Box < Path > , ToolFamily > > ,
3563
3597
cargo_output : & CargoOutput ,
3564
3598
) -> Self {
3565
- Self :: with_features ( path, clang_driver, false , cargo_output)
3599
+ Self :: with_features (
3600
+ path,
3601
+ clang_driver,
3602
+ false ,
3603
+ cached_compiler_family,
3604
+ cargo_output,
3605
+ )
3566
3606
}
3567
3607
3568
3608
#[ cfg( windows) ]
@@ -3585,9 +3625,10 @@ impl Tool {
3585
3625
path : PathBuf ,
3586
3626
clang_driver : Option < & str > ,
3587
3627
cuda : bool ,
3628
+ cached_compiler_family : & Mutex < HashMap < Box < Path > , ToolFamily > > ,
3588
3629
cargo_output : & CargoOutput ,
3589
3630
) -> Self {
3590
- fn detect_family ( path : & Path , cargo_output : & CargoOutput ) -> ToolFamily {
3631
+ fn detect_family_inner ( path : & Path , cargo_output : & CargoOutput ) -> ToolFamily {
3591
3632
let mut cmd = Command :: new ( path) ;
3592
3633
cmd. arg ( "--version" ) ;
3593
3634
@@ -3620,6 +3661,18 @@ impl Tool {
3620
3661
ToolFamily :: Gnu
3621
3662
}
3622
3663
}
3664
+ let detect_family = |path : & Path | -> ToolFamily {
3665
+ if let Some ( family) = cached_compiler_family. lock ( ) . unwrap ( ) . get ( path) {
3666
+ return * family;
3667
+ }
3668
+
3669
+ let family = detect_family_inner ( path, cargo_output) ;
3670
+ cached_compiler_family
3671
+ . lock ( )
3672
+ . unwrap ( )
3673
+ . insert ( path. into ( ) , family) ;
3674
+ family
3675
+ } ;
3623
3676
3624
3677
// Try to detect family of the tool from its name, falling back to Gnu.
3625
3678
let family = if let Some ( fname) = path. file_name ( ) . and_then ( |p| p. to_str ( ) ) {
@@ -3633,10 +3686,10 @@ impl Tool {
3633
3686
_ => ToolFamily :: Clang ,
3634
3687
}
3635
3688
} else {
3636
- detect_family ( & path, cargo_output )
3689
+ detect_family ( & path)
3637
3690
}
3638
3691
} else {
3639
- detect_family ( & path, cargo_output )
3692
+ detect_family ( & path)
3640
3693
} ;
3641
3694
3642
3695
Tool {
0 commit comments