Skip to content

Commit fdc96b1

Browse files
committed
Add cfg option gnu_time64_abi
The gnu_time64_abi option indicates that a 32-bit arch should use 64-bit time_t and 64-bit off_t and simliar file related types. 32-bit platforms are identified by CARGO_CFG_TARGET_POINTER_WIDTH, but x86_64-unknown-linux-gnux32 is a 64-bit platform and should not use gnu_time64_abi even if it has 32-bit pointers. riscv32 is a 32-bit platform but has always used 64-bit types for time and file operations. It should not use the gnu_time64_abi The default - all relevant platforms should use 64-bit time - can be overridden by setting the RUST_LIBC_TIME_BITS environment variable to 32. It can also be set to 64 to force gnu_time64_abi, or default which is the same as leaving it unset. The last two options are mainly useful for CI flows.
1 parent 42446e9 commit fdc96b1

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

build.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
1414
"freebsd13",
1515
"freebsd14",
1616
"freebsd15",
17+
"gnu_time64_abi",
1718
"libc_const_extern_fn",
1819
"libc_const_extern_fn_unstable",
1920
"libc_deny_warnings",
@@ -67,6 +68,11 @@ fn main() {
6768
Some(_) | None => (),
6869
}
6970

71+
// Some ABIs need to redirect time related symbols to their time64 equivalents.
72+
if is_gnu_time64_abi() {
73+
set_cfg("gnu_time64_abi");
74+
}
75+
7076
// On CI: deny all warnings
7177
if libc_ci {
7278
set_cfg("libc_deny_warnings");
@@ -205,3 +211,64 @@ fn set_cfg(cfg: &str) {
205211
}
206212
println!("cargo:rustc-cfg={}", cfg);
207213
}
214+
215+
fn is_gnu_time64_abi() -> bool {
216+
match env::var("CARGO_CFG_TARGET_ENV") {
217+
Ok(target_env) => {
218+
if target_env != "gnu" {
219+
return false;
220+
}
221+
}
222+
Err(_) => panic!("CARGO_CFG_TARGET_ENV not set"),
223+
}
224+
match env::var("CARGO_CFG_TARGET_OS") {
225+
Ok(target_os) => {
226+
if target_os != "linux" {
227+
return false;
228+
}
229+
}
230+
Err(_) => panic!("CARGO_CFG_TARGET_OS not set"),
231+
}
232+
match env::var("CARGO_CFG_TARGET_POINTER_WIDTH") {
233+
Ok(bits) => {
234+
if bits == "64" {
235+
return false;
236+
}
237+
}
238+
Err(_) => panic!("CARGO_CFG_TARGET_POINTER_WIDTH not set"),
239+
}
240+
match env::var("CARGO_CFG_TARGET_ARCH") {
241+
Ok(bits) => {
242+
if bits == "riscv32" {
243+
return false;
244+
}
245+
}
246+
Err(_) => panic!("CARGO_CFG_TARGET_ARCH not set"),
247+
}
248+
match env::var("RUST_LIBC_TIME_BITS") {
249+
Ok(time_bits) => {
250+
if time_bits == "64" {
251+
return true;
252+
}
253+
if time_bits == "32" {
254+
return false;
255+
}
256+
if time_bits != "default" {
257+
panic!("Invalid value for RUST_LIBC_TIME_BITS");
258+
}
259+
}
260+
Err(_) => {}
261+
}
262+
// At this point, we _know_ it is *-*-linux-gnu* with 32 bit
263+
// pointers. Some 64 bit arch still have 32 bit pointers though.
264+
match env::var("TARGET") {
265+
Ok(target) => {
266+
// x86_64-unknown-linux-gnux32 and similar
267+
if target.contains("x86_64") && target.contains("x32") {
268+
return false;
269+
}
270+
}
271+
Err(_) => panic!("TARGET not set"),
272+
}
273+
return true;
274+
}

0 commit comments

Comments
 (0)