From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Daniel Bertalan <dani@danielbertalan.dev>
Date: Thu, 14 Apr 2022 10:17:13 +0200
Subject: [PATCH] [libcxx] Add support for Minerva

This commit teaches libc++ about what features are available in our
LibC, namely:
* We do not have locale support, so no-op shims should be used in place
  of the C locale API.
* The number of errno constants defined by us is given by the value of
  the `ELAST` macro.
* Multithreading is implemented though the pthread library.
* Use libc++'s builtin character type table instead of the one provided
  by LibC as there's a lot of extra porting work to convince the rest of
  locale.cpp to use our character type table properly.
---
 libcxx/include/CMakeLists.txt                 |  1 +
 libcxx/include/__config                       |  5 +++--
 .../__cxx03/__support/ibm/locale_mgmt_zos.h   |  2 +-
 libcxx/include/__locale_dir/locale_base_api.h |  2 ++
 .../__locale_dir/locale_base_api/minerva.h   | 22 +++++++++++++++++++
 .../include/__support/ibm/locale_mgmt_zos.h   |  2 +-
 libcxx/include/locale                         |  2 +-
 libcxx/src/CMakeLists.txt                     |  4 ++++
 libcxx/src/include/config_elast.h             |  2 ++
 libcxx/src/support/ibm/xlocale_zos.cpp        |  2 ++
 10 files changed, 39 insertions(+), 5 deletions(-)
 create mode 100644 libcxx/include/__locale_dir/locale_base_api/minerva.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 8dac823503d73f51d5fb23e79f9f880a7580887b..2c02534f0151c3f11543235c8f055513c4ce747b 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -506,6 +506,7 @@ set(files
   __locale_dir/locale_base_api/ibm.h
   __locale_dir/locale_base_api/musl.h
   __locale_dir/locale_base_api/openbsd.h
+  __locale_dir/locale_base_api/minerva.h
   __locale_dir/pad_and_output.h
   __locale_dir/support/apple.h
   __locale_dir/support/bsd_like.h
diff --git a/libcxx/include/__config b/libcxx/include/__config
index a866a7e6518379a48e85027a1f77956045ffc6e8..5b8bc958cd267d88bafef5a522356b0870f21a21 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -825,7 +825,8 @@ typedef __char32_t char32_t;
         defined(__APPLE__) ||                                                                                          \
         defined(__MVS__) ||                                                                                            \
         defined(_AIX) ||                                                                                               \
-        defined(__EMSCRIPTEN__)
+        defined(__EMSCRIPTEN__) ||                                                                                     \
+        defined(__minerva__)
 // clang-format on
 #      undef _LIBCPP_HAS_THREAD_API_PTHREAD
 #      define _LIBCPP_HAS_THREAD_API_PTHREAD 1
@@ -908,7 +909,7 @@ typedef __char32_t char32_t;
 #  endif
 
 #  if defined(__BIONIC__) || defined(__NuttX__) || defined(__Fuchsia__) || defined(__wasi__) ||                        \
-      _LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || defined(__LLVM_LIBC__)
+      _LIBCPP_HAS_MUSL_LIBC || defined(__OpenBSD__) || defined(__LLVM_LIBC__) || defined(__minerva__)
 #    define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
 #  endif
 
diff --git a/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h b/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h
index 9fa8f8c9088e1fc42408dca5b8d046668a0fc101..2a9cbec71ea6e6692152cf787891762c39f65625 100644
--- a/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h
+++ b/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h
@@ -10,7 +10,7 @@
 #ifndef _LIBCPP___CXX03___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
 #define _LIBCPP___CXX03___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
 
-#if defined(__MVS__)
+#if defined(__MVS__) || defined(__minerva__)
 #  include <__cxx03/locale.h>
 #  include <__cxx03/string>
 
diff --git a/libcxx/include/__locale_dir/locale_base_api.h b/libcxx/include/__locale_dir/locale_base_api.h
index c1e73caeecced96b6472c57f91d94bc6d535d589..12611341eb8e74c22942e30790d571e0100db053 100644
--- a/libcxx/include/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__locale_dir/locale_base_api.h
@@ -133,6 +133,8 @@
 #    include <__locale_dir/locale_base_api/openbsd.h>
 #  elif defined(__wasi__) || _LIBCPP_HAS_MUSL_LIBC
 #    include <__locale_dir/locale_base_api/musl.h>
+#  elif defined(__minerva__)
+#    include <__locale_dir/locale_base_api/minerva.h>
 #  endif
 
 #  include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
diff --git a/libcxx/include/__locale_dir/locale_base_api/minerva.h b/libcxx/include/__locale_dir/locale_base_api/minerva.h
new file mode 100644
index 0000000000000000000000000000000000000000..13abb601dcb75d1459df0b943ef6ca3e911c8e46
--- /dev/null
+++ b/libcxx/include/__locale_dir/locale_base_api/minerva.h
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_LOCALE_BASE_API_MINERVA_H
+#define _LIBCPP___LOCALE_LOCALE_BASE_API_MINERVA_H
+
+#include <stddef.h>
+
+#include <__support/ibm/locale_mgmt_zos.h>
+#include <__support/xlocale/__posix_l_fallback.h>
+#include <__support/xlocale/__strtonum_fallback.h>
+#include <clocale>
+#include <cstdlib>
+#include <ctype.h>
+#include <cwctype>
+
+#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_MINERVA_H
diff --git a/libcxx/include/__support/ibm/locale_mgmt_zos.h b/libcxx/include/__support/ibm/locale_mgmt_zos.h
index 5fc04b6b4b2990c1a5f5b7fae083108c27e907b5..787cc04d57929537d73d871ffd10d1467c3565cf 100644
--- a/libcxx/include/__support/ibm/locale_mgmt_zos.h
+++ b/libcxx/include/__support/ibm/locale_mgmt_zos.h
@@ -10,7 +10,7 @@
 #ifndef _LIBCPP___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
 #define _LIBCPP___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
 
-#if defined(__MVS__)
+#if defined(__MVS__) || defined(__minerva__)
 #  include <locale.h>
 #  include <string>
 
diff --git a/libcxx/include/locale b/libcxx/include/locale
index 5af674d19414b45ef066cfeac9c9c69f85fab00d..3f8526776beb4c036232ab0786a512bfdd124cc8 100644
--- a/libcxx/include/locale
+++ b/libcxx/include/locale
@@ -224,7 +224,7 @@ template <class charT> class messages_byname;
 
 #    if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
 // Most unix variants have catopen.  These are the specific ones that don't.
-#      if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
+#      if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__) && !defined(__minerva__)
 #        define _LIBCPP_HAS_CATOPEN 1
 #        include <nl_types.h>
 #      else
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index 4e9bf900af4c53ba885be708482277eb97371c13..ea40148ae2784bfa8c6a6045440eb91d8ddb87b7 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -113,6 +113,10 @@ elseif(ZOS)
     support/ibm/wcsnrtombs.cpp
     support/ibm/xlocale_zos.cpp
     )
