Skip to content

Commit

Permalink
Merge pull request #142 from h-vetinari/tzdb
Browse files Browse the repository at this point in the history
tzdb integration
  • Loading branch information
isuruf authored Aug 5, 2024
2 parents 54154d3 + a6d52a9 commit 2cdc2b9
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .scripts/build_steps.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ source:
sha256: 3a2b10cab86e32358fdac871546d57e2700e9bdb5875ef33fff5b601265b9e32 # [gcc_version == "13.3.0"]
sha256: 5a30de2be740062bb3ddd3fd13c9b1bb4584d8f85616d33f23a713439d714148 # [gcc_version == "12.4.0"]
patches:
- patches/0001-cross-compile-older-glibc.patch # [cross_target_stdlib_version == "2.12" and target_platform != "linux-64"]
- patches/0002-allow-commands-in-main-specfile.patch
- patches/0003-patch-zoneinfo_dir_override-to-point-to-our-tzdata.patch # [gcc_maj_ver == 14]
{% if cross_target_platform.startswith("linux-") %}
- patches/0001-Hardcode-HAVE_ALIGNED_ALLOC-1-in-libstdc-v3-configur.patch
{% endif %}
- patches/0002-cross-compile-older-glibc.patch # [cross_target_stdlib_version == "2.12" and target_platform != "linux-64"]
- patches/0003-allow-commands-in-main-specfile.patch
{% if cross_target_platform.startswith("win-") %}
- patches/0004-add-ldl-to-libstdc___la_LDFLAGS.patch # [gcc_maj_ver == 14]
- patches/0005-Hardcode-HAVE_ALIGNED_ALLOC-1-in-libstdc-v3-configur.patch
{% else %}
# for GCC 13: https://github.com/msys2/MINGW-packages/tree/4f1262b4e1072632eccf0958764f90d890b832ac/mingw-w64-gcc
# for GCC 14: https://github.com/msys2/MINGW-packages/tree/331bf945d21af562d228ed46bda21c8272d1e76e/mingw-w64-gcc
- patches/mingw/{{ gcc_maj_ver }}/0001-missing-__thiscall-attribute-on-builtin-declaration-of-__cxa_thread_atexit.patch # [gcc_maj_ver == 14]
Expand Down Expand Up @@ -307,6 +308,8 @@ outputs:
# not needed due to pinning above but marks this build as using the new sysroots
- {{ cross_target_stdlib }}_{{ cross_target_platform }}
- {{ pin_subpackage("libstdcxx-devel_" ~ cross_target_platform, exact=True) }}
# for C++20 chrono support
- tzdata
run_exports:
# impose this requirement across the build/host boundary
strong:
Expand All @@ -325,6 +328,20 @@ outputs:
- ${CXX} -Wall tests/hello_world.cpp -c -o hello_world.o --std=c++17
- ${CXX} -Wall hello_world.o -o hello_world --std=c++17

# test tzdb integration
{% if gcc_maj_ver|int >= 14 %}
- ${CXX} -isystem ${PREFIX}/include --std=c++20 -Wall tests/tzdb-override.cpp -c -o tzdb-override.o
- ${CXX} -isystem ${PREFIX}/include --std=c++20 -Wall tests/tzdb.cpp -c -o tzdb.o
{% if target_platform == cross_target_platform %}
- ${CXX} tzdb-override.o -o tzdb-override && ./tzdb-override
- ${CXX} tzdb.o -o tzdb && ./tzdb
- strace ./tzdb 2>&1 | grep ${CONDA_PREFIX}/lib/../share/zoneinfo/tzdata.zi
# also test without any environment activation
- unset PREFIX
- unset CONDA_PREFIX
- ./tzdb
{% endif %}
{% endif %}
about:
summary: GNU C++ Compiler
home: https://gcc.gnu.org/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
From 50ad6bc09ecaa3143faac20b6ad571fe6659e548 Mon Sep 17 00:00:00 2001
From be1d364e55ba4c44b8f6ead05810b28fcb66d173 Mon Sep 17 00:00:00 2001
From: Isuru Fernando <isuruf@gmail.com>
Date: Thu, 15 Jul 2021 07:58:14 +0000
Subject: [PATCH 2/4] cross-compile older glibc
Subject: [PATCH 1/5] cross-compile older glibc

---
libgomp/secure_getenv.h | 2 +-
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
From e63de7d363c14624c50b8c84148540fca96db9e5 Mon Sep 17 00:00:00 2001
From 77615b55353865222794500487ae0b4b70888efd Mon Sep 17 00:00:00 2001
From: Tim Snyder <snyder.tim@gmail.com>
Date: Tue, 29 Mar 2022 22:33:27 +0000
Subject: [PATCH 3/4] allow % commands in main specfile
Subject: [PATCH 2/5] allow % commands in main specfile

