Cherry-pick upstream r252457 Add initial support for the MUSL C library. Summary: This patch adds the LIBCXX_LIBC_IS_MUSL cmake option to allow the building of libcxx with the Musl C library. The option is necessary as Musl does not provide any predefined macro in order to test for its presence, like GLIBC. Most of the changes specify the correct path to choose through the various #if/#else constructs in the locale code. Depends on D13407. Reviewers: mclow.lists, jroelofs, EricWF Subscribers: jfb, tberghammer, danalbert, srhines, cfe-commits Differential Revision: http://reviews.llvm.org/D13673 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@252457 91177308-0d34-0410-b5e6-96231b3b80d8 BUG= https://code.google.com/p/nativeclient/issues/detail?id=4335 R=dschuff@chromium.org Review URL: https://codereview.chromium.org/1441603003 .
diff --git a/include/__config b/include/__config index 6f57df5..15af159 100644 --- a/include/__config +++ b/include/__config
@@ -118,6 +118,10 @@ # define _LIBCPP_USING_NACL_RANDOM #endif // defined(__native_client__) +#ifdef __musl__ +#define _LIBCPP_HAS_MUSL_LIBC +#endif // defined(__musl__) + #if !defined(_LIBCPP_LITTLE_ENDIAN) || !defined(_LIBCPP_BIG_ENDIAN) # include <endian.h> # if __BYTE_ORDER == __LITTLE_ENDIAN @@ -332,14 +336,19 @@ #elif defined(__ANDROID__) #define _LIBCPP_HAS_QUICK_EXIT #elif defined(__linux__) -#include <features.h> +#if !defined(_LIBCPP_HAS_MUSL_LIBC) +# include <features.h> #if __GLIBC_PREREQ(2, 15) #define _LIBCPP_HAS_QUICK_EXIT #endif #if __GLIBC_PREREQ(2, 17) #define _LIBCPP_HAS_C11_FEATURES #endif +#else // defined(_LIBCPP_HAS_MUSL_LIBC) +#define _LIBCPP_HAS_QUICK_EXIT +#define _LIBCPP_HAS_C11_FEATURES #endif +#endif // __linux__ #endif #if (__has_feature(cxx_noexcept))
diff --git a/include/__locale b/include/__locale index 03f1b52..e68a06d 100644 --- a/include/__locale +++ b/include/__locale
@@ -41,6 +41,8 @@ #elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) \ || defined(__EMSCRIPTEN__) || defined(__IBMCPP__)) # include <xlocale.h> +#elif defined(_LIBCPP_HAS_MUSL_LIBC) +# include <support/musl/xlocale.h> #endif // __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__ #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) @@ -333,7 +335,7 @@ class _LIBCPP_TYPE_VIS ctype_base { public: -#ifdef __GLIBC__ +#if defined(__GLIBC__) typedef unsigned short mask; static const mask space = _ISspace; static const mask print = _ISprint; @@ -362,7 +364,7 @@ typedef __uint32_t mask; #elif defined(__FreeBSD__) typedef unsigned long mask; -#elif defined(__EMSCRIPTEN__) || defined(__NetBSD__) +# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__) || defined(_LIBCPP_HAS_MUSL_LIBC) typedef unsigned short mask; #elif defined(__ANDROID__) typedef unsigned char mask; @@ -414,7 +416,11 @@ static const mask blank = _B; // @LOCALMOD-END #else // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ || __EMSCRIPTEN__ || __sun__ +#if defined(_LIBCPP_HAS_MUSL_LIBC) + typedef unsigned short mask; +#else typedef unsigned long mask; +#endif static const mask space = 1<<0; static const mask print = 1<<1; static const mask cntrl = 1<<2; @@ -636,7 +642,7 @@ #endif _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;} static const mask* classic_table() _NOEXCEPT; -#if defined(__GLIBC__) || defined(__EMSCRIPTEN__) +#if defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) static const int* __classic_upper_table() _NOEXCEPT; static const int* __classic_lower_table() _NOEXCEPT; #endif
diff --git a/include/locale b/include/locale index 0d01002..3affd2d 100644 --- a/include/locale +++ b/include/locale
@@ -235,7 +235,7 @@ // locale. Linux, not so much. The following functions avoid the locale when // that's possible and otherwise do the wrong thing. FIXME. #if defined(__linux__) || defined(__EMSCRIPTEN__) || defined(_AIX) || \ - defined(_NEWLIB_VERSION) + defined(_NEWLIB_VERSION) || defined(_LIBCPP_HAS_MUSL_LIBC) #ifdef _LIBCPP_LOCALE__L_EXTENSIONS decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
diff --git a/include/support/musl/xlocale.h b/include/support/musl/xlocale.h new file mode 100644 index 0000000..3e31c99 --- /dev/null +++ b/include/support/musl/xlocale.h
@@ -0,0 +1,58 @@ +// -*- C++ -*- +//===------------------- support/musl/xlocale.h ------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// This adds support for the extended locale functions that are currently +// missing from the Musl C library. +// +// This only works when the specified locale is "C" or "POSIX", but that's +// about as good as we can do without implementing full xlocale support +// in Musl. +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_SUPPORT_MUSL_XLOCALE_H +#define _LIBCPP_SUPPORT_MUSL_XLOCALE_H + +#include <cstdlib> +#include <cwchar> + +#ifdef __cplusplus +extern "C" { +#endif + +static inline long long strtoll_l(const char *nptr, char **endptr, int base, + locale_t) { + return strtoll(nptr, endptr, base); +} + +static inline unsigned long long strtoull_l(const char *nptr, char **endptr, + int base, locale_t) { + return strtoull(nptr, endptr, base); +} + +static inline long long wcstoll_l(const wchar_t *nptr, wchar_t **endptr, + int base, locale_t) { + return wcstoll(nptr, endptr, base); +} + +static inline unsigned long long wcstoull_l(const wchar_t *nptr, + wchar_t **endptr, int base, + locale_t) { + return wcstoull(nptr, endptr, base); +} + +static inline long double wcstold_l(const wchar_t *nptr, wchar_t **endptr, + locale_t) { + return wcstold(nptr, endptr); +} + +#ifdef __cplusplus +} +#endif + +#endif // _LIBCPP_SUPPORT_MUSL_XLOCALE_H
diff --git a/src/locale.cpp b/src/locale.cpp index 191b239..de13d79 100644 --- a/src/locale.cpp +++ b/src/locale.cpp
@@ -810,7 +810,8 @@ { #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c; -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \ + defined(__NetBSD__) || defined(_LIBCPP_HAS_MUSL_LIBC) return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c; #else return (isascii(c) && iswlower_l(c, __cloc())) ? c-L'a'+L'A' : c; @@ -823,7 +824,8 @@ for (; low != high; ++low) #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low; -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \ + defined(__NetBSD__) || defined(_LIBCPP_HAS_MUSL_LIBC) *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low] : *low; #else @@ -837,7 +839,8 @@ { #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c; -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \ + defined(__NetBSD__) || defined(_LIBCPP_HAS_MUSL_LIBC) return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c; #else return (isascii(c) && isupper_l(c, __cloc())) ? c-L'A'+'a' : c; @@ -850,7 +853,8 @@ for (; low != high; ++low) #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low; -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \ + defined(__NetBSD__) || defined(_LIBCPP_HAS_MUSL_LIBC) *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low] : *low; #else @@ -919,8 +923,8 @@ static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c; #elif defined(__NetBSD__) return static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]); -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) - return isascii(c) ? +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) + return isascii(c) ? static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]) : c; #else return (isascii(c) && islower_l(c, __cloc())) ? c-'a'+'A' : c; @@ -936,7 +940,7 @@ static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low; #elif defined(__NetBSD__) *low = static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(*low)]); -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) *low = isascii(*low) ? static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low; #else @@ -953,7 +957,7 @@ static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c; #elif defined(__NetBSD__) return static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(c)]); -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) return isascii(c) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c; #else @@ -969,7 +973,7 @@ *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low; #elif defined(__NetBSD__) *low = static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(*low)]); -#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) +#elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) *low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low; #else *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-'A'+'a' : *low; @@ -1010,7 +1014,7 @@ return low; } -#ifdef __EMSCRIPTEN__ +#if defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) extern "C" const unsigned short ** __ctype_b_loc(); extern "C" const int ** __ctype_tolower_loc(); extern "C" const int ** __ctype_toupper_loc(); @@ -1031,7 +1035,7 @@ return _ctype+1; // internal ctype mask table defined in msvcrt.dll // This is assumed to be safe, which is a nonsense assumption because we're // going to end up dereferencing it later... -#elif defined(__EMSCRIPTEN__) +#elif defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) return *__ctype_b_loc(); #elif defined(_NEWLIB_VERSION) // @LOCALMOD-START // Newlib has a 257-entry table in ctype_.c, where (char)0 starts at [1]. @@ -1076,7 +1080,7 @@ return _C_toupper_tab_ + 1; } -#elif defined(__EMSCRIPTEN__) +#elif defined(__EMSCRIPTEN__) || defined(_LIBCPP_HAS_MUSL_LIBC) const int* ctype<char>::__classic_lower_table() _NOEXCEPT { @@ -1088,7 +1092,7 @@ { return *__ctype_toupper_loc(); } -#endif // __GLIBC__ || __EMSCRIPTEN__ || __NETBSD__ +#endif // __GLIBC__ || __NETBSD__ || __EMSCRIPTEN__ || defined(_LIBCPP_HAS_MUSL_LIBC) // template <> class ctype_byname<char>