+elseif(MINERVA)
+  list(APPEND LIBCXX_SOURCES
+    support/ibm/xlocale_zos.cpp
+    )
 endif()
 
 if (LIBCXX_ENABLE_FILESYSTEM)
diff --git a/libcxx/src/include/config_elast.h b/libcxx/src/include/config_elast.h
index 7edff2d9375d46f465a1d72b916b1ccb1a555724..ba24e85be8d0a2b4502d5986377081c1bcd036ee 100644
--- a/libcxx/src/include/config_elast.h
+++ b/libcxx/src/include/config_elast.h
@@ -37,6 +37,8 @@
 #  define _LIBCPP_ELAST 4095
 #elif defined(__APPLE__)
 // No _LIBCPP_ELAST needed on Apple
+#elif defined(__minerva__)
+// No _LIBCPP_ELAST needed on Minerva
 #elif defined(__MVS__)
 #  define _LIBCPP_ELAST 1160
 #elif defined(_LIBCPP_MSVCRT_LIKE)
diff --git a/libcxx/src/support/ibm/xlocale_zos.cpp b/libcxx/src/support/ibm/xlocale_zos.cpp
index 136999ec0b02f8171bbd8d170aca2b68f7d65681..23a56c0f586728e37ee89bba608b1853983bcb94 100644
--- a/libcxx/src/support/ibm/xlocale_zos.cpp
+++ b/libcxx/src/support/ibm/xlocale_zos.cpp
@@ -7,7 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include <__assert>
+#ifndef __minerva__
 #include <__support/ibm/xlocale.h>
+#endif
 #include <sstream>
 #include <vector>
 