---
gcc/gcc.cc | 3 ++-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
From 364f9fd8505a162a6cacd0223c3b55927804981c Mon Sep 17 00:00:00 2001
From: "H. Vetinari" <h.vetinari@gmx.com>
Date: Fri, 21 Jun 2024 12:41:07 +1100
Subject: [PATCH 3/5] patch zoneinfo_dir_override to point to our tzdata

---
libstdc++-v3/src/c++20/tzdb.cc | 78 ++++++++++++++++++++++++++++------
1 file changed, 65 insertions(+), 13 deletions(-)

diff --git a/libstdc++-v3/src/c++20/tzdb.cc b/libstdc++-v3/src/c++20/tzdb.cc
index c7c7cc9de..1596a77b2 100644
--- a/libstdc++-v3/src/c++20/tzdb.cc
+++ b/libstdc++-v3/src/c++20/tzdb.cc
@@ -37,8 +37,12 @@
#include <mutex> // mutex
#include <filesystem> // filesystem::read_symlink

-#ifndef _AIX
-# include <cstdlib> // getenv
+#if defined(__linux__)
+# include <dlfcn.h> // dladdr
+# include <cstdlib> // free, malloc, realpath
+# include <cstring> // memcpy, strlen
+#else defined(_WIN32)
+# include <windows.h>
#endif

#if defined __GTHREADS && ATOMIC_POINTER_LOCK_FREE == 2
@@ -64,25 +68,73 @@

namespace __gnu_cxx
{
-#ifdef _AIX
- // Cannot override weak symbols on AIX.
- const char* (*zoneinfo_dir_override)() = nullptr;
-#else
- [[gnu::weak]] const char* zoneinfo_dir_override();
-
-#if defined(__APPLE__) || defined(__hpux__) \
- || (defined(__VXWORKS__) && !defined(__RTP__))
- // Need a weak definition for Mach-O et al.
[[gnu::weak]] const char* zoneinfo_dir_override()
{
+#ifdef _GLIBCXX_SHARED
+ // get path to library we're in, to determine our location relative to $PREFIX;
+ // with help from the MIT-licensed https://github.com/gpakosz/whereami
+ void* addr = __builtin_extract_return_addr(__builtin_return_address(0));
+ char* this_lib;
+ int i;
+ // <string> is included through <chrono>
+ static std::string tz_dir;
+ if (!tz_dir.empty()) {
+ return tz_dir.c_str();
+ }
+#ifdef _WIN32
+ // we're in %PREFIX%\Library\bin\libstdc++-6.dll
+# define TO_PREFIX "/../.."
+ // needs single quotes for character (not string) literal
+# define PATH_SEP '\\'
+ wchar_t buffer[MAX_PATH];
+ HMODULE hm = NULL;
+ // non-zero return means success
+ if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
+ GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+ (LPCSTR) addr, &hm)) {
+ // returns length of string (not counting null byte), see
+ // https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew#return-value
+ DWORD total_length = GetModuleFileNameW(hm, buffer, sizeof(buffer));
+ if (total_length) {
+ this_lib = (char*)malloc(total_length + 1);
+ memcpy(this_lib, buffer, total_length);
+#else
+ // we're in $PREFIX/lib/libstdcxx.so
+# define TO_PREFIX "/.."
+# define PATH_SEP '/'
+ char buffer[PATH_MAX];
+ Dl_info info;
+
+ if (dladdr(addr, &info)) {
+ char* resolved = realpath(info.dli_fname, buffer);
+ if (resolved) {
+ int total_length = (int)strlen(resolved);
+ this_lib = (char*)malloc(total_length + 1);
+ memcpy(this_lib, resolved, total_length);
+#endif
+
+ for (i = (int)total_length - 1; i >= 0; --i) {
+ if (this_lib[i] == PATH_SEP) {
+ // set to null byte so the string ends before the basename
+ this_lib[i] = '\0';
+ break;
+ }
+ }
+ tz_dir = {this_lib};
+ tz_dir += TO_PREFIX;
+ tz_dir += "/share/zoneinfo";
+ // std::string constructor for tz_dir deep-copies
+ free(this_lib);
+ return tz_dir.c_str();
+ }
+ }
+#endif // _GLIBCXX_SHARED
#ifdef _GLIBCXX_ZONEINFO_DIR
return _GLIBCXX_ZONEINFO_DIR;
#else
return nullptr;
#endif
}
-#endif
-#endif
}

