@@ -283,6 +283,9 @@ pub struct Build {
283
283
ccbin : bool ,
284
284
std : Option < Arc < str > > ,
285
285
target : Option < Arc < str > > ,
286
+ /// The host compiler.
287
+ ///
288
+ /// Try to not access this directly, and instead prefer `cfg!(...)`.
286
289
host : Option < Arc < str > > ,
287
290
out_dir : Option < Arc < Path > > ,
288
291
opt_level : Option < Arc < str > > ,
@@ -658,11 +661,13 @@ impl Build {
658
661
. cargo_metadata ( self . cargo_output . metadata )
659
662
. target ( target)
660
663
. opt_level ( 0 )
661
- . host ( & self . get_host ( ) ?)
662
664
. debug ( false )
663
665
. cpp ( self . cpp )
664
666
. cuda ( self . cuda )
665
667
. emit_rerun_if_env_changed ( self . emit_rerun_if_env_changed ) ;
668
+ if let Some ( host) = & self . host {
669
+ cfg. host ( host) ;
670
+ }
666
671
cfg. try_get_compiler ( ) ?
667
672
} ;
668
673
@@ -2866,7 +2871,6 @@ impl Build {
2866
2871
out_dir,
2867
2872
) ) ;
2868
2873
}
2869
- let host = self . get_host ( ) ?;
2870
2874
let target = self . get_target ( ) ?;
2871
2875
let target = & * target;
2872
2876
let ( env, msvc, gnu, traditional, clang) = if self . cpp {
@@ -2879,7 +2883,7 @@ impl Build {
2879
2883
// is not flag-compatible with "gcc". This history casts a long shadow,
2880
2884
// and many modern illumos distributions today ship GCC as "gcc" without
2881
2885
// also making it available as "cc".
2882
- let default = if host . contains ( "solaris" ) || host . contains ( "illumos" ) {
2886
+ let default = if cfg ! ( target_os = "solaris" ) || cfg ! ( target_os = "illumos" ) {
2883
2887
gnu
2884
2888
} else {
2885
2889
traditional
@@ -2944,7 +2948,7 @@ impl Build {
2944
2948
let tool = match tool_opt {
2945
2949
Some ( t) => t,
2946
2950
None => {
2947
- let compiler = if host . contains ( " windows" ) && target. contains ( "windows" ) {
2951
+ let compiler = if cfg ! ( windows) && target. contains ( "windows" ) {
2948
2952
if target. contains ( "msvc" ) {
2949
2953
msvc. to_string ( )
2950
2954
} else {
@@ -2958,7 +2962,7 @@ impl Build {
2958
2962
{
2959
2963
clang. to_string ( )
2960
2964
} else if target. contains ( "android" ) {
2961
- autodetect_android_compiler ( target, & host , gnu, clang)
2965
+ autodetect_android_compiler ( target, gnu, clang)
2962
2966
} else if target. contains ( "cloudabi" ) {
2963
2967
format ! ( "{}-{}" , target, traditional)
2964
2968
} else if Build :: is_wasi_target ( target) {
@@ -2977,7 +2981,7 @@ impl Build {
2977
2981
format ! ( "arm-kmc-eabi-{}" , gnu)
2978
2982
} else if target. starts_with ( "aarch64-kmc-solid_" ) {
2979
2983
format ! ( "aarch64-kmc-elf-{}" , gnu)
2980
- } else if & * self . get_host ( ) ? != target {
2984
+ } else if self . get_is_cross_compile ( ) ? {
2981
2985
let prefix = self . prefix_for_target ( target) ;
2982
2986
match prefix {
2983
2987
Some ( prefix) => {
@@ -3042,8 +3046,7 @@ impl Build {
3042
3046
// on Windows is restricted to around 8k characters instead of around 32k characters.
3043
3047
// To remove this limit, we call the main clang binary directly and construct the
3044
3048
// `--target=` ourselves.
3045
- if host. contains ( "windows" ) && android_clang_compiler_uses_target_arg_internally ( & tool. path )
3046
- {
3049
+ if cfg ! ( windows) && android_clang_compiler_uses_target_arg_internally ( & tool. path ) {
3047
3050
if let Some ( path) = tool. path . file_name ( ) {
3048
3051
let file_name = path. to_str ( ) . unwrap ( ) . to_owned ( ) ;
3049
3052
let ( target, clang) = file_name. split_at ( file_name. rfind ( '-' ) . unwrap ( ) ) ;
@@ -3430,7 +3433,7 @@ impl Build {
3430
3433
// Use the GNU-variant to match other Unix systems.
3431
3434
name = format ! ( "g{}" , tool) . into ( ) ;
3432
3435
self . cmd ( & name)
3433
- } else if self . get_host ( ) ? != target {
3436
+ } else if self . get_is_cross_compile ( ) ? {
3434
3437
match self . prefix_for_target ( & target) {
3435
3438
Some ( p) => {
3436
3439
// GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both.
@@ -3644,11 +3647,13 @@ impl Build {
3644
3647
}
3645
3648
}
3646
3649
3647
- fn get_host ( & self ) -> Result < Cow < ' _ , str > , Error > {
3648
- match & self . host {
3649
- Some ( h) => Ok ( Cow :: Borrowed ( h) ) ,
3650
- None => self . getenv_unwrap_str ( "HOST" ) . map ( Cow :: Owned ) ,
3651
- }
3650
+ fn get_is_cross_compile ( & self ) -> Result < bool , Error > {
3651
+ let target = self . get_target ( ) ?;
3652
+ let host: Cow < ' _ , str > = match & self . host {
3653
+ Some ( h) => Cow :: Borrowed ( h) ,
3654
+ None => Cow :: Owned ( self . getenv_unwrap_str ( "HOST" ) ?) ,
3655
+ } ;
3656
+ Ok ( host != target)
3652
3657
}
3653
3658
3654
3659
fn get_opt_level ( & self ) -> Result < Cow < ' _ , str > , Error > {
@@ -3765,8 +3770,11 @@ impl Build {
3765
3770
3766
3771
fn getenv_with_target_prefixes ( & self , var_base : & str ) -> Result < Arc < OsStr > , Error > {
3767
3772
let target = self . get_target ( ) ?;
3768
- let host = self . get_host ( ) ?;
3769
- let kind = if host == target { "HOST" } else { "TARGET" } ;
3773
+ let kind = if self . get_is_cross_compile ( ) ? {
3774
+ "TARGET"
3775
+ } else {
3776
+ "HOST"
3777
+ } ;
3770
3778
let target_u = target. replace ( '-' , "_" ) ;
3771
3779
let res = self
3772
3780
. getenv ( & format ! ( "{}_{}" , var_base, target) )
@@ -3799,8 +3807,7 @@ impl Build {
3799
3807
3800
3808
fn fix_env_for_apple_os ( & self , cmd : & mut Command ) -> Result < ( ) , Error > {
3801
3809
let target = self . get_target ( ) ?;
3802
- let host = self . get_host ( ) ?;
3803
- if host. contains ( "apple-darwin" ) && target. contains ( "apple-darwin" ) {
3810
+ if cfg ! ( target_os = "macos" ) && target. contains ( "apple-darwin" ) {
3804
3811
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
3805
3812
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
3806
3813
// although this is apparently ignored when using the linker at "/usr/bin/ld".
@@ -4209,7 +4216,7 @@ fn android_clang_compiler_uses_target_arg_internally(clang_path: &Path) -> bool
4209
4216
false
4210
4217
}
4211
4218
4212
- fn autodetect_android_compiler ( target : & str , host : & str , gnu : & str , clang : & str ) -> String {
4219
+ fn autodetect_android_compiler ( target : & str , gnu : & str , clang : & str ) -> String {
4213
4220
let new_clang_key = match target {
4214
4221
"aarch64-linux-android" => Some ( "aarch64" ) ,
4215
4222
"armv7-linux-androideabi" => Some ( "armv7a" ) ,
@@ -4249,7 +4256,7 @@ fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str)
4249
4256
// if not, use clang
4250
4257
if Command :: new ( & gnu_compiler) . output ( ) . is_ok ( ) {
4251
4258
gnu_compiler
4252
- } else if host . contains ( " windows" ) && Command :: new ( & clang_compiler_cmd) . output ( ) . is_ok ( ) {
4259
+ } else if cfg ! ( windows) && Command :: new ( & clang_compiler_cmd) . output ( ) . is_ok ( ) {
4253
4260
clang_compiler_cmd
4254
4261
} else {
4255
4262
clang_compiler
0 commit comments