Skip to content

Commit 5c1ba3a

Browse files
committed
Merge #18358: util: fix compilation with mingw-w64 7.0.0
a46484c build: Detect gmtime_* definitions via configure (Ben Woosley) Pull request description: Something has changed in the mingw-w64 headers such that we no-longer compile when using 7.0.0. ```bash util/time.cpp: In function 'std::__cxx11::string FormatISO8601DateTime(int64_t)': util/time.cpp:84:9: error: 'gmtime_r' was not declared in this scope if (gmtime_r(&time_val, &ts) == nullptr) { ^~~~~~~~ util/time.cpp: In function 'std::__cxx11::string FormatISO8601Date(int64_t)': util/time.cpp:97:9: error: 'gmtime_r' was not declared in this scope if (gmtime_r(&time_val, &ts) == nullptr) { ``` Looking at time.h, it seems that `gmtime_r()` is only available when `_POSIX_C_SOURCE` is defined. This must have been the case for 6.0.0 (which we compile fine using), but no-longer seems to be for 7.0.0? I've checked that adding `-D_POSIX_C_SOURCE=200112L` to our compile flags does fix the issue above. However, an alternative solution seems to be to just use `gmtime_s()` instead, when compiling with `mingw-w64`, as `gmtime_r()` [just wraps `gmtime_s()` anyways](https://github.com/mirror/mingw-w64/blob/7c03b11bf173944679102bbe0ac061842e2f594b/mingw-w64-headers/crt/time.h#L284). I've tested this change crosss-compiling on Debian Bullseye ([mingw-w64 7.0.0](https://packages.debian.org/source/bullseye/mingw-w64)) and Buster ([mingw-w64 6.0.0](https://packages.debian.org/source/buster/mingw-w64)). ACKs for top commit: laanwj: ACK a46484c Tree-SHA512: 7cf1a81060b9625d64de40b77341d74704cc8ae1358d25d7e2909685dc83a7a9762260d72e47806e9f0a5cbabf88d0239ec9e0fd0ebd3731b1d206b075f43a63
2 parents 41fa292 + a46484c commit 5c1ba3a

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

configure.ac

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,22 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te
906906
LDFLAGS="$TEMP_LDFLAGS"
907907
fi
908908

909+
dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable
910+
dnl fail if neither are available.
911+
AC_MSG_CHECKING(for gmtime_r)
912+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
913+
[[ gmtime_r((const time_t *) nullptr, (struct tm *) nullptr); ]])],
914+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GMTIME_R, 1, [Define this symbol if gmtime_r is available]) ],
915+
[ AC_MSG_RESULT(no);
916+
AC_MSG_CHECKING(for gmtime_s);
917+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
918+
[[ gmtime_s((struct tm *) nullptr, (const time_t *) nullptr); ]])],
919+
[ AC_MSG_RESULT(yes)],
920+
[ AC_MSG_RESULT(no); AC_MSG_ERROR(Both gmtime_r and gmtime_s are unavailable) ]
921+
)
922+
]
923+
)
924+
909925
dnl Check for different ways of gathering OS randomness
910926
AC_MSG_CHECKING(for Linux getrandom syscall)
911927
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
@@ -1596,6 +1612,7 @@ AC_SUBST(EVENT_LIBS)
15961612
AC_SUBST(EVENT_PTHREADS_LIBS)
15971613
AC_SUBST(ZMQ_LIBS)
15981614
AC_SUBST(QR_LIBS)
1615+
AC_SUBST(HAVE_GMTIME_R)
15991616
AC_SUBST(HAVE_FDATASYNC)
16001617
AC_SUBST(HAVE_FULLFSYNC)
16011618
AC_SUBST(HAVE_O_CLOEXEC)

src/util/time.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ int64_t GetSystemTimeInSeconds()
7878
std::string FormatISO8601DateTime(int64_t nTime) {
7979
struct tm ts;
8080
time_t time_val = nTime;
81-
#ifdef _MSC_VER
82-
if (gmtime_s(&ts, &time_val) != 0) {
83-
#else
81+
#ifdef HAVE_GMTIME_R
8482
if (gmtime_r(&time_val, &ts) == nullptr) {
83+
#else
84+
if (gmtime_s(&ts, &time_val) != 0) {
8585
#endif
8686
return {};
8787
}
@@ -91,10 +91,10 @@ std::string FormatISO8601DateTime(int64_t nTime) {
9191
std::string FormatISO8601Date(int64_t nTime) {
9292
struct tm ts;
9393
time_t time_val = nTime;
94-
#ifdef _MSC_VER
95-
if (gmtime_s(&ts, &time_val) != 0) {
96-
#else
94+
#ifdef HAVE_GMTIME_R
9795
if (gmtime_r(&time_val, &ts) == nullptr) {
96+
#else
97+
if (gmtime_s(&ts, &time_val) != 0) {
9898
#endif
9999
return {};
100100
}

0 commit comments

Comments
 (0)