Skip to content

Commit

Permalink
Mirror c_char configuration from rust-lang/rust
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgross35 committed Dec 17, 2024
1 parent 39a6799 commit 28d4064
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
35 changes: 34 additions & 1 deletion libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
});
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
}
});
Expand Down Expand Up @@ -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,
}
});
Expand Down Expand Up @@ -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_"));
Expand Down Expand Up @@ -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,
}
});
Expand Down Expand Up @@ -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,
}
});
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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,
}
});
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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+
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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,
}
});
Expand Down
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 28d4064

Please sign in to comment.