[libcxx-commits] [libcxx] [libc++] Don't include `<langinfo.h>` for macOS in locale base API (PR #206163)
William Tran-Viet via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jun 26 12:48:59 PDT 2026
https://github.com/smallp-o-p created https://github.com/llvm/llvm-project/pull/206163
- The current method broke certain builds on macOS. e.g. [Green Dragon](https://ci.swift.org/job/llvm.org/view/LLDB/job/lldb-cmake/22413/) due to a circular dependency introduced by `<langinfo.h>`.
- As an unfortunate workaround: Don't include it and subsequently unimplement `__get_locale_encoding()` for Apple, and use `nl_langinfo_l` directly in `text_encoding.cpp`
>From ae86e84d6e7895d6ad7501c544e62152fa38bdbe Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Fri, 26 Jun 2026 15:44:58 -0400
Subject: [PATCH] Unbreak Apple Clang modules builds
---
libcxx/include/__locale_dir/support/bsd_like.h | 6 +++++-
libcxx/src/text_encoding.cpp | 14 ++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/libcxx/include/__locale_dir/support/bsd_like.h b/libcxx/include/__locale_dir/support/bsd_like.h
index 6632ca51e1eaa..11400dac98cbc 100644
--- a/libcxx/include/__locale_dir/support/bsd_like.h
+++ b/libcxx/include/__locale_dir/support/bsd_like.h
@@ -15,7 +15,9 @@
#include <__utility/forward.h>
#include <clocale> // std::lconv
#include <ctype.h>
-#include <langinfo.h>
+#ifndef __APPLE__
+# include <langinfo.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -182,9 +184,11 @@ __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps,
}
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
+# ifndef __APPLE__
inline _LIBCPP_HIDE_FROM_ABI const char* __get_locale_encoding(__locale_t __loc) {
return ::nl_langinfo_l(CODESET, __loc);
}
+# endif
#endif // _LIBCPP_BUILDING_LIBRARY
_LIBCPP_DIAGNOSTIC_PUSH
diff --git a/libcxx/src/text_encoding.cpp b/libcxx/src/text_encoding.cpp
index 0804c3457f1e6..4e26255c5bc6b 100644
--- a/libcxx/src/text_encoding.cpp
+++ b/libcxx/src/text_encoding.cpp
@@ -11,6 +11,12 @@
#include <__utility/scope_guard.h>
#include <text_encoding>
+// FIXME: Including <langinfo.h> in the locale base API in Clang modules on Apple introduces a circular dependency and
+// breaks certain builds.
+#if defined(__APPLE__)
+# include <langinfo.h>
+#endif
+
_LIBCPP_BEGIN_NAMESPACE_STD
_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
@@ -31,7 +37,11 @@ static text_encoding __make_text_encoding(const char* __name) {
std::text_encoding __get_locale_encoding(const char* __name) {
if (__name == nullptr)
+# if defined(__APPLE__)
+ return __make_text_encoding(::nl_langinfo_l(CODESET, static_cast<::__locale::__locale_t>(0)));
+# else
return __make_text_encoding(__locale::__get_locale_encoding(static_cast<__locale::__locale_t>(0)));
+# endif
__locale::__locale_t __l = __locale::__newlocale(_LIBCPP_CTYPE_MASK, __name, static_cast<__locale::__locale_t>(0));
@@ -45,7 +55,11 @@ std::text_encoding __get_locale_encoding(const char* __name) {
return text_encoding{};
}
+# if defined(__APPLE__)
+ return __make_text_encoding(::nl_langinfo_l(CODESET, __l));
+# else
return __make_text_encoding(__locale::__get_locale_encoding(__l));
+# endif
}
#endif // __ANDROID__
More information about the libcxx-commits
mailing list