Skip to content

Commit a6643f2

Browse files
voltur01ldionne
andauthored
[libc++] Add support for picolibc and newlib in RUNTIMES_USE_LIBC (#147956)
This replaces detection of picolibc in libc++ (by checking for and including picolibc.h) with using RUNTIMES_USE_LIBC build time option intriduced in llvm/llvm-project#134893 RUNTIMES_USE_LIBC is extended to accept picolibc and newlib. Detection of picolibc via the header is kept as a deprecated feature to avoid breaking builds. libc++ is updated to use dedicated LIBCXX_LIBC_NEWLIB macro to check for newlib specific conditions instead of less informative _NEWLIB_VERSION --------- Co-authored-by: Louis Dionne <[email protected]>
1 parent ae58ae1 commit a6643f2

File tree

22 files changed

+40
-34
lines changed

22 files changed

+40
-34
lines changed

libcxx/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,18 @@ config_define(${LIBCXX_ENABLE_WIDE_CHARACTERS} _LIBCPP_HAS_WIDE_CHARACTERS)
763763
config_define(${LIBCXX_ENABLE_TIME_ZONE_DATABASE} _LIBCPP_HAS_TIME_ZONE_DATABASE)
764764
config_define(${LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS} _LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS)
765765

766+
# Set C library in use
767+
if (RUNTIMES_USE_LIBC STREQUAL "picolibc")
768+
config_define(1 _LIBCPP_LIBC_PICOLIBC)
769+
# picolibc is derived from newlib and behaves the same in regards to libc++
770+
# so setting both here:
771+
# * _LIBCPP_LIBC_NEWLIB is used now
772+
# * _LIBCPP_LIBC_PICOLIBC can be used for further customizations later
773+
config_define(1 _LIBCPP_LIBC_NEWLIB)
774+
elseif (RUNTIMES_USE_LIBC STREQUAL "newlib")
775+
config_define(1 _LIBCPP_LIBC_NEWLIB)
776+
endif()
777+
766778
# TODO: Remove in LLVM 21. We're leaving an error to make this fail explicitly.
767779
if (LIBCXX_ENABLE_ASSERTIONS)
768780
message(FATAL_ERROR "LIBCXX_ENABLE_ASSERTIONS has been removed. Please use LIBCXX_HARDENING_MODE instead.")

libcxx/include/__config_site.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#cmakedefine _LIBCPP_HARDENING_MODE_DEFAULT @_LIBCPP_HARDENING_MODE_DEFAULT@
4343
#cmakedefine _LIBCPP_ASSERTION_SEMANTIC_DEFAULT @_LIBCPP_ASSERTION_SEMANTIC_DEFAULT@
4444

45+
// C libraries
46+
#cmakedefine01 _LIBCPP_LIBC_PICOLIBC
47+
#cmakedefine01 _LIBCPP_LIBC_NEWLIB
48+
4549
// __USE_MINGW_ANSI_STDIO gets redefined on MinGW
4650
#ifdef __clang__
4751
# pragma clang diagnostic push

libcxx/include/__configuration/platform.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@
4242
# define _LIBCPP_GLIBC_PREREQ(a, b) 0
4343
#endif
4444

45-
// This is required in order for _NEWLIB_VERSION to be defined in places where we use it.
46-
// TODO: We shouldn't be including arbitrarily-named headers from libc++ since this can break valid
47-
// user code. Move code paths that need _NEWLIB_VERSION to another customization mechanism.
48-
#if __has_include(<picolibc.h>)
49-
# include <picolibc.h>
50-
#endif
51-
5245
#ifndef __BYTE_ORDER__
5346
# error \
5447
"Your compiler doesn't seem to define __BYTE_ORDER__, which is required by libc++ to know the endianness of your target platform"

libcxx/include/__cxx03/__fwd/ios.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using wios = basic_ios<wchar_t>;
3131
template <class _CharT, class _Traits>
3232
class _LIBCPP_PREFERRED_NAME(ios) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wios)) basic_ios;
3333

