[libcxx-commits] [libcxx] [libc++] Support using Newlib as libc with locale support. (PR #167962)

Mr. Walls via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 4 15:07:23 PST 2025


================
@@ -118,6 +118,8 @@
 #    include <__locale_dir/support/fuchsia.h>
 #  elif defined(__linux__)
 #    include <__locale_dir/support/linux.h>
+#  elif _LIBCPP_LIBC_NEWLIB
+#    include <__locale_dir/support/newlib.h>
----------------
reactive-firewall wrote:

@wecing 

> However, I recently realized that the -Dxxx approach might actually be okay, because it just exposes things that are compiled and included in libc.a anyways.

* Regarding bare-metal I generally agree;
  * TL;DR - the `__linux__` flag just declares the presence of the linux headers, which may or may not be relevant to the libc (which by standard (e.g. ISO 9989:<any version> ) has no requirement on the linux headers, e.g., musl and BSD libc can compile without the linux headers) so baremetal builds should not expect it to describe libc.
  
* It seems to me like we might be checking the wrong thing by checking `elif defined(__linux__)` above, perhaps what we want is `elif defined(_GLIBC)`?
  * IIUC newlib has its requirements on a small subset of OS provided functions (with minimal implementations suggested in the docs), and is well suited to building without the `__linux__` being defined when bootstraping a stage0 toolset, (possibly even for an eventual linux system). AFAIK, newlib does seem to provide the `strto`*`_l` functions unconditionally (albeit not sure where in their codebase :shrug:)
  * musl libc has no requirement on linux headers even when building for linux, rather they guard things based on macros like `_GNU_SOURCE` or `_BSD_SOURCE`
  * IIUC glibc defines __linux__ somewhere (I know GNU compilers pre-define it, albeit GNU lib != GNU compiler), so while misleading, may be correct in the specific case for identifying "official" (e.g. non-llvm) linux builds. However, `_GLIBC` would be more intuitive and granular for identifying the particular standard c library (e.g. glibc, stdlibc, libc, libsystem_c etc.)


In summation: `elif defined(_GLIBC)` seems more granular and maintainable regarding near-baremetal (e.g. just minimal libc) use-cases.

:bow: Hopefully this helps.

https://github.com/llvm/llvm-project/pull/167962


More information about the libcxx-commits mailing list