[libcxx-commits] [libcxx] [libc++] Fix the locale base API on Linux with musl (PR #167980)
Mr. Walls via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 4 14:04:31 PST 2025
reactive-firewall wrote:
> That's what I have in mind, and I believe what @philnik777 also had in mind:
>
> ```diff
> diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
> index 85a2f8ac8ec4..2919f16adb91 100644
> --- a/libcxx/include/CMakeLists.txt
> +++ b/libcxx/include/CMakeLists.txt
> @@ -532,6 +532,7 @@ set(files
> __locale_dir/support/freebsd.h
> __locale_dir/support/fuchsia.h
> __locale_dir/support/linux.h
> + __locale_dir/support/musl.h
> __locale_dir/support/netbsd.h
> __locale_dir/support/no_locale/characters.h
> __locale_dir/support/no_locale/strtonum.h
> diff --git a/libcxx/include/__locale_dir/locale_base_api.h b/libcxx/include/__locale_dir/locale_base_api.h
> index d26d529d4e0c..f9d011782543 100644
> --- a/libcxx/include/__locale_dir/locale_base_api.h
> +++ b/libcxx/include/__locale_dir/locale_base_api.h
> @@ -116,6 +116,8 @@
> # include <__locale_dir/support/windows.h>
> # elif defined(__Fuchsia__)
> # include <__locale_dir/support/fuchsia.h>
> +# elif defined(_LIBCPP_HAS_MUSL_LIBC)
> +# include <__locale_dir/support/musl.h>
> # elif defined(__linux__)
> # include <__locale_dir/support/linux.h>
> # else
> diff --git a/libcxx/include/__locale_dir/support/musl.h b/libcxx/include/__locale_dir/support/musl.h
> new file mode 100644
> index 000000000000..e21046b7ca6d
> --- /dev/null
> +++ b/libcxx/include/__locale_dir/support/musl.h
> @@ -0,0 +1,42 @@
> +//===-----------------------------------------------------------------------===//
> +//
> +// 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_SUPPORT_MUSL_H
> +#define _LIBCPP___LOCALE_DIR_SUPPORT_MUSL_H
> +
> +#include <__config>
> +#include <__cstddef/size_t.h>
> +#include <__std_mbstate_t.h>
> +#include <__utility/forward.h>
> +#include <clocale> // std::lconv
> +#include <cstdio>
> +#include <cstdlib>
> +#include <ctype.h>
> +#include <stdarg.h>
> +#include <string.h>
> +#include <time.h>
> +#if _LIBCPP_HAS_WIDE_CHARACTERS
> +# include <cwchar>
> +# include <wctype.h>
> +#endif
> +
> +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
> +# pragma GCC system_header
> +#endif
> +
> +_LIBCPP_BEGIN_NAMESPACE_STD
> +namespace __locale {
> +
> +//
> +// Take inspiration from libcxx/include/__locale_dir/support/linux.h
> +//
> +
> +} // namespace __locale
> +_LIBCPP_END_NAMESPACE_STD
> +
> +#endif // _LIBCPP___LOCALE_DIR_SUPPORT_MUSL_H
> diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
> index 0ac5a1ade817..59249ec41d40 100644
> --- a/libcxx/include/module.modulemap.in
> +++ b/libcxx/include/module.modulemap.in
> @@ -1583,6 +1583,7 @@ module std [system] {
> textual header "__locale_dir/support/freebsd.h"
> textual header "__locale_dir/support/fuchsia.h"
> textual header "__locale_dir/support/linux.h"
> + textual header "__locale_dir/support/musl.h"
> textual header "__locale_dir/support/netbsd.h"
> textual header "__locale_dir/support/no_locale/characters.h"
> textual header "__locale_dir/support/no_locale/strtonum.h"
> ```
This would not resolve GHI llvm/llvm-project#167977 as the `__locale_dir/support/linux.h` still would report errors such as:
```console
#59 15.01 In file included from /bootstrap/llvmorg/libcxx/src/chrono.cpp:17:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/chrono:1058:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__chrono/exception.h:25:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/format:202:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__format/container_adaptor.h:20:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__format/range_default_formatter.h:23:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__format/range_formatter.h:23:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__format/format_context.h:28:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale:17:
#59 15.01 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/locale_base_api.h:123:
#59 15.01 /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/support/linux.h:86:12: error: no member named 'strtof_l' in the global namespace
#59 15.01 86 | return ::strtof_l(__nptr, __endptr, __loc);
#59 15.01 | ~~^
#59 15.01 /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/support/linux.h:90:12: error: no member named 'strtod_l' in the global namespace
#59 15.01 90 | return ::strtod_l(__nptr, __endptr, __loc);
#59 15.01 | ~~^
#59 15.01 /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/support/linux.h:94:12: error: no member named 'strtold_l' in the global namespace
#59 15.01 94 | return ::strtold_l(__nptr, __endptr, __loc);
#59 15.01 | ~~^
#59 15.01 3 errors generated.
#59 15.70 [1733/1963] Building CXX object libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/filesystem_error.cpp.o
#59 15.70 FAILED: libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/filesystem_error.cpp.o
```
and
```console
#59 15.94 In file included from /bootstrap/llvmorg/libcxx/src/filesystem/path.cpp:10:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/filesystem:543:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__filesystem/directory_entry.h:19:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__filesystem/filesystem_error.h:14:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__filesystem/path.h:31:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/iomanip:53:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/money.h:17:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale:17:
#59 15.94 In file included from /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/locale_base_api.h:123:
#59 15.94 /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/support/linux.h:86:12: error: no member named 'strtof_l' in the global namespace
#59 15.94 86 | return ::strtof_l(__nptr, __endptr, __loc);
#59 15.94 | ~~^
#59 15.94 /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/support/linux.h:90:12: error: no member named 'strtod_l' in the global namespace
#59 15.94 90 | return ::strtod_l(__nptr, __endptr, __loc);
#59 15.94 | ~~^
#59 15.94 /bootstrap/llvmorg/build-runtimes/include/c++/v1/__locale_dir/support/linux.h:94:12: error: no member named 'strtold_l' in the global namespace
#59 15.94 94 | return ::strtold_l(__nptr, __endptr, __loc);
#59 15.94 | ~~^
#59 15.94 3 errors generated.
#59 15.94 [1735/1963] Building CXX object libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/filesystem_clock.cpp.o
#59 15.94 FAILED: libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/filesystem_clock.cpp.o
```
etc.
I am not comfortable enough with `cmake` to suggest a solution but I generally agree with the overall approach being better; I am comfortable with the libcxx/include/__locale_dir/locale_base_api.h changes and libcxx/include/__locale_dir/support/musl.h implementation based on the linux.h/freebsd.h for musl libc) but then the glibc.h idea seems more relevant. Should I just duplicate the common code in linux.h for musl.h for now and leave the glibc part undone? I don't want to cause regressions. PLEASE ADVISE.
:bow: also thanks for all of the feedback and time on this.
https://github.com/llvm/llvm-project/pull/167980
More information about the libcxx-commits
mailing list