34-
#if defined(_NEWLIB_VERSION)
34+
#if _LIBCPP_LIBC_NEWLIB
3535
// On newlib, off_t is 'long int'
3636
using streamoff = long int; // for char_traits in <string>
3737
#else

libcxx/include/__cxx03/__locale

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public:
384384
static const mask xdigit = _ISXDIGIT;
385385
static const mask blank = _ISBLANK;
386386
static const mask __regex_word = 0x8000;
387-
#elif defined(_NEWLIB_VERSION)
387+
#elif _LIBCPP_LIBC_NEWLIB
388388
// Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
389389
typedef char mask;
390390
// In case char is signed, static_cast is needed to avoid warning on

libcxx/include/__cxx03/__locale_dir/locale_base_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# include <__cxx03/__locale_dir/locale_base_api/android.h>
1818
#elif defined(__sun__)
1919
# include <__cxx03/__locale_dir/locale_base_api/solaris.h>
20-
#elif defined(_NEWLIB_VERSION)
20+
#elif _LIBCPP_LIBC_NEWLIB
2121
# include <__cxx03/__locale_dir/locale_base_api/newlib.h>
2222
#elif defined(__OpenBSD__)
2323
# include <__cxx03/__locale_dir/locale_base_api/openbsd.h>

libcxx/include/__cxx03/fstream

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ typedef basic_fstream<wchar_t> wfstream;
210210
_LIBCPP_PUSH_MACROS
211211
#include <__cxx03/__undef_macros>
212212

213-
#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
213+
#if defined(_LIBCPP_MSVCRT) || _LIBCPP_LIBC_NEWLIB
214214
# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
215215
#endif
216216

libcxx/include/__cxx03/locale

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ template <class charT> class messages_byname;
220220

221221
# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
222222
// Most unix variants have catopen. These are the specific ones that don't.
223-
# if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
223+
# if !defined(__BIONIC__) && !_LIBCPP_LIBC_NEWLIB && !defined(__EMSCRIPTEN__)
224224
# define _LIBCPP_HAS_CATOPEN 1
225225
# include <nl_types.h>
226226
# endif

libcxx/include/__cxx03/regex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ public:
984984
typedef _CharT char_type;
985985
typedef basic_string<char_type> string_type;
986986
typedef locale locale_type;
987-
#if defined(__BIONIC__) || defined(_NEWLIB_VERSION)
987+
#if defined(__BIONIC__) || _LIBCPP_LIBC_NEWLIB
988988
// Originally bionic's ctype_base used its own ctype masks because the
989989
// builtin ctype implementation wasn't in libc++ yet. Bionic's ctype mask
990990
// was only 8 bits wide and already saturated, so it used a wider type here
@@ -993,9 +993,7 @@ public:
993993
// implementation, but this was not updated to match. Since then Android has
994994
// needed to maintain a stable libc++ ABI, and this can't be changed without
995995
// an ABI break.
996-
// We also need this workaround for newlib since _NEWLIB_VERSION is not
997-
// defined yet inside __config, so we can't set the
998-
// _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE macro. Additionally, newlib is
996+
// We also need this workaround for newlib since newlib is
999997
// often used for space constrained environments, so it makes sense not to
1000998
// duplicate the ctype table.
1001999
typedef uint16_t char_class_type;

libcxx/include/__fwd/ios.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using wios = basic_ios<wchar_t>;
3131
template <class _CharT, class _Traits>
3232
class _LIBCPP_PREFERRED_NAME(ios) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wios)) basic_ios;
3333

34-
#if defined(_NEWLIB_VERSION)
34+
#if _LIBCPP_LIBC_NEWLIB
3535
// On newlib, off_t is 'long int'
3636
using streamoff = long int; // for char_traits in <string>
3737
#else

0 commit comments

Comments
 (0)