Skip to content

Commit 219c164

Browse files
committed
Use CApiFFI where available
In https://ghc.haskell.org/trac/ghc/ticket/9689 aosivitz provided a proper patch (http://lpaste.net/8287391336118616064) to zlib that allows it to be cross compiled with hsc2hs. This patch seems to have been lost in time. The original patch has been slightly adapted to the current `Stream.hsc`.
1 parent f57b2e9 commit 219c164

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

Codec/Compression/Zlib/Stream.hsc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#if __GLASGOW_HASKELL__ >= 702
33
{-# LANGUAGE DeriveGeneric #-}
44
#endif
5+
#if __GLASGOW_HASKELL__ >= 706
6+
{-# LANGUAGE CApiFFI #-}
7+
#endif
58
-----------------------------------------------------------------------------
69
-- |
710
-- Copyright : (c) 2006-2015 Duncan Coutts
@@ -1008,13 +1011,25 @@ newtype StreamState = StreamState (Ptr StreamState)
10081011
--
10091012
-- So we define c_inflateInit2 and c_deflateInit2 here as wrappers around
10101013
-- their _ counterparts and pass the extra args.
1014+
--
1015+
-- As of GHC 7.6, we can import macros directly using the CApiFFI extension.
1016+
-- This avoids the need for the hsc2hs #{const_str} construct, which means
1017+
-- hsc2hs can run in cross-compilation mode.
10111018

10121019
##ifdef NON_BLOCKING_FFI
10131020
##define SAFTY safe
10141021
##else
10151022
##define SAFTY unsafe
10161023
##endif
10171024

1025+
#if __GLASGOW_HASKELL__ >= 706
1026+
foreign import capi unsafe "zlib.h inflateInit2"
1027+
c_inflateInit2 :: StreamState -> CInt -> IO CInt
1028+
1029+
foreign import capi unsafe "zlib.h deflateInit2"
1030+
c_deflateInit2 :: StreamState
1031+
-> CInt -> CInt -> CInt -> CInt -> CInt -> IO CInt
1032+
#else
10181033
foreign import ccall unsafe "zlib.h inflateInit2_"
10191034
c_inflateInit2_ :: StreamState -> CInt -> Ptr CChar -> CInt -> IO CInt
10201035

@@ -1023,15 +1038,6 @@ c_inflateInit2 z n =
10231038
withCAString #{const_str ZLIB_VERSION} $ \versionStr ->
10241039
c_inflateInit2_ z n versionStr (#{const sizeof(z_stream)} :: CInt)
10251040

1026-
foreign import ccall SAFTY "zlib.h inflate"
1027-
c_inflate :: StreamState -> CInt -> IO CInt
1028-
1029-
foreign import ccall unsafe "zlib.h &inflateEnd"
1030-
c_inflateEnd :: FinalizerPtr StreamState
1031-
1032-
foreign import ccall unsafe "zlib.h inflateReset"
1033-
c_inflateReset :: StreamState -> IO CInt
1034-
10351041
foreign import ccall unsafe "zlib.h deflateInit2_"
10361042
c_deflateInit2_ :: StreamState
10371043
-> CInt -> CInt -> CInt -> CInt -> CInt
@@ -1043,6 +1049,16 @@ c_deflateInit2 :: StreamState
10431049
c_deflateInit2 z a b c d e =
10441050
withCAString #{const_str ZLIB_VERSION} $ \versionStr ->
10451051
c_deflateInit2_ z a b c d e versionStr (#{const sizeof(z_stream)} :: CInt)
1052+
#endif
1053+
1054+
foreign import ccall SAFTY "zlib.h inflate"
1055+
c_inflate :: StreamState -> CInt -> IO CInt
1056+
1057+
foreign import ccall unsafe "zlib.h &inflateEnd"
1058+
c_inflateEnd :: FinalizerPtr StreamState
1059+
1060+
foreign import ccall unsafe "zlib.h inflateReset"
1061+
c_inflateReset :: StreamState -> IO CInt
10461062

10471063
foreign import ccall unsafe "zlib.h deflateSetDictionary"
10481064
c_deflateSetDictionary :: StreamState

0 commit comments

Comments
 (0)