Skip to content

Simplify the reading of RUST_LIBC_UNSTABLE_GNU_*_BITS variables #4652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,37 @@ fn main() {
&& target_arch != "riscv32"
&& target_arch != "x86_64"
{
match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
Ok(val) if val == "64" => {
set_cfg("gnu_file_offset_bits64");
set_cfg("linux_time_bits64");
set_cfg("gnu_time_bits64");
}
Ok(val) if val != "32" => {
panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
}
_ => {
match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
Ok(val) if val == "64" => {
set_cfg("gnu_file_offset_bits64");
}
Ok(val) if val != "32" => {
panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
}
_ => {}
}
}
let defaultbits = "32".to_string();
let (timebits, filebits) = match (
env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS"),
env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
) {
(Ok(_), Ok(_)) => panic!("Do not set both RUST_LIBC_UNSTABLE_GNU_TIME_BITS and RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
(Err(_), Err(_)) => (defaultbits.clone(), defaultbits.clone()),
(Ok(tb), Err(_)) if tb == "64" => (tb.clone(), tb.clone()),
(Ok(tb), Err(_)) if tb == "32" => (tb, defaultbits.clone()),
(Ok(_), Err(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS, must be 32 or 64"),
(Err(_), Ok(fb)) if fb == "32" || fb == "64" => (defaultbits.clone(), fb),
(Err(_), Ok(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32 or 64"),
};
let valid_bits = ["32", "64"];
assert!(
valid_bits.contains(&filebits.as_str()) && valid_bits.contains(&timebits.as_str()),
"Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS or RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32, 64 or unset"
);
assert!(
!(filebits == "32" && timebits == "64"),
"RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS must be 64 or unset if RUST_LIBC_UNSTABLE_GNU_TIME_BITS is 64"
);
if timebits == "64" {
set_cfg("linux_time_bits64");
set_cfg("gnu_time_bits64");
}
if filebits == "64" {
set_cfg("gnu_file_offset_bits64");
}
}

// On CI: deny all warnings
if libc_ci {
set_cfg("libc_deny_warnings");
Expand Down
53 changes: 30 additions & 23 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3559,29 +3559,36 @@ fn config_gnu_bits(target: &str, cfg: &mut ctest::TestGenerator) {
&& !target.contains("riscv32")
&& pointer_width == "32"
{
match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
Ok(val) if val == "64" => {
cfg.define("_FILE_OFFSET_BITS", Some("64"));
cfg.define("_TIME_BITS", Some("64"));
cfg.cfg("gnu_file_offset_bits64", None);
cfg.cfg("linux_time_bits64", None);
cfg.cfg("gnu_time_bits64", None);
}
Ok(val) if val != "32" => {
panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
}
_ => {
match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
Ok(val) if val == "64" => {
cfg.define("_FILE_OFFSET_BITS", Some("64"));
cfg.cfg("gnu_file_offset_bits64", None);
}
Ok(val) if val != "32" => {
panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
}
_ => {}
}
}
let defaultbits = "32".to_string();
let (timebits, filebits) = match (
env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS"),
env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
) {
(Ok(_), Ok(_)) => panic!("Do not set both RUST_LIBC_UNSTABLE_GNU_TIME_BITS and RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS"),
(Err(_), Err(_)) => (defaultbits.clone(), defaultbits.clone()),
(Ok(tb), Err(_)) if tb == "64" => (tb.clone(), tb.clone()),
(Ok(tb), Err(_)) if tb == "32" => (tb, defaultbits.clone()),
(Ok(_), Err(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS, must be 32 or 64"),
(Err(_), Ok(fb)) if fb == "32" || fb == "64" => (defaultbits.clone(), fb),
(Err(_), Ok(_)) => panic!("Invalid value for RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32 or 64"),
};
let valid_bits = ["32", "64"];
assert!(
valid_bits.contains(&filebits.as_str()) && valid_bits.contains(&timebits.as_str()),
"Invalid value for RUST_LIBC_UNSTABLE_GNU_TIME_BITS or RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS, must be 32, 64 or unset"
);
assert!(
!(filebits == "32" && timebits == "64"),
"RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS must be 64 or unset if RUST_LIBC_UNSTABLE_GNU_TIME_BITS is 64"
);
if timebits == "64" {
cfg.define("_TIME_BITS", Some("64"));
cfg.cfg("linux_time_bits64", None);
cfg.cfg("gnu_time_bits64", None);
}
if filebits == "64" {
cfg.define("_FILE_OFFSET_BITS", Some("64"));
cfg.cfg("gnu_file_offset_bits64", None);
}
}
}
Expand Down