hsc2hs: #const shouldn't be used with clockid_t
In https://gitlab.haskell.org/ghc/ghc/-/blob/master/libraries/base/System/CPUTime/Posix/ClockGetTime.hsc, we use #const
to get CLOCK_PROCESS_CPUTIME_ID
. This works with glibc
, where clockid_t
is int
, and all CLOCK_*
macros are integer literals.
However, clockid_t = int
is not a POSIX standard. One counter-example I just found in asterius work: in wasi-libc
, clockid_t
a pointer type, and CLOCK_*
macros are struct literals. So using #const
to obtain the CLOCK_*
values won't work. There maybe other similar #const
use sites which are less portable than we think.
I propose to:
- Refactor the
CInt
types of such#const
use sites toCUIntPtr
- Use a
foreign import capi
to replace the#const
usage
This will add a very small overhead to native architectures, since what used to be an integer literal in the object files will become a CAF that involves a single c call at runtime. But this will improve the portability of our libraries, especially for wasm32-unknown-wasi
support.