[libcxx-commits] [libcxx] Get cxx03 headers working on z/OS (PR #132992)
Sean Perry via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Mar 25 14:02:41 PDT 2025
https://github.com/perry-ca created https://github.com/llvm/llvm-project/pull/132992
The support headers for z/OS essentially provide functions not currently available from the operating system. They are independent of libcxx. We want both flavours of libcxx to be using the same support code.
The headers were duplicated when the libc++ headers were frozen for c++03. Rather than requiring dual maintenance of these headers just use a single version.
I also moved the gettod.h header into the src dir since the only use of this function is within the shared lib and the function isn't exported.
>From f87f19b48a55c637d31bd5c839b9ee70d9a151af Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 25 Mar 2025 16:07:45 -0400
Subject: [PATCH] Get cxx03 headers working on z/OS
---
libcxx/include/CMakeLists.txt | 5 -
.../__cxx03/__locale_dir/locale_base_api.h | 2 +-
.../__locale_dir/locale_base_api/ibm.h | 108 ------------------
.../__cxx03/__support/ibm/gettod_zos.h | 52 ---------
.../__cxx03/__support/ibm/locale_mgmt_zos.h | 53 ---------
.../include/__cxx03/__support/ibm/nanosleep.h | 55 ---------
.../__cxx03/__thread/support/pthread.h | 2 +-
libcxx/src/chrono.cpp | 2 +-
.../support}/ibm/gettod_zos.h | 0
9 files changed, 3 insertions(+), 276 deletions(-)
delete mode 100644 libcxx/include/__cxx03/__locale_dir/locale_base_api/ibm.h
delete mode 100644 libcxx/include/__cxx03/__support/ibm/gettod_zos.h
delete mode 100644 libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h
delete mode 100644 libcxx/include/__cxx03/__support/ibm/nanosleep.h
rename libcxx/{include/__support => src/support}/ibm/gettod_zos.h (100%)
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index a021b9bb44d67..6ad637787e06f 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -732,7 +732,6 @@ set(files
__string/char_traits.h
__string/constexpr_c_functions.h
__string/extern_template_lists.h
- __support/ibm/gettod_zos.h
__support/ibm/locale_mgmt_zos.h
__support/ibm/nanosleep.h
__support/xlocale/__nop_locale_mgmt.h
@@ -1562,7 +1561,6 @@ set(files
__cxx03/__locale_dir/locale_base_api/bsd_locale_defaults.h
__cxx03/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
__cxx03/__locale_dir/locale_base_api/fuchsia.h
- __cxx03/__locale_dir/locale_base_api/ibm.h
__cxx03/__locale_dir/locale_base_api/locale_guard.h
__cxx03/__locale_dir/locale_base_api/musl.h
__cxx03/__locale_dir/locale_base_api/newlib.h
@@ -1765,9 +1763,6 @@ set(files
__cxx03/__string/char_traits.h
__cxx03/__string/constexpr_c_functions.h
__cxx03/__string/extern_template_lists.h
- __cxx03/__support/ibm/gettod_zos.h
- __cxx03/__support/ibm/locale_mgmt_zos.h
- __cxx03/__support/ibm/nanosleep.h
__cxx03/__support/xlocale/__nop_locale_mgmt.h
__cxx03/__support/xlocale/__posix_l_fallback.h
__cxx03/__support/xlocale/__strtonum_fallback.h
diff --git a/libcxx/include/__cxx03/__locale_dir/locale_base_api.h b/libcxx/include/__cxx03/__locale_dir/locale_base_api.h
index a20f0952f52c3..03f24630af6ca 100644
--- a/libcxx/include/__cxx03/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__cxx03/__locale_dir/locale_base_api.h
@@ -12,7 +12,7 @@
#if defined(_LIBCPP_MSVCRT_LIKE)
# include <__cxx03/__locale_dir/locale_base_api/win32.h>
#elif defined(_AIX) || defined(__MVS__)
-# include <__cxx03/__locale_dir/locale_base_api/ibm.h>
+# include <__locale_dir/locale_base_api/ibm.h>
#elif defined(__ANDROID__)
# include <__cxx03/__locale_dir/locale_base_api/android.h>
#elif defined(__sun__)
diff --git a/libcxx/include/__cxx03/__locale_dir/locale_base_api/ibm.h b/libcxx/include/__cxx03/__locale_dir/locale_base_api/ibm.h
deleted file mode 100644
index 0fab9d24d84b6..0000000000000
--- a/libcxx/include/__cxx03/__locale_dir/locale_base_api/ibm.h
+++ /dev/null
@@ -1,108 +0,0 @@
-// -*- C++ -*-
-//===-----------------------------------------------------------------------===//
-//
-// 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___CXX03___LOCALE_LOCALE_BASE_API_IBM_H
-#define _LIBCPP___CXX03___LOCALE_LOCALE_BASE_API_IBM_H
-
-#if defined(__MVS__)
-# include <__cxx03/__support/ibm/locale_mgmt_zos.h>
-#endif // defined(__MVS__)
-
-#include <__cxx03/locale.h>
-#include <__cxx03/stdarg.h>
-#include <__cxx03/stdio.h>
-
-#include "cstdlib"
-
-#if defined(__MVS__)
-# include <__cxx03/wctype.h>
-// POSIX routines
-# include <__cxx03/__support/xlocale/__posix_l_fallback.h>
-#endif // defined(__MVS__)
-
-namespace {
-
-struct __setAndRestore {
- explicit __setAndRestore(locale_t locale) {
- if (locale == (locale_t)0) {
- __cloc = newlocale(LC_ALL_MASK, "C", /* base */ (locale_t)0);
- __stored = uselocale(__cloc);
- } else {
- __stored = uselocale(locale);
- }
- }
-
- ~__setAndRestore() {
- uselocale(__stored);
- if (__cloc)
- freelocale(__cloc);
- }
-
-private:
- locale_t __stored = (locale_t)0;
- locale_t __cloc = (locale_t)0;
-};
-
-} // namespace
-
-// The following are not POSIX routines. These are quick-and-dirty hacks
-// to make things pretend to work
-inline _LIBCPP_HIDE_FROM_ABI long long strtoll_l(const char* __nptr, char** __endptr, int __base, locale_t locale) {
- __setAndRestore __newloc(locale);
- return ::strtoll(__nptr, __endptr, __base);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI double strtod_l(const char* __nptr, char** __endptr, locale_t locale) {
- __setAndRestore __newloc(locale);
- return ::strtod(__nptr, __endptr);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI float strtof_l(const char* __nptr, char** __endptr, locale_t locale) {
- __setAndRestore __newloc(locale);
- return ::strtof(__nptr, __endptr);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI long double strtold_l(const char* __nptr, char** __endptr, locale_t locale) {
- __setAndRestore __newloc(locale);
- return ::strtold(__nptr, __endptr);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI unsigned long long
-strtoull_l(const char* __nptr, char** __endptr, int __base, locale_t locale) {
- __setAndRestore __newloc(locale);
- return ::strtoull(__nptr, __endptr, __base);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI
-_LIBCPP_ATTRIBUTE_FORMAT(__printf__, 2, 0) int vasprintf(char** strp, const char* fmt, va_list ap) {
- const size_t buff_size = 256;
- if ((*strp = (char*)malloc(buff_size)) == NULL) {
- return -1;
- }
-
- va_list ap_copy;
- // va_copy may not be provided by the C library in C++03 mode.
-#if defined(_LIBCPP_CXX03_LANG) && __has_builtin(__builtin_va_copy)
- __builtin_va_copy(ap_copy, ap);
-#else
- va_copy(ap_copy, ap);
-#endif
- int str_size = vsnprintf(*strp, buff_size, fmt, ap_copy);
- va_end(ap_copy);
-
- if ((size_t)str_size >= buff_size) {
- if ((*strp = (char*)realloc(*strp, str_size + 1)) == NULL) {
- return -1;
- }
- str_size = vsnprintf(*strp, str_size + 1, fmt, ap);
- }
- return str_size;
-}
-
-#endif // _LIBCPP___CXX03___LOCALE_LOCALE_BASE_API_IBM_H
diff --git a/libcxx/include/__cxx03/__support/ibm/gettod_zos.h b/libcxx/include/__cxx03/__support/ibm/gettod_zos.h
deleted file mode 100644
index 3bb9b942a3903..0000000000000
--- a/libcxx/include/__cxx03/__support/ibm/gettod_zos.h
+++ /dev/null
@@ -1,52 +0,0 @@
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// 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___CXX03___SUPPORT_IBM_GETTOD_ZOS_H
-#define _LIBCPP___CXX03___SUPPORT_IBM_GETTOD_ZOS_H
-
-#include <__cxx03/time.h>
-
-inline _LIBCPP_HIDE_FROM_ABI int gettimeofdayMonotonic(struct timespec64* Output) {
- // The POSIX gettimeofday() function is not available on z/OS. Therefore,
- // we will call stcke and other hardware instructions in implement equivalent.
- // Note that nanoseconds alone will overflow when reaching new epoch in 2042.
-
- struct _t {
- uint64_t Hi;
- uint64_t Lo;
- };
- struct _t Value = {0, 0};
- uint64_t CC = 0;
- asm(" stcke %0\n"
- " ipm %1\n"
- " srlg %1,%1,28\n"
- : "=m"(Value), "+r"(CC)::);
-
- if (CC != 0) {
- errno = EMVSTODNOTSET;
- return CC;
- }
- uint64_t us = (Value.Hi >> 4);
- uint64_t ns = ((Value.Hi & 0x0F) << 8) + (Value.Lo >> 56);
- ns = (ns * 1000) >> 12;
- us = us - 2208988800000000;
-
- register uint64_t DivPair0 asm("r0"); // dividend (upper half), remainder
- DivPair0 = 0;
- register uint64_t DivPair1 asm("r1"); // dividend (lower half), quotient
- DivPair1 = us;
- uint64_t Divisor = 1000000;
- asm(" dlgr %0,%2" : "+r"(DivPair0), "+r"(DivPair1) : "r"(Divisor) :);
-
- Output->tv_sec = DivPair1;
- Output->tv_nsec = DivPair0 * 1000 + ns;
- return 0;
-}
-
-#endif // _LIBCPP___CXX03___SUPPORT_IBM_GETTOD_ZOS_H
diff --git a/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h b/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h
deleted file mode 100644
index 9fa8f8c9088e1..0000000000000
--- a/libcxx/include/__cxx03/__support/ibm/locale_mgmt_zos.h
+++ /dev/null
@@ -1,53 +0,0 @@
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// 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___CXX03___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
-#define _LIBCPP___CXX03___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
-
-#if defined(__MVS__)
-# include <__cxx03/locale.h>
-# include <__cxx03/string>
-
-# ifdef __cplusplus
-extern "C" {
-# endif
-
-# define _LC_MAX LC_MESSAGES /* highest real category */
-# define _NCAT (_LC_MAX + 1) /* maximum + 1 */
-
-# define _CATMASK(n) (1 << (n))
-# define LC_COLLATE_MASK _CATMASK(LC_COLLATE)
-# define LC_CTYPE_MASK _CATMASK(LC_CTYPE)
-# define LC_MONETARY_MASK _CATMASK(LC_MONETARY)
-# define LC_NUMERIC_MASK _CATMASK(LC_NUMERIC)
-# define LC_TIME_MASK _CATMASK(LC_TIME)
-# define LC_MESSAGES_MASK _CATMASK(LC_MESSAGES)
-# define LC_ALL_MASK (_CATMASK(_NCAT) - 1)
-
-typedef struct locale_struct {
- int category_mask;
- std::string lc_collate;
- std::string lc_ctype;
- std::string lc_monetary;
- std::string lc_numeric;
- std::string lc_time;
- std::string lc_messages;
-}* locale_t;
-
-// z/OS does not have newlocale, freelocale and uselocale.
-// The functions below are workarounds in single thread mode.
-locale_t newlocale(int category_mask, const char* locale, locale_t base);
-void freelocale(locale_t locobj);
-locale_t uselocale(locale_t newloc);
-
-# ifdef __cplusplus
-}
-# endif
-#endif // defined(__MVS__)
-#endif // _LIBCPP___CXX03___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
diff --git a/libcxx/include/__cxx03/__support/ibm/nanosleep.h b/libcxx/include/__cxx03/__support/ibm/nanosleep.h
deleted file mode 100644
index 1b6883bbcc111..0000000000000
--- a/libcxx/include/__cxx03/__support/ibm/nanosleep.h
+++ /dev/null
@@ -1,55 +0,0 @@
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// 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___CXX03___SUPPORT_IBM_NANOSLEEP_H
-#define _LIBCPP___CXX03___SUPPORT_IBM_NANOSLEEP_H
-
-#include <__cxx03/unistd.h>
-
-inline int nanosleep(const struct timespec* __req, struct timespec* __rem) {
- // The nanosleep() function is not available on z/OS. Therefore, we will call
- // sleep() to sleep for whole seconds and usleep() to sleep for any remaining
- // fraction of a second. Any remaining nanoseconds will round up to the next
- // microsecond.
- if (__req->tv_sec < 0 || __req->tv_nsec < 0 || __req->tv_nsec > 999999999) {
- errno = EINVAL;
- return -1;
- }
- long __micro_sec = (__req->tv_nsec + 999) / 1000;
- time_t __sec = __req->tv_sec;
- if (__micro_sec > 999999) {
- ++__sec;
- __micro_sec -= 1000000;
- }
- __sec = static_cast<time_t>(sleep(static_cast<unsigned int>(__sec)));
- if (__sec) {
- if (__rem) {
- // Updating the remaining time to sleep in case of unsuccessful call to sleep().
- __rem->tv_sec = __sec;
- __rem->tv_nsec = __micro_sec * 1000;
- }
- errno = EINTR;
- return -1;
- }
- if (__micro_sec) {
- int __rt = usleep(static_cast<unsigned int>(__micro_sec));
- if (__rt != 0 && __rem) {
- // The usleep() does not provide the amount of remaining time upon its failure,
- // so the time slept will be ignored.
- __rem->tv_sec = 0;
- __rem->tv_nsec = __micro_sec * 1000;
- // The errno is already set.
- return -1;
- }
- return __rt;
- }
- return 0;
-}
-
-#endif // _LIBCPP___CXX03___SUPPORT_IBM_NANOSLEEP_H
diff --git a/libcxx/include/__cxx03/__thread/support/pthread.h b/libcxx/include/__cxx03/__thread/support/pthread.h
index 4dc7a4980de2b..5d044ebb78275 100644
--- a/libcxx/include/__cxx03/__thread/support/pthread.h
+++ b/libcxx/include/__cxx03/__thread/support/pthread.h
@@ -19,7 +19,7 @@
#include <sched.h>
#ifdef __MVS__
-# include <__cxx03/__support/ibm/nanosleep.h>
+# include <__support/ibm/nanosleep.h>
#endif
// Some platforms require <bits/atomic_wide_counter.h> in order for
diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 20387ea76124b..c85a0162dc2b2 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -17,7 +17,7 @@
#include <chrono>
#if defined(__MVS__)
-# include <__support/ibm/gettod_zos.h> // gettimeofdayMonotonic
+# include <support/ibm/gettod_zos.h> // gettimeofdayMonotonic
#endif
#include "include/apple_availability.h"
diff --git a/libcxx/include/__support/ibm/gettod_zos.h b/libcxx/src/support/ibm/gettod_zos.h
similarity index 100%
rename from libcxx/include/__support/ibm/gettod_zos.h
rename to libcxx/src/support/ibm/gettod_zos.h
More information about the libcxx-commits
mailing list