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.

(backport <#4198>)
(cherry picked from commit 28d4064)
  • Loading branch information
tgross35 committed Dec 18, 2024
1 parent 3eb9670 commit 9fea17a
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 @@ -364,6 +364,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 @@ -758,6 +761,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 @@ -975,6 +980,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 @@ -1278,6 +1285,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 @@ -1497,7 +1506,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 @@ -1659,6 +1669,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 @@ -1907,6 +1919,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 @@ -2669,6 +2684,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 @@ -2989,6 +3007,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 @@ -3260,6 +3281,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 @@ -3426,6 +3450,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 @@ -3773,6 +3799,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 @@ -4725,6 +4754,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 @@ -4960,6 +4991,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 @@ -41,6 +41,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 9fea17a

Please sign in to comment.