|
| 1 | +use std::env::VarError; |
1 | 2 | use std::process::{ |
2 | 3 | Command, |
3 | 4 | Output, |
@@ -53,7 +54,7 @@ fn main() { |
53 | 54 | println!("cargo:rerun-if-changed=build.rs"); |
54 | 55 |
|
55 | 56 | let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly(); |
56 | | - let libc_ci = env::var("LIBC_CI").is_ok(); |
| 57 | + let libc_ci = env_flag("LIBC_CI"); |
57 | 58 | let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); |
58 | 59 | let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); |
59 | 60 | let target_ptr_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap_or_default(); |
@@ -98,14 +99,14 @@ fn main() { |
98 | 99 | _ => (), |
99 | 100 | } |
100 | 101 |
|
101 | | - let musl_v1_2_3 = env::var("RUST_LIBC_UNSTABLE_MUSL_V1_2_3").is_ok(); |
| 102 | + let musl_v1_2_3 = env_flag("RUST_LIBC_UNSTABLE_MUSL_V1_2_3"); |
102 | 103 | println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_MUSL_V1_2_3"); |
103 | 104 | // loongarch64 and ohos have already updated |
104 | 105 | if musl_v1_2_3 || target_arch == "loongarch64" || target_env == "ohos" { |
105 | 106 | // FIXME(musl): enable time64 api as well |
106 | 107 | set_cfg("musl_v1_2_3"); |
107 | 108 | } |
108 | | - let linux_time_bits64 = env::var("RUST_LIBC_UNSTABLE_LINUX_TIME_BITS64").is_ok(); |
| 109 | + let linux_time_bits64 = env_flag("RUST_LIBC_UNSTABLE_LINUX_TIME_BITS64"); |
109 | 110 | println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_LINUX_TIME_BITS64"); |
110 | 111 | if linux_time_bits64 { |
111 | 112 | set_cfg("linux_time_bits64"); |
@@ -308,3 +309,13 @@ fn set_cfg(cfg: &str) { |
308 | 309 | ); |
309 | 310 | println!("cargo:rustc-cfg={cfg}"); |
310 | 311 | } |
| 312 | + |
| 313 | +/// Return true if the env is set to a value other than `0`. |
| 314 | +fn env_flag(key: &str) -> bool { |
| 315 | + match env::var(key) { |
| 316 | + Ok(x) if x == "0" => false, |
| 317 | + Err(VarError::NotPresent) => false, |
| 318 | + Err(VarError::NotUnicode(_)) => panic!("non-unicode var for `{key}`"), |
| 319 | + Ok(_) => true, |
| 320 | + } |
| 321 | +} |
0 commit comments