From 2b3026fff50417bb57d909b2fa87d298c091cc1c Mon Sep 17 00:00:00 2001
From: Cheng Shao <astrohavoc@gmail.com>
Date: Wed, 18 May 2022 11:16:08 +0000
Subject: [PATCH] Use base CClockId as ClockId type

We used to define ClockId as hsc2hs-detected ${type clockid_t} type.
Unfortunately, certain libcs (e.g. wasi-libc) define clockid_t as a
pointer type, and hsc2hs only supports detecting C
integral/floating-point types, so clockid_t will mistakenly be
detected as a floating-point type, resulting in incorrect code
generation.

We should really just use the base CClockId type here. base handles C
pointer types correctly, since it uses its own custom autoconf logic
instead of hsc2hs, and similar issues have been reported & fixed
before, see https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6896
and https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6912.

Also make tzset conditional.
---
 configure.ac                               | 1 +
 lib/Data/Time/Clock/Internal/CTimespec.hsc | 5 +++--
 lib/cbits/HsTime.c                         | 2 +-
 lib/include/HsTime.h                       | 2 ++
 4 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 90845c9..61154f5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -17,6 +17,7 @@ AC_CHECK_HEADERS([time.h])
 AC_CHECK_FUNCS([gmtime_r localtime_r])
 
 AC_CHECK_FUNCS([clock_gettime])
+AC_CHECK_FUNCS([tzset])
 
 AC_STRUCT_TM
 AC_STRUCT_TIMEZONE
diff --git a/lib/Data/Time/Clock/Internal/CTimespec.hsc b/lib/Data/Time/Clock/Internal/CTimespec.hsc
index 9b162e6..d699cc7 100644
--- a/lib/Data/Time/Clock/Internal/CTimespec.hsc
+++ b/lib/Data/Time/Clock/Internal/CTimespec.hsc
@@ -9,10 +9,11 @@ module Data.Time.Clock.Internal.CTimespec where
 import Foreign
 import Foreign.C
 import System.IO.Unsafe
+import System.Posix.Types
 
 #include <time.h>
 
-type ClockID = #{type clockid_t}
+type ClockID = CClockId
 
 data CTimespec = MkCTimespec CTime CLong
 
@@ -51,7 +52,7 @@ clockGetTime clockid = alloca (\ptspec -> do
     peek ptspec
     )
 
-foreign import capi unsafe "time.h value CLOCK_REALTIME" clock_REALTIME :: ClockID
+foreign import capi unsafe "HsTime.h value HS_CLOCK_REALTIME" clock_REALTIME :: ClockID
 
 clock_TAI :: Maybe ClockID
 clock_TAI =
diff --git a/lib/cbits/HsTime.c b/lib/cbits/HsTime.c
index ae863b8..6bee24b 100644
--- a/lib/cbits/HsTime.c
+++ b/lib/cbits/HsTime.c
@@ -10,7 +10,7 @@ long int get_current_timezone_seconds (time_t t,int* pdst,char const* * pname)
     // as Microsoft considers the POSIX named `tzset()` function
     // deprecated (see http://msdn.microsoft.com/en-us/library/ms235384.aspx)
     _tzset();
-#else
+#elif defined(HAVE_TZSET)
     tzset();
 #endif
 
diff --git a/lib/include/HsTime.h b/lib/include/HsTime.h
index 5296437..43dd437 100644
--- a/lib/include/HsTime.h
+++ b/lib/include/HsTime.h
@@ -18,6 +18,8 @@
 #include <time.h>
 #endif
 
+#define HS_CLOCK_REALTIME (uintptr_t)(CLOCK_REALTIME)
+
 long int get_current_timezone_seconds (time_t,int* pdst,char const* * pname);
 
 #endif
-- 
GitLab