From 28d406458efb715b5b1b37f57b27617f3b56110b Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Tue, 17 Dec 2024 09:57:41 +0000 Subject: [PATCH] Mirror `c_char` configuration from `rust-lang/rust` Create a module providing the same definitions of `c_char` as in `rust-lang/rust`, which in most cases are based on the architecture rather than the OS. This will allow individual platforms to reexport the definition rather than having configuration repeated in numerous modules. --- libc-test/build.rs | 35 ++++++++++++++++++++++++++++++++++- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/libc-test/build.rs b/libc-test/build.rs index 9db76e35b2235..edfde387ce91e 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -353,6 +353,9 @@ fn test_apple(target: &str) { // FIXME: "'__uint128' undeclared" in C "__uint128" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, + _ => false, } }); @@ -714,6 +717,8 @@ fn test_windows(target: &str) { "ssize_t" if !gnu => true, // FIXME: The size and alignment of this type are incorrect "time_t" if gnu && i686 => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => false, }); @@ -924,6 +929,8 @@ fn test_solarish(target: &str) { cfg.skip_type(move |ty| match ty { "sighandler_t" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => false, }); @@ -1224,6 +1231,8 @@ fn test_netbsd(target: &str) { match ty { // FIXME: sighandler_t is crazy across platforms "sighandler_t" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => false, } }); @@ -1441,7 +1450,8 @@ fn test_dragonflybsd(target: &str) { match ty { // sighandler_t is crazy across platforms "sighandler_t" => true, - + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => false, } }); @@ -1600,6 +1610,8 @@ fn test_wasi(target: &str) { } }); + cfg.skip_type(|ty| ty == "c_char_def"); + // These have a different and internal type in header files and are only // used here to generate a pointer to them in bindings so skip these tests. cfg.skip_static(|c| c.starts_with("_CLOCK_")); @@ -1848,6 +1860,9 @@ fn test_android(target: &str) { // FIXME: "'__uint128' undeclared" in C "__uint128" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, + _ => false, } }); @@ -2599,6 +2614,9 @@ fn test_freebsd(target: &str) { // `eventfd(2)` and things come with it are added in FreeBSD 13 "eventfd_t" if Some(13) > freebsd_ver => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, + _ => false, } }); @@ -2915,6 +2933,9 @@ fn test_emscripten(target: &str) { // https://github.com/emscripten-core/emscripten/issues/5033 ty if ty.starts_with("epoll") => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, + // LFS64 types have been removed in Emscripten 3.1.44 // https://github.com/emscripten-core/emscripten/pull/19812 t => t.ends_with("64") || t.ends_with("64_t"), @@ -3191,6 +3212,9 @@ fn test_neutrino(target: &str) { // Does not exist in Neutrino "locale_t" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, + _ => false, } }); @@ -3354,6 +3378,8 @@ fn test_vxworks(target: &str) { // FIXME cfg.skip_type(move |ty| match ty { "stat64" | "sighandler_t" | "off64_t" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => false, }); @@ -3701,6 +3727,9 @@ fn test_linux(target: &str) { // FIXME: "'__uint128' undeclared" in C "__uint128" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, + t => { if musl { // LFS64 types have been removed in musl 1.2.4+ @@ -4649,6 +4678,8 @@ fn test_linux_like_apis(target: &str) { }) .skip_type(move |ty| match ty { "Elf64_Phdr" | "Elf32_Phdr" => false, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => true, }); cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_elf.rs"); @@ -4884,6 +4915,8 @@ fn test_haiku(target: &str) { "pthread_condattr_t" => true, "pthread_mutexattr_t" => true, "pthread_rwlockattr_t" => true, + // `c_char_def` is always public but not always reexported. + "c_char_def" => true, _ => false, } }); diff --git a/src/lib.rs b/src/lib.rs index 01c092b2a6b48..b0304844641c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,44 @@ cfg_if! { pub use core::ffi::c_void; +/// Type definitions that are coupled tighter to architecture than OS. +mod arch { + cfg_if! { + // This configuration comes from `rust-lang/rust` in `library/core/src/ffi/mod.rs`. + if #[cfg(all( + not(windows), + // FIXME(ctest): just use `target_vendor` = "apple"` once `ctest` supports it + not(any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + )), + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "csky", + target_arch = "hexagon", + target_arch = "msp430", + target_arch = "powerpc", + target_arch = "powerpc64", + target_arch = "riscv64", + target_arch = "riscv32", + target_arch = "s390x", + target_arch = "xtensa", + ) + ))] { + // To be reexported as `c_char` + // FIXME(ctest): just name these `c_char` once `ctest` learns that these don't get + // exported. + pub type c_char_def = u8; + } else { + pub type c_char_def = i8; + } + } +} + cfg_if! { if #[cfg(windows)] { mod fixed_width_ints;