From a46484c8b3715725f5dc0b8ad1bf921880ed9af1 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Wed, 18 Mar 2020 15:23:25 -0700 Subject: [PATCH] build: Detect gmtime_* definitions via configure This improves the portability of the codebase and fixes compilation with mingw-w64 7.0+. Co-authored-by: fanquake --- configure.ac | 17 +++++++++++++++++ src/util/time.cpp | 12 ++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 1f85dd3a99..4cfd5c9274 100644 --- a/configure.ac +++ b/configure.ac @@ -906,6 +906,22 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te LDFLAGS="$TEMP_LDFLAGS" fi +dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable +dnl fail if neither are available. +AC_MSG_CHECKING(for gmtime_r) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[ gmtime_r((const time_t *) nullptr, (struct tm *) nullptr); ]])], + [ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GMTIME_R, 1, [Define this symbol if gmtime_r is available]) ], + [ AC_MSG_RESULT(no); + AC_MSG_CHECKING(for gmtime_s); + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[ gmtime_s((struct tm *) nullptr, (const time_t *) nullptr); ]])], + [ AC_MSG_RESULT(yes)], + [ AC_MSG_RESULT(no); AC_MSG_ERROR(Both gmtime_r and gmtime_s are unavailable) ] + ) + ] +) + dnl Check for different ways of gathering OS randomness AC_MSG_CHECKING(for Linux getrandom syscall) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include @@ -1596,6 +1612,7 @@ AC_SUBST(EVENT_LIBS) AC_SUBST(EVENT_PTHREADS_LIBS) AC_SUBST(ZMQ_LIBS) AC_SUBST(QR_LIBS) +AC_SUBST(HAVE_GMTIME_R) AC_SUBST(HAVE_FDATASYNC) AC_SUBST(HAVE_FULLFSYNC) AC_SUBST(HAVE_O_CLOEXEC) diff --git a/src/util/time.cpp b/src/util/time.cpp index 14937b985e..0938ff36a6 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -78,10 +78,10 @@ int64_t GetSystemTimeInSeconds() std::string FormatISO8601DateTime(int64_t nTime) { struct tm ts; time_t time_val = nTime; -#ifdef _MSC_VER - if (gmtime_s(&ts, &time_val) != 0) { -#else +#ifdef HAVE_GMTIME_R if (gmtime_r(&time_val, &ts) == nullptr) { +#else + if (gmtime_s(&ts, &time_val) != 0) { #endif return {}; } @@ -91,10 +91,10 @@ std::string FormatISO8601DateTime(int64_t nTime) { std::string FormatISO8601Date(int64_t nTime) { struct tm ts; time_t time_val = nTime; -#ifdef _MSC_VER - if (gmtime_s(&ts, &time_val) != 0) { -#else +#ifdef HAVE_GMTIME_R if (gmtime_r(&time_val, &ts) == nullptr) { +#else + if (gmtime_s(&ts, &time_val) != 0) { #endif return {}; }