From 826a984fdbe1bee7f012525297223b97a690f6c6 Mon Sep 17 00:00:00 2001 From: Red Bear OS Date: Mon, 29 Jun 2026 01:28:57 +0300 Subject: [PATCH] relibc: move stddef header from C to cbindgen (integrate upstream 3be84f4b) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cherry-pick upstream commit 3be84f4b (auronandace, 2026-06-24): - Delete hand-written include/stddef.h - Create src/header/stddef/ with cbindgen.toml + mod.rs - Create src/header/bits_wchar-t/ with cbindgen.toml + mod.rs - Create src/header/bits_size-t/ with cbindgen.toml + mod.rs - Create src/header/bits_null/ with cbindgen.toml + mod.rs - Update src/header/mod.rs: add bits_* modules, uncomment stddef The generated stddef.h now includes modular bits_* sub-headers: #include — wchar_t with _WCHAR_T guard + __WCHAR_TYPE__ #include — size_t from Rust usize via cbindgen [export] #include — NULL: nullptr (C++11), 0L (C++), (void*)0 (C) This fixes the wchar_t circular include issue structurally. --- include/stddef.h | 22 -------------------- src/header/bits_null/cbindgen.toml | 20 ++++++++++++++++++ src/header/bits_null/mod.rs | 5 +++++ src/header/bits_size-t/cbindgen.toml | 11 ++++++++++ src/header/bits_size-t/mod.rs | 9 +++++++++ src/header/bits_wchar-t/cbindgen.toml | 21 +++++++++++++++++++ src/header/bits_wchar-t/mod.rs | 5 +++++ src/header/mod.rs | 7 ++++++- src/header/stddef/cbindgen.toml | 29 +++++++++++++++++++++++++++ src/header/stddef/mod.rs | 5 +++++ 10 files changed, 111 insertions(+), 23 deletions(-) delete mode 100644 include/stddef.h create mode 100644 src/header/bits_null/cbindgen.toml create mode 100644 src/header/bits_null/mod.rs create mode 100644 src/header/bits_size-t/cbindgen.toml create mode 100644 src/header/bits_size-t/mod.rs create mode 100644 src/header/bits_wchar-t/cbindgen.toml create mode 100644 src/header/bits_wchar-t/mod.rs create mode 100644 src/header/stddef/cbindgen.toml create mode 100644 src/header/stddef/mod.rs diff --git a/include/stddef.h b/include/stddef.h deleted file mode 100644 index a25ac8beb7..0000000000 --- a/include/stddef.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _STDDEF_H -#define _STDDEF_H -#include - -#define NULL 0 - -#ifndef __PTRDIFF_TYPE__ -#define __PTRDIFF_TYPE__ long int -#endif -typedef __PTRDIFF_TYPE__ ptrdiff_t; - -typedef long unsigned int size_t; - -#ifndef __cplusplus -typedef int wchar_t; -#endif - -typedef struct { long long __ll; long double __ld; } max_align_t; - -#define offsetof(type, member) __builtin_offsetof(type, member) - -#endif /* _STDDEF_H */ diff --git a/src/header/bits_null/cbindgen.toml b/src/header/bits_null/cbindgen.toml new file mode 100644 index 0000000000..53d2880f36 --- /dev/null +++ b/src/header/bits_null/cbindgen.toml @@ -0,0 +1,20 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/stddef.h.html +# +# This type is split out to prevent importing all of stddef.h into other headers. +include_guard = "_RELIBC_BITS_NULL_T_H" +language = "C" +no_includes = true +after_includes = """ +// Null pointer constant. +// Any pointer object whose representation has all bits set to zero, perhaps by +// memset() to 0 or by calloc(), shall be treated as a null pointer. +#ifdef __cplusplus +#if __cplusplus >= 201103L + #define NULL nullptr +#else + #define NULL 0L +#endif +#else + #define NULL ((void *)0) +#endif +""" diff --git a/src/header/bits_null/mod.rs b/src/header/bits_null/mod.rs new file mode 100644 index 0000000000..92f520a41e --- /dev/null +++ b/src/header/bits_null/mod.rs @@ -0,0 +1,5 @@ +//! `NULL` from `stddef.h` implementation. +//! +//! See . +//! +//! Implemented via ifdefs and defines in cbindgen. diff --git a/src/header/bits_size-t/cbindgen.toml b/src/header/bits_size-t/cbindgen.toml new file mode 100644 index 0000000000..d2835f458a --- /dev/null +++ b/src/header/bits_size-t/cbindgen.toml @@ -0,0 +1,11 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/stddef.h.html +# +# This type is split out to prevent importing all of stddef.h into other headers. +include_guard = "_RELIBC_BITS_SIZE_T_H" +language = "C" +no_includes = true +cpp_compat = true +[export] +include = ["size_t"] +[enum] +prefix_with_name = true diff --git a/src/header/bits_size-t/mod.rs b/src/header/bits_size-t/mod.rs new file mode 100644 index 0000000000..4bde8f9acf --- /dev/null +++ b/src/header/bits_size-t/mod.rs @@ -0,0 +1,9 @@ +//! `size_t` from `stddef.h` implementation. +//! +//! See . + +use crate::platform::types::c_ulong; + +/// Unsigned integer type of the result of the sizeof operator. +#[allow(non_camel_case_types)] +pub type size_t = c_ulong; diff --git a/src/header/bits_wchar-t/cbindgen.toml b/src/header/bits_wchar-t/cbindgen.toml new file mode 100644 index 0000000000..1d25b19d39 --- /dev/null +++ b/src/header/bits_wchar-t/cbindgen.toml @@ -0,0 +1,21 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/stddef.h.html +# +# This type is split out to prevent importing all of stddef.h into inttypes.h +include_guard = "_RELIBC_BITS_WCHAR_T_H" +after_includes = """ +// Integer type whose range of values can represent distinct codes for all +// members of the largest extended character set specified among the supported +// locales; the null character shall have the code value zero. +#ifndef _WCHAR_T +#define _WCHAR_T +#ifndef __cplusplus + #ifndef __WCHAR_TYPE__ + #define __WCHAR_TYPE__ int + #endif + typedef __WCHAR_TYPE__ wchar_t; +#endif // __cplusplus +#endif // _WCHAR_T +""" +language = "C" +no_includes = true +cpp_compat = true diff --git a/src/header/bits_wchar-t/mod.rs b/src/header/bits_wchar-t/mod.rs new file mode 100644 index 0000000000..d299be4a82 --- /dev/null +++ b/src/header/bits_wchar-t/mod.rs @@ -0,0 +1,5 @@ +//! `wchar_t` from `stddef.h` implementation. +//! +//! See . +//! +//! Implemented via ifdefs and defines in cbindgen. diff --git a/src/header/mod.rs b/src/header/mod.rs index c078fbe1c8..49e5e52ce6 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -9,14 +9,19 @@ pub mod bits_eventfd; pub mod bits_iovec; #[path = "bits_locale-t/mod.rs"] pub mod bits_locale_t; +pub mod bits_null; pub mod bits_pthread; #[path = "bits_safamily-t/mod.rs"] pub mod bits_safamily_t; #[path = "bits_sigset-t/mod.rs"] pub mod bits_sigset_t; +#[path = "bits_size-t/mod.rs"] +pub mod bits_size_t; #[path = "bits_socklen-t/mod.rs"] pub mod bits_socklen_t; pub mod bits_timespec; +#[path = "bits_wchar-t/mod.rs"] +pub mod bits_wchar_t; // complex.h implemented in C pub mod cpio; pub mod crypt; @@ -82,7 +87,7 @@ pub mod spawn; // stdarg.h implemented in C // stdatomic.h implemented in C // stdbool.h implemented in C -// stddef.h implemented in C +pub mod stddef; // stdint.h implemented in C pub mod stdio; pub mod stdlib; diff --git a/src/header/stddef/cbindgen.toml b/src/header/stddef/cbindgen.toml new file mode 100644 index 0000000000..ab10db8119 --- /dev/null +++ b/src/header/stddef/cbindgen.toml @@ -0,0 +1,29 @@ +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/stddef.h.html +# +# There are no spec quotations relating to includes +include_guard = "_RELIBC_STDDEF_H" +after_includes = """ +// wchar_t should come before NULL and size_t +// according to comment in wchar cbindgen +#include +#include +#include + +#ifndef __PTRDIFF_TYPE__ +#define __PTRDIFF_TYPE__ long int +#endif +// Signed integer type of the result of subtracting two pointers. +typedef __PTRDIFF_TYPE__ ptrdiff_t; + +// Object type whose alignment is the greatest fundamental alignment. +typedef struct { long long __ll; long double __ld; } max_align_t; + +// Integer constant expression of type size_t, the value of which is the offset +// in bytes to the structure member from the beginning of its structure. +#define offsetof(type, member) __builtin_offsetof(type, member) +""" +language = "C" +no_includes = true +cpp_compat = true +[enum] +prefix_with_name = true diff --git a/src/header/stddef/mod.rs b/src/header/stddef/mod.rs new file mode 100644 index 0000000000..50fd1de6a0 --- /dev/null +++ b/src/header/stddef/mod.rs @@ -0,0 +1,5 @@ +//! `stddef.h` implementation. +//! +//! See . +//! +//! Implemented via includes, typedefs and defines in cbindgen.