Skip to content

Commit 4a4a57b

Browse files
snoggetgross35
authored andcommitted
Simplify the reading of RUST_LIBC_UNSTABLE_GNU_*_BITS variables
Use (almost) the same code in build.rs and libc-test/build.rs. Make the decision chain clearer. Have a single default value that can be overriden in various ways, which should make switching the default much easier.
1 parent 6f115e5 commit 4a4a57b

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

build.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,37 @@ fn main() {
108108
&& target_arch != "riscv32"
109109
&& target_arch != "x86_64"
110110
{
111-
match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
112-
Ok(val) if val == "64" => {
113-
set_cfg("gnu_file_offset_bits64");
114-
set_cfg("linux_time_bits64");
115-
set_cfg("gnu_time_bits64");
116-
}
117-
Ok(val) if val != "32" => {
118-
panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
119-
}
120-
_ => {
121-
match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
122-
Ok(val) if val == "64" => {
123-
set_cfg("gnu_file_offset_bits64");
124-
}
125-
Ok(val) if val != "32" => {
126-
panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
127-
}
128-
_ => {}
129-
}
130-
}
111+
let defaultbits = "32".to_string();
112+
let (timebits, filebits) = match (
113+
env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS"),
114+
env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
115+
) {
116+
(Ok(_), Ok(_)) => panic!("Do not set both RUST_LIBC_UNSTABLE_GNU_TIME_BITS and RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
117+
(Err(_), Err(_)) => (defaultbits.clone(), defaultbits.clone()),
118+
(Ok(tb), Err(_)) if tb == "64" => (tb.clone(), tb.clone()),
119+
(Ok(tb), Err(_)) if tb == "32" => (tb, defaultbits.clone()),
120+
(Ok(_), Err(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS, must be 32 or 64"),
121+
(Err(_), Ok(fb)) if fb == "32" || fb == "64" => (defaultbits.clone(), fb),
122+
(Err(_), Ok(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32 or 64"),
123+
};
124+
let valid_bits = ["32", "64"];
125+
assert!(
126+
valid_bits.contains(&filebits.as_str()) && valid_bits.contains(&timebits.as_str()),
127+
"Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS or RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32, 64 or unset"
128+
);
129+
assert!(
130+
!(filebits == "32" && timebits == "64"),
131+
"RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS must be 64 or unset if RUST_LIBC_UNSTABLE_GNU_TIME_BITS is 64"
132+
);
133+
if timebits == "64" {
134+
set_cfg("linux_time_bits64");
135+
set_cfg("gnu_time_bits64");
136+
}
137+
if filebits == "64" {
138+
set_cfg("gnu_file_offset_bits64");
131139
}
132140
}
141+
133142
// On CI: deny all warnings
134143
if libc_ci {
135144
set_cfg("libc_deny_warnings");

libc-test/build.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,29 +3556,36 @@ fn config_gnu_bits(target: &str, cfg: &mut ctest::TestGenerator) {
35563556
&& !target.contains("riscv32")
35573557
&& pointer_width == "32"
35583558
{
3559-
match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
3560-
Ok(val) if val == "64" => {
3561-
cfg.define("_FILE_OFFSET_BITS", Some("64"));
3562-
cfg.define("_TIME_BITS", Some("64"));
3563-
cfg.cfg("gnu_file_offset_bits64", None);
3564-
cfg.cfg("linux_time_bits64", None);
3565-
cfg.cfg("gnu_time_bits64", None);
3566-
}
3567-
Ok(val) if val != "32" => {
3568-
panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
3569-
}
3570-
_ => {
3571-
match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
3572-
Ok(val) if val == "64" => {
3573-
cfg.define("_FILE_OFFSET_BITS", Some("64"));
3574-
cfg.cfg("gnu_file_offset_bits64", None);
3575-
}
3576-
Ok(val) if val != "32" => {
3577-
panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
3578-
}
3579-
_ => {}
3580-
}
3581-
}
3559+
let defaultbits = "32".to_string();
3560+
let (timebits, filebits) = match (
3561+
env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS"),
3562+
env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
3563+
) {
3564+
(Ok(_), Ok(_)) => panic!("Do not set both RUST_LIBC_UNSTABLE_GNU_TIME_BITS and RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
3565+
(Err(_), Err(_)) => (defaultbits.clone(), defaultbits.clone()),
3566+
(Ok(tb), Err(_)) if tb == "64" => (tb.clone(), tb.clone()),
3567+
(Ok(tb), Err(_)) if tb == "32" => (tb, defaultbits.clone()),
3568+
(Ok(_), Err(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS, must be 32 or 64"),
3569+
(Err(_), Ok(fb)) if fb == "32" || fb == "64" => (defaultbits.clone(), fb),
3570+
(Err(_), Ok(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32 or 64"),
3571+
};
3572+
let valid_bits = ["32", "64"];
3573+
assert!(
3574+
valid_bits.contains(&filebits.as_str()) && valid_bits.contains(&timebits.as_str()),
3575+
"Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS or RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32, 64 or unset"
3576+
);
3577+
assert!(
3578+
!(filebits == "32" && timebits == "64"),
3579+
"RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS must be 64 or unset if RUST_LIBC_UNSTABLE_GNU_TIME_BITS is 64"
3580+
);
3581+
if timebits == "64" {
3582+
cfg.define("_TIME_BITS", Some("64"));
3583+
cfg.cfg("linux_time_bits64", None);
3584+
cfg.cfg("gnu_time_bits64", None);
3585+
}
3586+
if filebits == "64" {
3587+
cfg.define("_FILE_OFFSET_BITS", Some("64"));
3588+
cfg.cfg("gnu_file_offset_bits64", None);
35823589
}
35833590
}
35843591
}

0 commit comments

Comments
 (0)