#if ! defined _GLIBCXX_ZONEINFO_DIR && ! defined _GLIBCXX_STATIC_TZDATA
40 changes: 40 additions & 0 deletions recipe/patches/0004-add-ldl-to-libstdc___la_LDFLAGS.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
From 47b93186270f8f72a8a0229c08871b7a613484a4 Mon Sep 17 00:00:00 2001
From: "H. Vetinari" <h.vetinari@gmx.com>
Date: Mon, 15 Jul 2024 19:16:05 +1100
Subject: [PATCH 4/5] add `-ldl` to libstdc___la_LDFLAGS

we want to link static-only here, to avoid having to add
`-ldl` everytime something links against libstdc++.so

Co-Authored-By: Isuru Fernando <isuruf@gmail.com>
---
libstdc++-v3/src/Makefile.am | 2 +-
libstdc++-v3/src/Makefile.in | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/src/Makefile.am b/libstdc++-v3/src/Makefile.am
index 37ba1491d..5b69f43ec 100644
--- a/libstdc++-v3/src/Makefile.am
+++ b/libstdc++-v3/src/Makefile.am
@@ -161,7 +161,7 @@ libstdc___darwin_rpath += -Wl,-rpath,@loader_path
endif

libstdc___la_LDFLAGS = \
- -version-info $(libtool_VERSION) ${version_arg} -lm $(libstdc___darwin_rpath)
+ -version-info $(libtool_VERSION) ${version_arg} -lm -Bstatic -ldl -Bdynamic -Wl,--exclude-libs,libdl.a $(libstdc___darwin_rpath)

libstdc___la_LINK = $(CXXLINK) $(libstdc___la_LDFLAGS) $(lt_host_flags)

diff --git a/libstdc++-v3/src/Makefile.in b/libstdc++-v3/src/Makefile.in
index 1bdf0daa8..ab28e1480 100644
--- a/libstdc++-v3/src/Makefile.in
+++ b/libstdc++-v3/src/Makefile.in
@@ -566,7 +566,7 @@ libstdc___la_DEPENDENCIES = \
@ENABLE_DARWIN_AT_RPATH_TRUE@ -Wc,-nodefaultrpaths \
@ENABLE_DARWIN_AT_RPATH_TRUE@ -Wl,-rpath,@loader_path
libstdc___la_LDFLAGS = \
- -version-info $(libtool_VERSION) ${version_arg} -lm $(libstdc___darwin_rpath)
+ -version-info $(libtool_VERSION) ${version_arg} -lm -Bstatic -ldl -Bdynamic -Wl,--exclude-libs,libdl.a $(libstdc___darwin_rpath)

libstdc___la_LINK = $(CXXLINK) $(libstdc___la_LDFLAGS) $(lt_host_flags)
@GLIBCXX_LDBL_ALT128_COMPAT_FALSE@@GLIBCXX_LDBL_COMPAT_TRUE@LTCXXCOMPILE64 = $(LTCXXCOMPILE)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
From be731c6d9ebcf6d3951a51f57e1c30665403b982 Mon Sep 17 00:00:00 2001
From bdc5db51bf810074d4301f41b88add71945fbabd Mon Sep 17 00:00:00 2001
From: Nehal J Wani <nehaljw.kkd1@gmail.com>
Date: Tue, 12 Jun 2018 05:26:24 +0000
Subject: [PATCH 1/4] Hardcode HAVE_ALIGNED_ALLOC=1 in libstdc++-v3/configure
Subject: [PATCH 5/5] Hardcode HAVE_ALIGNED_ALLOC=1 in libstdc++-v3/configure

---
libstdc++-v3/configure | 16 ++++++++++++++++
Expand Down
29 changes: 29 additions & 0 deletions recipe/tests/tzdb-override.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// We are using a weak symbol in linux and windows using a patch
// while upstream only has the definition and no implementation.
// Check that overriding works.

// adapted from upstream tests
// https://github.com/gcc-mirror/gcc/blob/releases/gcc-14.1.0/libstdc%2B%2B-v3/testsuite/std/time/tzdb/1.cc
// https://github.com/gcc-mirror/gcc/blob/releases/gcc-14.1.0/libstdc%2B%2B-v3/testsuite/std/time/tzdb/leap_seconds.cc

#include <chrono>
#include <stdio.h>
#include <stdlib.h>

static bool override_used = false;

namespace __gnu_cxx
{
const char* zoneinfo_dir_override() {
override_used = true;
return "./";
}
}

using namespace std::chrono;

int main()
{
auto &a = get_tzdb();
return !override_used;
}
1 change: 1 addition & 0 deletions recipe/yum_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ sed
findutils
make
file
strace

0 comments on commit 2cdc2b9

Please sign in to comment.