From 536e6625bf45762aec2dc216745253002573b575 Mon Sep 17 00:00:00 2001 From: Lucas Chollet Date: Wed, 26 Aug 2026 21:03:30 +0200 Subject: [PATCH] libcxx: add serenity support --- lib/libcxx/include/__config | 7 +- .../include/__locale_dir/locale_base_api.h | 3 + lib/libcxx/include/__locale_dir/messages.h | 3 +- .../include/__locale_dir/support/serenity.h | 274 ++++++++++++++++++ lib/libcxxabi/include/cxxabi.h | 3 +- lib/libcxxabi/src/cxa_thread_atexit.cpp | 3 +- 6 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 lib/libcxx/include/__locale_dir/support/serenity.h diff --git a/lib/libcxx/include/__config b/lib/libcxx/include/__config index b94483274c24ee0ac222518ddc677c79bd660bfd..777fd1ea5b2c08aad654a70ef9e5203b78d32c2f 100644 --- a/lib/libcxx/include/__config +++ b/lib/libcxx/include/__config @@ -236,8 +236,10 @@ typedef __char32_t char32_t; defined(__MVS__) || \ defined(_AIX) || \ defined(__EMSCRIPTEN__) || \ - defined(__HAIKU__) + defined(__HAIKU__) || \ + defined(__serenity__) // zig patch: haiku support +// zig patch: serenity support // clang-format on # undef _LIBCPP_HAS_THREAD_API_PTHREAD # define _LIBCPP_HAS_THREAD_API_PTHREAD 1 @@ -315,8 +317,9 @@ typedef __char32_t char32_t; # define _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION 0 # endif +// zig patch: serenity support # if defined(__BIONIC__) || defined(__NuttX__) || defined(__Fuchsia__) || defined(__wasi__) || \ - _LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || _LIBCPP_LIBC_LLVM_LIBC + _LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || _LIBCPP_LIBC_LLVM_LIBC || defined(__serenity__) # define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE # endif diff --git a/lib/libcxx/include/__locale_dir/locale_base_api.h b/lib/libcxx/include/__locale_dir/locale_base_api.h index 2e315625cfb621f6c6bbb7788213f2b7058826a2..922a1765883a596d4d11f45ef431354e338ecfe8 100644 --- a/lib/libcxx/include/__locale_dir/locale_base_api.h +++ b/lib/libcxx/include/__locale_dir/locale_base_api.h @@ -131,6 +131,9 @@ // zig patch: haiku support # elif defined(__HAIKU__) # include <__locale_dir/support/haiku.h> +// zig patch: serenity support +# elif defined(__serenity__) +# include <__locale_dir/support/serenity.h> # else // TODO: This is a temporary definition to bridge between the old way we defined the locale base API diff --git a/lib/libcxx/include/__locale_dir/messages.h b/lib/libcxx/include/__locale_dir/messages.h index 986a57d8b17b23bf4867ac7908623bb867c0667c..78202786e1ed946821c45057da8e1b630578144f 100644 --- a/lib/libcxx/include/__locale_dir/messages.h +++ b/lib/libcxx/include/__locale_dir/messages.h @@ -22,7 +22,8 @@ # if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) // Most unix variants have catopen. These are the specific ones that don't. -# if !defined(__BIONIC__) && !_LIBCPP_LIBC_NEWLIB && !defined(__EMSCRIPTEN__) +// zig patch: serenity support +# if !defined(__BIONIC__) && !_LIBCPP_LIBC_NEWLIB && !defined(__EMSCRIPTEN__) && !defined(__serenity__) # define _LIBCPP_HAS_CATOPEN 1 # include # else diff --git a/lib/libcxx/include/__locale_dir/support/serenity.h b/lib/libcxx/include/__locale_dir/support/serenity.h new file mode 100644 index 0000000000000000000000000000000000000000..3d5bd00c46fe6f112e7227c694f215215033b894 --- /dev/null +++ b/lib/libcxx/include/__locale_dir/support/serenity.h @@ -0,0 +1,274 @@ +// zig patch: serenity support +// -*- C++ -*- +//===-----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_SERENITY_H +#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_SERENITY_H + +#include <__config> +#include <__cstddef/size_t.h> +#include <__std_mbstate_t.h> +#include <__utility/forward.h> +#include // std::lconv +#include +#include +#include +#include +#include +#include +#if _LIBCPP_HAS_WIDE_CHARACTERS +# include +# include +#endif + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD +namespace __locale { + +struct __locale_guard { + __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {} + + ~__locale_guard() { + if (__old_loc_) + ::uselocale(__old_loc_); + } + + locale_t __old_loc_; + + __locale_guard(__locale_guard const&) = delete; + __locale_guard& operator=(__locale_guard const&) = delete; +}; + +// +// Locale management +// +#define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK +#define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK +#define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK +#define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK +#define _LIBCPP_TIME_MASK LC_TIME_MASK +#define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK +#define _LIBCPP_ALL_MASK LC_ALL_MASK +#define _LIBCPP_LC_ALL LC_ALL + +using __locale_t _LIBCPP_NODEBUG = ::locale_t; +#if defined(_LIBCPP_BUILDING_LIBRARY) +using __lconv_t _LIBCPP_NODEBUG = std::lconv; + +inline __locale_t __newlocale(int __category_mask, const char* __locale, __locale_t __base) { + return ::newlocale(__category_mask, __locale, __base); +} + +inline void __freelocale(__locale_t __loc) { ::freelocale(__loc); } + +inline char* __setlocale(int __category, char const* __locale) { return ::setlocale(__category, __locale); } + +inline __lconv_t* __localeconv(__locale_t& __loc) { + __locale_guard __current(__loc); + return ::localeconv(); +} +#endif // _LIBCPP_BUILDING_LIBRARY + +// +// Strtonum functions +// +inline float __strtof(const char* __nptr, char** __endptr, __locale_t) { return ::strtof(__nptr, __endptr); } + +inline double __strtod(const char* __nptr, char** __endptr, __locale_t) { return ::strtod(__nptr, __endptr); } + +inline long double __strtold(const char* __nptr, char** __endptr, __locale_t) { return ::strtold(__nptr, __endptr); } + +// +// Character manipulation functions +// +#if defined(_LIBCPP_BUILDING_LIBRARY) +inline int __toupper(int __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::toupper(__c); +} + +inline int __tolower(int __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::tolower(__c); +} + +inline int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) { + __locale_guard __current(__loc); + return ::strcoll(__s1, __s2); +} + +inline size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) { + __locale_guard __current(__loc); + return ::strxfrm(__dest, __src, __n); +} + +# if _LIBCPP_HAS_WIDE_CHARACTERS +inline int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswctype(__c, __type); } + +inline int __iswspace(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswspace(__c); } + +inline int __iswprint(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswprint(__c); } + +inline int __iswcntrl(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswcntrl(__c); } + +inline int __iswupper(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswupper(__c); } + +inline int __iswlower(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswlower(__c); } + +inline int __iswalpha(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswalpha(__c); } + +inline int __iswblank(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswblank(__c); } + +inline int __iswdigit(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswdigit(__c); } + +inline int __iswpunct(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswpunct(__c); } + +inline int __iswxdigit(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::iswxdigit(__c); } + +inline wint_t __towupper(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::towupper(__c); } + +inline wint_t __towlower(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return ::towlower(__c); } + +inline int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) { + __locale_guard __current(__loc); + return ::wcscoll(__ws1, __ws2); +} + +inline size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) { + __locale_guard __current(__loc); + return ::wcsxfrm(__dest, __src, __n); +} +# endif // _LIBCPP_HAS_WIDE_CHARACTERS + +inline _LIBCPP_HIDE_FROM_ABI const char* __get_locale_encoding(__locale_t __loc) { + return ::nl_langinfo_l(CODESET, __loc); +} + +inline _LIBCPP_ATTRIBUTE_FORMAT(__strftime__, 3, 0) size_t + __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) { + __locale_guard __current(__loc); + return ::strftime(__s, __max, __format, __tm); +} + +// +// Other functions +// +inline decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) { + __locale_guard __current(__loc); + return MB_CUR_MAX; +} + +# if _LIBCPP_HAS_WIDE_CHARACTERS +inline wint_t __btowc(int __c, __locale_t __loc) { + __locale_guard __current(__loc); + return btowc(__c); +} + +inline int __wctob(wint_t __c, __locale_t __loc) { + __locale_guard __current(__loc); + return wctob(__c); +} + +inline size_t +__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) { + __locale_guard __current(__loc); + return wcsnrtombs(__dest, __src, __nwc, __len, __ps); // wcsnrtombs is a POSIX extension +} + +inline size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) { + __locale_guard __current(__loc); + return wcrtomb(__s, __wc, __ps); +} + +inline size_t +__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) { + __locale_guard __current(__loc); + return mbsnrtowcs(__dest, __src, __nms, __len, __ps); // mbsnrtowcs is a POSIX extension +} + +inline size_t __mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { + __locale_guard __current(__loc); + return mbrtowc(__pwc, __s, __n, __ps); +} + +inline int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) { + __locale_guard __current(__loc); + return mbtowc(__pwc, __pmb, __max); +} + +inline size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { + __locale_guard __current(__loc); + return mbrlen(__s, __n, __ps); +} + +inline size_t __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) { + __locale_guard __current(__loc); + return mbsrtowcs(__dest, __src, __len, __ps); +} +# endif // _LIBCPP_HAS_WIDE_CHARACTERS +#endif // _LIBCPP_BUILDING_LIBRARY + +_LIBCPP_DIAGNOSTIC_PUSH +_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat") +_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates +#ifdef _LIBCPP_COMPILER_CLANG_BASED +# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__) +#else +# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */ +#endif + +template +_LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) +int __snprintf(char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) { + __locale_guard __current(__loc); + return ::snprintf(__s, __n, __format, std::forward<_Args>(__args)...); +} + +template +_LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) +int __asprintf(char** __s, __locale_t __loc, const char* __format, _Args&&... __args) { + __locale_guard __current(__loc); + return ::asprintf(__s, __format, std::forward<_Args>(__args)...); // non-standard +} +_LIBCPP_DIAGNOSTIC_POP +#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT + +} // namespace __locale +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_SERENITY_H diff --git a/lib/libcxxabi/include/cxxabi.h b/lib/libcxxabi/include/cxxabi.h index 7b88011c4234e6fc92577a92f2a1810c16ceb55b..51676a2b5ae238fd8aae2c25881a09b36c7c85a0 100644 --- a/lib/libcxxabi/include/cxxabi.h +++ b/lib/libcxxabi/include/cxxabi.h @@ -180,7 +180,8 @@ extern _LIBCXXABI_FUNC_VIS bool __cxa_uncaught_exception() _LIBCXXABI_NOEXCEPT; extern _LIBCXXABI_FUNC_VIS unsigned int __cxa_uncaught_exceptions() _LIBCXXABI_NOEXCEPT; // zig patch: haiku support -#if defined(__linux__) || defined(__Fuchsia__) || defined(__HAIKU__) +// zig patch: serenity support +#if defined(__linux__) || defined(__Fuchsia__) || defined(__HAIKU__) || defined(__serenity__) // Linux and Fuchsia TLS support. Not yet an official part of the Itanium ABI. // https://sourceware.org/glibc/wiki/Destructor%20support%20for%20thread_local%20variables extern _LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(void (*)(void *), void *, diff --git a/lib/libcxxabi/src/cxa_thread_atexit.cpp b/lib/libcxxabi/src/cxa_thread_atexit.cpp index bf3351f864c7257f6c714c78b5c7f61bb2d50463..4ece5c761bf04df03585c64b9fc0fffc87d35b5e 100644 --- a/lib/libcxxabi/src/cxa_thread_atexit.cpp +++ b/lib/libcxxabi/src/cxa_thread_atexit.cpp @@ -107,7 +107,8 @@ namespace { #endif // HAVE___CXA_THREAD_ATEXIT_IMPL // zig patch: haiku support -#if defined(__linux__) || defined(__Fuchsia__) || defined(__HAIKU__) +// zig patch: serenity support +#if defined(__linux__) || defined(__Fuchsia__) || defined(__HAIKU__) || defined(__serenity__) extern "C" { _LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() { -- 2.54.0