Skip to content

Commit 5981556

Browse files
committed
build: Introduce env_flag to allow setting flags false
1 parent 5c4255f commit 5981556

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

build.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env::VarError;
12
use std::process::{
23
Command,
34
Output,
@@ -53,7 +54,7 @@ fn main() {
5354
println!("cargo:rerun-if-changed=build.rs");
5455

5556
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");
5758
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
5859
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
5960
let target_ptr_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap_or_default();
@@ -98,14 +99,14 @@ fn main() {
9899
_ => (),
99100
}
100101

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");
102103
println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_MUSL_V1_2_3");
103104
// loongarch64 and ohos have already updated
104105
if musl_v1_2_3 || target_arch == "loongarch64" || target_env == "ohos" {
105106
// FIXME(musl): enable time64 api as well
106107
set_cfg("musl_v1_2_3");
107108
}
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");
109110
println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_LINUX_TIME_BITS64");
110111
if linux_time_bits64 {
111112
set_cfg("linux_time_bits64");
@@ -308,3 +309,13 @@ fn set_cfg(cfg: &str) {
308309
);
309310
println!("cargo:rustc-cfg={cfg}");
310311
}
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

Comments
 (0)