Skip to content

Commit b4a0fda

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. (backport <#4652>) (cherry picked from commit 4a4a57b)
1 parent 4d041c7 commit b4a0fda

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
@@ -112,28 +112,37 @@ fn main() {
112112
&& target_arch != "riscv32"
113113
&& target_arch != "x86_64"
114114
{
115-
match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
116-
Ok(val) if val == "64" => {
117-
set_cfg("gnu_file_offset_bits64");
118-
set_cfg("linux_time_bits64");
119-
set_cfg("gnu_time_bits64");
120-
}
121-
Ok(val) if val != "32" => {
122-
panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
123-
}
124-
_ => {
125-
match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
126-
Ok(val) if val == "64" => {
127-
set_cfg("gnu_file_offset_bits64");
128-
}
129-
Ok(val) if val != "32" => {
130-
panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
131-
}
132-
_ => {}
133-
}
134-
}
115+
let defaultbits = "32".to_string();
116+
let (timebits, filebits) = match (
117+
env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS"),
118+
env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
119+
) {
120+
(Ok(_), Ok(_)) => panic!("Do not set both RUST_LIBC_UNSTABLE_GNU_TIME_BITS and RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
121+
(Err(_), Err(_)) => (defaultbits.clone(), defaultbits.clone()),
122+
(Ok(tb), Err(_)) if tb == "64" => (tb.clone(), tb.clone()),
123+
(Ok(tb), Err(_)) if tb == "32" => (tb, defaultbits.clone()),
124+
(Ok(_), Err(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS, must be 32 or 64"),
125+
(Err(_), Ok(fb)) if fb == "32" || fb == "64" => (defaultbits.clone(), fb),
126+
(Err(_), Ok(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32 or 64"),
127+
};
128+
let valid_bits = ["32", "64"];
129+
assert!(
130+
valid_bits.contains(&filebits.as_str()) && valid_bits.contains(&timebits.as_str()),
131+
"Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS or RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32, 64 or unset"
132+
);
133+
assert!(
134+
!(filebits == "32" && timebits == "64"),
135+
"RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS must be 64 or unset if RUST_LIBC_UNSTABLE_GNU_TIME_BITS is 64"
136+
);
137+
if timebits == "64" {
138+
set_cfg("linux_time_bits64");
139+
set_cfg("gnu_time_bits64");
140+
}
141+
if filebits == "64" {
142+
set_cfg("gnu_file_offset_bits64");
135143
}
136144
}
145+
137146
// On CI: deny all warnings
138147
if libc_ci {
139148
set_cfg("libc_deny_warnings");

libc-test/build.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,29 +3698,36 @@ fn config_gnu_bits(target: &str, cfg: &mut ctest::TestGenerator) {
36983698
&& !target.contains("riscv32")
36993699
&& pointer_width == "32"
37003700
{
3701-
match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
3702-
Ok(val) if val == "64" => {
3703-
cfg.define("_FILE_OFFSET_BITS", Some("64"));
3704-
cfg.define("_TIME_BITS", Some("64"));
3705-
cfg.cfg("gnu_file_offset_bits64", None);
3706-
cfg.cfg("linux_time_bits64", None);
3707-
cfg.cfg("gnu_time_bits64", None);
3708-
}
3709-
Ok(val) if val != "32" => {
3710-
panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
3711-
}
3712-
_ => {
3713-
match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
3714-
Ok(val) if val == "64" => {
3715-
cfg.define("_FILE_OFFSET_BITS", Some("64"));
3716-
cfg.cfg("gnu_file_offset_bits64", None);
3717-
}
3718-
Ok(val) if val != "32" => {
3719-
panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
3720-
}
3721-
_ => {}
3722-
}
3723-
}
3701+
let defaultbits = "32".to_string();
3702+
let (timebits, filebits) = match (
3703+
env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS"),
3704+
env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
3705+
) {
3706+
(Ok(_), Ok(_)) => panic!("Do not set both RUST_LIBC_UNSTABLE_GNU_TIME_BITS and RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
3707+
(Err(_), Err(_)) => (defaultbits.clone(), defaultbits.clone()),
3708+
(Ok(tb), Err(_)) if tb == "64" => (tb.clone(), tb.clone()),
3709+
(Ok(tb), Err(_)) if tb == "32" => (tb, defaultbits.clone()),
3710+
(Ok(_), Err(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS, must be 32 or 64"),
3711+
(Err(_), Ok(fb)) if fb == "32" || fb == "64" => (defaultbits.clone(), fb),
3712+
(Err(_), Ok(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32 or 64"),
3713+
};
3714+
let valid_bits = ["32", "64"];
3715+
assert!(
3716+
valid_bits.contains(&filebits.as_str()) && valid_bits.contains(&timebits.as_str()),
3717+
"Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS or RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32, 64 or unset"
3718+
);
3719+
assert!(
3720+
!(filebits == "32" && timebits == "64"),
3721+
"RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS must be 64 or unset if RUST_LIBC_UNSTABLE_GNU_TIME_BITS is 64"
3722+
);
3723+
if timebits == "64" {
3724+
cfg.define("_TIME_BITS", Some("64"));
3725+
cfg.cfg("linux_time_bits64", None);
3726+
cfg.cfg("gnu_time_bits64", None);
3727+
}
3728+
if filebits == "64" {
3729+
cfg.define("_FILE_OFFSET_BITS", Some("64"));
3730+
cfg.cfg("gnu_file_offset_bits64", None);
37243731
}
37253732
}
37263733
}

0 commit comments

Comments
 (0)