Compare commits
21 Commits
569fae5d32
...
5d2ba1a217
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d2ba1a217 | ||
|
|
528cf7b8d7 | ||
|
|
cb05afc21d | ||
|
|
604bc8cf2f | ||
|
|
6468f15d73 | ||
|
|
024b6aec87 | ||
|
|
40bb939302 | ||
|
|
ddc8b9097d | ||
|
|
25875c7dc1 | ||
|
|
2bab0ff94e | ||
|
|
7d90990b74 | ||
|
|
56370bb8a4 | ||
|
|
b9e2e59174 | ||
|
|
97a4f1c359 | ||
|
|
d8c12b5418 | ||
|
|
0e54cccd74 | ||
|
|
f1ec06fcca | ||
|
|
13b9324bc4 | ||
|
|
5f1e49c4ce | ||
|
|
7e09c1d681 | ||
|
|
0a1911862f |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
dist*
|
||||
result
|
||||
target
|
||||
cabal.project.local*
|
||||
|
||||
@ -1,13 +1,5 @@
|
||||
packages: .
|
||||
|
||||
-- TODO a total hack
|
||||
package garnet
|
||||
extra-lib-dirs:
|
||||
/home/gthomas/code/garnet/rust/target/release
|
||||
/home/gthomas/code/garnet/rust/target/debug
|
||||
extra-include-dirs:
|
||||
/home/gthomas/code/garnet/rust
|
||||
|
||||
-- https://well-typed.com/blog/2026/02/hs-bindgen-alpha
|
||||
-- Haskell.nix doesn't seem to like `tag: release-0.1-alpha`, which the blog post suggests
|
||||
-- so we specify the equivalent commit SHAs manually instead
|
||||
|
||||
@ -6,6 +6,7 @@ module GarnetRs.Wrapped (
|
||||
hello,
|
||||
helloStruct,
|
||||
helloShape,
|
||||
add,
|
||||
) where
|
||||
|
||||
import Data.ByteString
|
||||
@ -21,15 +22,15 @@ data T = T
|
||||
, b :: Word8
|
||||
}
|
||||
convertT :: T -> Raw.T
|
||||
convertT T{a, b} = Raw.T{a = CBool $ fromBool a, b}
|
||||
convertT T{a, b} = Raw.T{a = fromBool a, b}
|
||||
|
||||
data Shape
|
||||
= Circle CDouble
|
||||
| Rectangle CDouble CDouble
|
||||
convertShape :: Shape -> Raw.Shape
|
||||
convertShape = \case
|
||||
Circle r -> Raw.Shape (Raw.Shape_Tag 0) $ Raw.set_shape_body_circle $ Raw.Circle_Body r
|
||||
Rectangle w h -> Raw.Shape (Raw.Shape_Tag 1) $ Raw.set_shape_body_rectangle $ Raw.Rectangle_Body w h
|
||||
Circle r -> Raw.Shape Raw.Circle $ Raw.set_shape_body_circle $ Raw.Circle_Body r
|
||||
Rectangle w h -> Raw.Shape Raw.Rectangle $ Raw.set_shape_body_rectangle $ Raw.Rectangle_Body w h
|
||||
|
||||
hello :: ByteString -> IO ()
|
||||
hello s = useAsCString s $ Raw.hello . unsafeFromPtr
|
||||
@ -39,3 +40,6 @@ helloStruct = Raw.hello_struct . convertT
|
||||
|
||||
helloShape :: Shape -> IO ()
|
||||
helloShape = Raw.hello_shape . convertShape
|
||||
|
||||
add :: Int64 -> Int64 -> Int64
|
||||
add = Raw.add
|
||||
|
||||
@ -9,3 +9,4 @@ main = do
|
||||
helloStruct T{a = False, b = maxBound}
|
||||
helloShape $ Circle 3.14
|
||||
helloShape $ Rectangle 10.0 5.0
|
||||
putStrLn $ "3 + 4 = " <> show (add 3 4)
|
||||
|
||||
@ -5,7 +5,12 @@ set -euo pipefail
|
||||
|
||||
# Generate Haskell FFI bindings from Rust source code.
|
||||
#
|
||||
# Pipeline: cargo build -> cbindgen -> patch header -> hs-bindgen
|
||||
# Pipeline:
|
||||
# 1. cargo build - build the Rust static library
|
||||
# 2. cbindgen - generate a C header from the Rust source
|
||||
# 3. awk - patch the header for hs-bindgen compatibility
|
||||
# 4. hs-bindgen-cli - generate Haskell FFI modules from the C header
|
||||
# 5. cabal configure - point Cabal at the Rust build artifacts
|
||||
#
|
||||
# Prerequisites: run inside the Nix dev shell (provides gcc, cabal, cbindgen, hs-bindgen-cli).
|
||||
|
||||
@ -25,59 +30,18 @@ cbindgen \
|
||||
--lang c \
|
||||
--crate garnet-rs \
|
||||
--output "$HEADER" \
|
||||
--style tag \
|
||||
"$RUST_DIR"
|
||||
|
||||
echo " Raw header written to $HEADER"
|
||||
|
||||
# --- Step 3: Patch the header for hs-bindgen compatibility ---
|
||||
#
|
||||
# cbindgen emits: union { ... }; (anonymous)
|
||||
# hs-bindgen needs: union { ... } body; (named)
|
||||
# See: https://github.com/well-typed/hs-bindgen/issues/1649
|
||||
echo "=== Patching header ==="
|
||||
|
||||
# Two patches are needed:
|
||||
#
|
||||
# Patch 1: Replace cbindgen's enum pattern with typedef + #defines.
|
||||
# cbindgen emits: enum Shape_Tag { Circle, Rectangle, }; typedef uint8_t Shape_Tag;
|
||||
# hs-bindgen needs: typedef uint8_t Shape_Tag; #define Circle 0 ...
|
||||
#
|
||||
# Patch 2: Name anonymous unions inside structs.
|
||||
# cbindgen emits: union { ... };
|
||||
# hs-bindgen needs: union { ... } body;
|
||||
|
||||
awk '
|
||||
# State machine for enum->typedef+define transformation
|
||||
/^enum [A-Za-z_][A-Za-z0-9_]* \{$/ {
|
||||
in_enum = 1
|
||||
enum_name = $2
|
||||
variant_count = 0
|
||||
delete variants
|
||||
next
|
||||
}
|
||||
in_enum && /^\};$/ {
|
||||
# Next line should be: typedef <type> <enum_name>;
|
||||
in_enum = 0
|
||||
pending_enum = 1
|
||||
next
|
||||
}
|
||||
in_enum {
|
||||
# Collect variant names (strip trailing comma and whitespace)
|
||||
v = $0
|
||||
gsub(/^[[:space:]]+/, "", v)
|
||||
gsub(/,[[:space:]]*$/, "", v)
|
||||
if (v != "") {
|
||||
variants[variant_count] = v
|
||||
variant_count++
|
||||
}
|
||||
next
|
||||
}
|
||||
pending_enum && /^typedef [A-Za-z0-9_]+ / {
|
||||
# Emit: typedef <type> <name>; then #define for each variant
|
||||
print $0
|
||||
for (i = 0; i < variant_count; i++) {
|
||||
printf "#define %s %d\n", variants[i], i
|
||||
}
|
||||
pending_enum = 0
|
||||
next
|
||||
}
|
||||
|
||||
# Name anonymous unions: }; at end of union block inside struct -> } body;
|
||||
/^ \};$/ && saw_union {
|
||||
print " } body;"
|
||||
@ -94,11 +58,11 @@ pending_enum && /^typedef [A-Za-z0-9_]+ / {
|
||||
echo " Patched header at $HEADER"
|
||||
|
||||
# --- Step 4: Derive system include paths for hs-bindgen's libclang ---
|
||||
echo "=== Detecting system include paths ==="
|
||||
|
||||
#
|
||||
# hs-bindgen uses libclang directly, which doesn't know about NixOS's
|
||||
# non-standard include locations. We extract them from cpp -v and pass
|
||||
# all of them — extra paths are harmless.
|
||||
echo "=== Detecting system include paths ==="
|
||||
CLANG_OPTIONS=()
|
||||
while IFS= read -r dir; do
|
||||
CLANG_OPTIONS+=("--clang-option" "-isystem$dir")
|
||||
@ -122,6 +86,16 @@ hs-bindgen-cli preprocess \
|
||||
"${CLANG_OPTIONS[@]}" \
|
||||
-I "$RUST_DIR" "$HEADER_NAME"
|
||||
|
||||
# --- Step 6: Configure Cabal ---
|
||||
#
|
||||
# Point Cabal at the Rust static library and C header. This writes
|
||||
# cabal.project.local (gitignored) with absolute paths derived from
|
||||
# the current working directory, avoiding hardcoded paths in cabal.project.
|
||||
echo "=== Configuring Cabal ==="
|
||||
cabal configure \
|
||||
--extra-lib-dirs="$RUST_DIR/target/debug" \
|
||||
--extra-include-dirs="$RUST_DIR"
|
||||
|
||||
echo "=== Done ==="
|
||||
echo "Generated Haskell bindings in $HASKELL_DIR/generated/"
|
||||
echo "Run 'cabal run' to test."
|
||||
|
||||
@ -3,12 +3,12 @@
|
||||
{-# LANGUAGE DerivingStrategies #-}
|
||||
{-# LANGUAGE DerivingVia #-}
|
||||
{-# LANGUAGE DuplicateRecordFields #-}
|
||||
{-# LANGUAGE ExplicitForAll #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
{-# LANGUAGE MagicHash #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE NoImplicitPrelude #-}
|
||||
{-# LANGUAGE PatternSynonyms #-}
|
||||
{-# LANGUAGE StandaloneDeriving #-}
|
||||
{-# LANGUAGE TypeApplications #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
@ -19,8 +19,7 @@
|
||||
module GarnetRs where
|
||||
|
||||
import qualified Data.Array.Byte
|
||||
import qualified Data.Bits as Bits
|
||||
import qualified Data.Ix as Ix
|
||||
import qualified Data.List.NonEmpty
|
||||
import qualified Data.Primitive.Types
|
||||
import qualified Data.Proxy
|
||||
import qualified Foreign as F
|
||||
@ -28,20 +27,20 @@ import qualified Foreign.C as FC
|
||||
import qualified GHC.Generics
|
||||
import qualified GHC.Ptr as Ptr
|
||||
import qualified GHC.Records
|
||||
import qualified HsBindgen.Runtime.CEnum
|
||||
import qualified HsBindgen.Runtime.HasCField
|
||||
import qualified HsBindgen.Runtime.Internal.Bitfield
|
||||
import qualified HsBindgen.Runtime.Internal.ByteArray
|
||||
import qualified HsBindgen.Runtime.Internal.HasFFIType
|
||||
import qualified HsBindgen.Runtime.Internal.SizedByteArray
|
||||
import qualified HsBindgen.Runtime.LibC
|
||||
import qualified HsBindgen.Runtime.Marshal
|
||||
import Data.Bits (FiniteBits)
|
||||
import qualified Text.Read
|
||||
import HsBindgen.Runtime.Internal.TypeEquality (TyEq)
|
||||
import Prelude ((<*>), (>>), Bounded, Enum, Eq, Int, Integral, Num, Ord, Read, Real, Show, pure)
|
||||
import Prelude ((<*>), (>>), Eq, Int, Ord, Read, Show, pure, showsPrec)
|
||||
|
||||
{-| __C declaration:__ @struct T@
|
||||
|
||||
__defined at:__ @garnet_rs.h 6:16@
|
||||
__defined at:__ @garnet_rs.h 6:8@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -114,34 +113,86 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType T) "b")
|
||||
getField =
|
||||
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"b")
|
||||
|
||||
{-| __C declaration:__ @Shape_Tag@
|
||||
{-| __C declaration:__ @enum Shape_Tag@
|
||||
|
||||
__defined at:__ @garnet_rs.h 11:17@
|
||||
__defined at:__ @garnet_rs.h 11:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
newtype Shape_Tag = Shape_Tag
|
||||
{ unwrap :: HsBindgen.Runtime.LibC.Word8
|
||||
{ unwrap :: FC.CUInt
|
||||
}
|
||||
deriving stock (GHC.Generics.Generic)
|
||||
deriving stock (Eq, Ord, Read, Show)
|
||||
deriving newtype
|
||||
( HsBindgen.Runtime.Marshal.StaticSize
|
||||
, HsBindgen.Runtime.Marshal.ReadRaw
|
||||
, HsBindgen.Runtime.Marshal.WriteRaw
|
||||
, F.Storable
|
||||
, HsBindgen.Runtime.Internal.HasFFIType.HasFFIType
|
||||
, Data.Primitive.Types.Prim
|
||||
, HsBindgen.Runtime.Internal.Bitfield.Bitfield
|
||||
, Bits.Bits
|
||||
, Bounded
|
||||
, Enum
|
||||
, FiniteBits
|
||||
, Integral
|
||||
, Ix.Ix
|
||||
, Num
|
||||
, Real
|
||||
)
|
||||
deriving stock (Eq, Ord)
|
||||
deriving newtype (HsBindgen.Runtime.Internal.HasFFIType.HasFFIType)
|
||||
|
||||
instance HsBindgen.Runtime.Marshal.StaticSize Shape_Tag where
|
||||
|
||||
staticSizeOf = \_ -> (4 :: Int)
|
||||
|
||||
staticAlignment = \_ -> (4 :: Int)
|
||||
|
||||
instance HsBindgen.Runtime.Marshal.ReadRaw Shape_Tag where
|
||||
|
||||
readRaw =
|
||||
\ptr0 ->
|
||||
pure Shape_Tag
|
||||
<*> HsBindgen.Runtime.Marshal.readRawByteOff ptr0 (0 :: Int)
|
||||
|
||||
instance HsBindgen.Runtime.Marshal.WriteRaw Shape_Tag where
|
||||
|
||||
writeRaw =
|
||||
\ptr0 ->
|
||||
\s1 ->
|
||||
case s1 of
|
||||
Shape_Tag unwrap2 ->
|
||||
HsBindgen.Runtime.Marshal.writeRawByteOff ptr0 (0 :: Int) unwrap2
|
||||
|
||||
deriving via HsBindgen.Runtime.Marshal.EquivStorable Shape_Tag instance F.Storable Shape_Tag
|
||||
|
||||
deriving via FC.CUInt instance Data.Primitive.Types.Prim Shape_Tag
|
||||
|
||||
instance HsBindgen.Runtime.CEnum.CEnum Shape_Tag where
|
||||
|
||||
type CEnumZ Shape_Tag = FC.CUInt
|
||||
|
||||
toCEnum = Shape_Tag
|
||||
|
||||
fromCEnum = GHC.Records.getField @"unwrap"
|
||||
|
||||
declaredValues =
|
||||
\_ ->
|
||||
HsBindgen.Runtime.CEnum.declaredValuesFromList [ (0, Data.List.NonEmpty.singleton "Circle")
|
||||
, (1, Data.List.NonEmpty.singleton "Rectangle")
|
||||
]
|
||||
|
||||
showsUndeclared =
|
||||
HsBindgen.Runtime.CEnum.showsWrappedUndeclared "Shape_Tag"
|
||||
|
||||
readPrecUndeclared =
|
||||
HsBindgen.Runtime.CEnum.readPrecWrappedUndeclared "Shape_Tag"
|
||||
|
||||
isDeclared = HsBindgen.Runtime.CEnum.seqIsDeclared
|
||||
|
||||
mkDeclared = HsBindgen.Runtime.CEnum.seqMkDeclared
|
||||
|
||||
instance HsBindgen.Runtime.CEnum.SequentialCEnum Shape_Tag where
|
||||
|
||||
minDeclaredValue = Circle
|
||||
|
||||
maxDeclaredValue = Rectangle
|
||||
|
||||
instance Show Shape_Tag where
|
||||
|
||||
showsPrec = HsBindgen.Runtime.CEnum.shows
|
||||
|
||||
instance Read Shape_Tag where
|
||||
|
||||
readPrec = HsBindgen.Runtime.CEnum.readPrec
|
||||
|
||||
readList = Text.Read.readListDefault
|
||||
|
||||
readListPrec = Text.Read.readListPrecDefault
|
||||
|
||||
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Shape_Tag) "unwrap")
|
||||
) => GHC.Records.HasField "unwrap" (Ptr.Ptr Shape_Tag) (Ptr.Ptr ty) where
|
||||
@ -151,32 +202,31 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Shape_Tag) "unwrap")
|
||||
|
||||
instance HsBindgen.Runtime.HasCField.HasCField Shape_Tag "unwrap" where
|
||||
|
||||
type CFieldType Shape_Tag "unwrap" =
|
||||
HsBindgen.Runtime.LibC.Word8
|
||||
type CFieldType Shape_Tag "unwrap" = FC.CUInt
|
||||
|
||||
offset# = \_ -> \_ -> 0
|
||||
|
||||
{-| __C declaration:__ @Circle@
|
||||
|
||||
__defined at:__ @garnet_rs.h 12:9@
|
||||
__defined at:__ @garnet_rs.h 12:3@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
circle :: FC.CInt
|
||||
circle = (0 :: FC.CInt)
|
||||
pattern Circle :: Shape_Tag
|
||||
pattern Circle = Shape_Tag 0
|
||||
|
||||
{-| __C declaration:__ @Rectangle@
|
||||
|
||||
__defined at:__ @garnet_rs.h 13:9@
|
||||
__defined at:__ @garnet_rs.h 13:3@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
rectangle :: FC.CInt
|
||||
rectangle = (1 :: FC.CInt)
|
||||
pattern Rectangle :: Shape_Tag
|
||||
pattern Rectangle = Shape_Tag 1
|
||||
|
||||
{-| __C declaration:__ @struct Circle_Body@
|
||||
|
||||
__defined at:__ @garnet_rs.h 15:16@
|
||||
__defined at:__ @garnet_rs.h 16:8@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -184,7 +234,7 @@ data Circle_Body = Circle_Body
|
||||
{ radius :: FC.CDouble
|
||||
{- ^ __C declaration:__ @radius@
|
||||
|
||||
__defined at:__ @garnet_rs.h 16:10@
|
||||
__defined at:__ @garnet_rs.h 17:10@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -230,7 +280,7 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Circle_Body) "radius
|
||||
|
||||
{-| __C declaration:__ @struct Rectangle_Body@
|
||||
|
||||
__defined at:__ @garnet_rs.h 19:16@
|
||||
__defined at:__ @garnet_rs.h 20:8@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -238,14 +288,14 @@ data Rectangle_Body = Rectangle_Body
|
||||
{ width :: FC.CDouble
|
||||
{- ^ __C declaration:__ @width@
|
||||
|
||||
__defined at:__ @garnet_rs.h 20:10@
|
||||
__defined at:__ @garnet_rs.h 21:10@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
, height :: FC.CDouble
|
||||
{- ^ __C declaration:__ @height@
|
||||
|
||||
__defined at:__ @garnet_rs.h 21:10@
|
||||
__defined at:__ @garnet_rs.h 22:10@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -305,7 +355,7 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Rectangle_Body) "hei
|
||||
|
||||
{-| __C declaration:__ @union \@Shape_body@
|
||||
|
||||
__defined at:__ @garnet_rs.h 26:3@
|
||||
__defined at:__ @garnet_rs.h 27:3@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -328,7 +378,7 @@ deriving via HsBindgen.Runtime.Marshal.EquivStorable Shape_body instance F.Stora
|
||||
|
||||
__C declaration:__ @circle@
|
||||
|
||||
__defined at:__ @garnet_rs.h 27:17@
|
||||
__defined at:__ @garnet_rs.h 28:24@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -355,7 +405,7 @@ set_shape_body_circle =
|
||||
|
||||
__C declaration:__ @rectangle@
|
||||
|
||||
__defined at:__ @garnet_rs.h 28:20@
|
||||
__defined at:__ @garnet_rs.h 29:27@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -403,7 +453,7 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Shape_body) "rectang
|
||||
|
||||
{-| __C declaration:__ @struct Shape@
|
||||
|
||||
__defined at:__ @garnet_rs.h 24:16@
|
||||
__defined at:__ @garnet_rs.h 25:8@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -411,14 +461,14 @@ data Shape = Shape
|
||||
{ tag :: Shape_Tag
|
||||
{- ^ __C declaration:__ @tag@
|
||||
|
||||
__defined at:__ @garnet_rs.h 25:13@
|
||||
__defined at:__ @garnet_rs.h 26:18@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
, body :: Shape_body
|
||||
{- ^ __C declaration:__ @body@
|
||||
|
||||
__defined at:__ @garnet_rs.h 29:5@
|
||||
__defined at:__ @garnet_rs.h 30:5@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
|
||||
@ -10,6 +10,7 @@ import qualified GHC.IO.Unsafe
|
||||
import qualified GHC.Ptr as Ptr
|
||||
import qualified HsBindgen.Runtime.Internal.CAPI
|
||||
import qualified HsBindgen.Runtime.Internal.HasFFIType
|
||||
import qualified HsBindgen.Runtime.LibC
|
||||
import qualified HsBindgen.Runtime.PtrConst
|
||||
import Data.Void (Void)
|
||||
import GarnetRs
|
||||
@ -41,6 +42,15 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
|
||||
, "{"
|
||||
, " return &hello_shape;"
|
||||
, "}"
|
||||
, "/* com_garnet_GarnetRs_get_add */"
|
||||
, "__attribute__ ((const))"
|
||||
, "int64_t (*hs_bindgen_bbabdbe61cd1eeb2 (void)) ("
|
||||
, " int64_t arg1,"
|
||||
, " int64_t arg2"
|
||||
, ")"
|
||||
, "{"
|
||||
, " return &add;"
|
||||
, "}"
|
||||
]))
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_get_hello@
|
||||
@ -55,7 +65,7 @@ hs_bindgen_faf62265b53521d3 =
|
||||
{-# NOINLINE hello #-}
|
||||
{-| __C declaration:__ @hello@
|
||||
|
||||
__defined at:__ @garnet_rs.h 32:6@
|
||||
__defined at:__ @garnet_rs.h 33:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -75,7 +85,7 @@ hs_bindgen_0f8c37ef19b17a6d =
|
||||
{-# NOINLINE hello_struct #-}
|
||||
{-| __C declaration:__ @hello_struct@
|
||||
|
||||
__defined at:__ @garnet_rs.h 34:6@
|
||||
__defined at:__ @garnet_rs.h 35:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -95,10 +105,30 @@ hs_bindgen_287ff3ac660f333b =
|
||||
{-# NOINLINE hello_shape #-}
|
||||
{-| __C declaration:__ @hello_shape@
|
||||
|
||||
__defined at:__ @garnet_rs.h 36:6@
|
||||
__defined at:__ @garnet_rs.h 37:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
hello_shape :: Ptr.FunPtr (Shape -> IO ())
|
||||
hello_shape =
|
||||
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_287ff3ac660f333b
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_get_add@
|
||||
foreign import ccall unsafe "hs_bindgen_bbabdbe61cd1eeb2" hs_bindgen_bbabdbe61cd1eeb2_base ::
|
||||
IO (Ptr.FunPtr Void)
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_get_add@
|
||||
hs_bindgen_bbabdbe61cd1eeb2 :: IO (Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64))
|
||||
hs_bindgen_bbabdbe61cd1eeb2 =
|
||||
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_bbabdbe61cd1eeb2_base
|
||||
|
||||
{-# NOINLINE add #-}
|
||||
{-| __C declaration:__ @add@
|
||||
|
||||
__defined at:__ @garnet_rs.h 39:32@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64)
|
||||
add =
|
||||
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_bbabdbe61cd1eeb2
|
||||
|
||||
@ -7,9 +7,11 @@ module GarnetRs.Safe where
|
||||
|
||||
import qualified Foreign as F
|
||||
import qualified Foreign.C as FC
|
||||
import qualified GHC.Int
|
||||
import qualified GHC.Ptr as Ptr
|
||||
import qualified HsBindgen.Runtime.Internal.CAPI
|
||||
import qualified HsBindgen.Runtime.Internal.HasFFIType
|
||||
import qualified HsBindgen.Runtime.LibC
|
||||
import qualified HsBindgen.Runtime.PtrConst
|
||||
import Data.Void (Void)
|
||||
import GarnetRs
|
||||
@ -35,6 +37,13 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
|
||||
, "{"
|
||||
, " hello_shape(*arg1);"
|
||||
, "}"
|
||||
, "int64_t hs_bindgen_1c0c71fa74c428a9 ("
|
||||
, " int64_t arg1,"
|
||||
, " int64_t arg2"
|
||||
, ")"
|
||||
, "{"
|
||||
, " return add(arg1, arg2);"
|
||||
, "}"
|
||||
]))
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_Safe_hello@
|
||||
@ -51,7 +60,7 @@ hs_bindgen_433ea2a26af4e593 =
|
||||
|
||||
{-| __C declaration:__ @hello@
|
||||
|
||||
__defined at:__ @garnet_rs.h 32:6@
|
||||
__defined at:__ @garnet_rs.h 33:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -75,7 +84,7 @@ hs_bindgen_51157946af5519c9 =
|
||||
|
||||
{-| __C declaration:__ @hello_struct@
|
||||
|
||||
__defined at:__ @garnet_rs.h 34:6@
|
||||
__defined at:__ @garnet_rs.h 35:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -101,7 +110,7 @@ hs_bindgen_7de06f1fd827ca60 =
|
||||
|
||||
{-| __C declaration:__ @hello_shape@
|
||||
|
||||
__defined at:__ @garnet_rs.h 36:6@
|
||||
__defined at:__ @garnet_rs.h 37:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -112,3 +121,35 @@ hello_shape ::
|
||||
hello_shape =
|
||||
\s0 ->
|
||||
F.with s0 (\s1 -> hs_bindgen_7de06f1fd827ca60 s1)
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_Safe_add@
|
||||
foreign import ccall safe "hs_bindgen_1c0c71fa74c428a9" hs_bindgen_1c0c71fa74c428a9_base ::
|
||||
GHC.Int.Int64
|
||||
-> GHC.Int.Int64
|
||||
-> GHC.Int.Int64
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_Safe_add@
|
||||
hs_bindgen_1c0c71fa74c428a9 ::
|
||||
HsBindgen.Runtime.LibC.Int64
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
hs_bindgen_1c0c71fa74c428a9 =
|
||||
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_1c0c71fa74c428a9_base
|
||||
|
||||
{-|
|
||||
|
||||
Marked @__attribute((const))__@
|
||||
|
||||
__C declaration:__ @add@
|
||||
|
||||
__defined at:__ @garnet_rs.h 39:32@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
add ::
|
||||
HsBindgen.Runtime.LibC.Int64
|
||||
-- ^ __C declaration:__ @a@
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
-- ^ __C declaration:__ @b@
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
add = hs_bindgen_1c0c71fa74c428a9
|
||||
|
||||
@ -7,9 +7,11 @@ module GarnetRs.Unsafe where
|
||||
|
||||
import qualified Foreign as F
|
||||
import qualified Foreign.C as FC
|
||||
import qualified GHC.Int
|
||||
import qualified GHC.Ptr as Ptr
|
||||
import qualified HsBindgen.Runtime.Internal.CAPI
|
||||
import qualified HsBindgen.Runtime.Internal.HasFFIType
|
||||
import qualified HsBindgen.Runtime.LibC
|
||||
import qualified HsBindgen.Runtime.PtrConst
|
||||
import Data.Void (Void)
|
||||
import GarnetRs
|
||||
@ -35,6 +37,13 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
|
||||
, "{"
|
||||
, " hello_shape(*arg1);"
|
||||
, "}"
|
||||
, "int64_t hs_bindgen_20eb651f0a8faf48 ("
|
||||
, " int64_t arg1,"
|
||||
, " int64_t arg2"
|
||||
, ")"
|
||||
, "{"
|
||||
, " return add(arg1, arg2);"
|
||||
, "}"
|
||||
]))
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_Unsafe_hello@
|
||||
@ -51,7 +60,7 @@ hs_bindgen_2dfe97662a4d6377 =
|
||||
|
||||
{-| __C declaration:__ @hello@
|
||||
|
||||
__defined at:__ @garnet_rs.h 32:6@
|
||||
__defined at:__ @garnet_rs.h 33:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -75,7 +84,7 @@ hs_bindgen_29d823ada2bc7302 =
|
||||
|
||||
{-| __C declaration:__ @hello_struct@
|
||||
|
||||
__defined at:__ @garnet_rs.h 34:6@
|
||||
__defined at:__ @garnet_rs.h 35:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -101,7 +110,7 @@ hs_bindgen_b3f40a03f07eaa85 =
|
||||
|
||||
{-| __C declaration:__ @hello_shape@
|
||||
|
||||
__defined at:__ @garnet_rs.h 36:6@
|
||||
__defined at:__ @garnet_rs.h 37:6@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
@ -112,3 +121,35 @@ hello_shape ::
|
||||
hello_shape =
|
||||
\s0 ->
|
||||
F.with s0 (\s1 -> hs_bindgen_b3f40a03f07eaa85 s1)
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_Unsafe_add@
|
||||
foreign import ccall unsafe "hs_bindgen_20eb651f0a8faf48" hs_bindgen_20eb651f0a8faf48_base ::
|
||||
GHC.Int.Int64
|
||||
-> GHC.Int.Int64
|
||||
-> GHC.Int.Int64
|
||||
|
||||
-- __unique:__ @com_garnet_GarnetRs_Unsafe_add@
|
||||
hs_bindgen_20eb651f0a8faf48 ::
|
||||
HsBindgen.Runtime.LibC.Int64
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
hs_bindgen_20eb651f0a8faf48 =
|
||||
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_20eb651f0a8faf48_base
|
||||
|
||||
{-|
|
||||
|
||||
Marked @__attribute((const))__@
|
||||
|
||||
__C declaration:__ @add@
|
||||
|
||||
__defined at:__ @garnet_rs.h 39:32@
|
||||
|
||||
__exported by:__ @garnet_rs.h@
|
||||
-}
|
||||
add ::
|
||||
HsBindgen.Runtime.LibC.Int64
|
||||
-- ^ __C declaration:__ @a@
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
-- ^ __C declaration:__ @b@
|
||||
-> HsBindgen.Runtime.LibC.Int64
|
||||
add = hs_bindgen_20eb651f0a8faf48
|
||||
|
||||
@ -3,34 +3,37 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct T {
|
||||
struct T {
|
||||
bool a;
|
||||
uint8_t b;
|
||||
} T;
|
||||
};
|
||||
|
||||
typedef uint8_t Shape_Tag;
|
||||
#define Circle 0
|
||||
#define Rectangle 1
|
||||
enum Shape_Tag {
|
||||
Circle,
|
||||
Rectangle,
|
||||
};
|
||||
|
||||
typedef struct Circle_Body {
|
||||
struct Circle_Body {
|
||||
double radius;
|
||||
} Circle_Body;
|
||||
};
|
||||
|
||||
typedef struct Rectangle_Body {
|
||||
struct Rectangle_Body {
|
||||
double width;
|
||||
double height;
|
||||
} Rectangle_Body;
|
||||
};
|
||||
|
||||
typedef struct Shape {
|
||||
Shape_Tag tag;
|
||||
struct Shape {
|
||||
enum Shape_Tag tag;
|
||||
union {
|
||||
Circle_Body circle;
|
||||
Rectangle_Body rectangle;
|
||||
struct Circle_Body circle;
|
||||
struct Rectangle_Body rectangle;
|
||||
} body;
|
||||
} Shape;
|
||||
};
|
||||
|
||||
void hello(const char *c);
|
||||
|
||||
void hello_struct(struct T t);
|
||||
|
||||
void hello_shape(struct Shape s);
|
||||
|
||||
__attribute__((const)) int64_t add(int64_t a, int64_t b);
|
||||
|
||||
@ -22,7 +22,7 @@ extern "C" fn hello_struct(t: T) -> () {
|
||||
say_hello(&format!("{:?}", t))
|
||||
}
|
||||
|
||||
#[repr(C, u8)]
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
enum Shape {
|
||||
Circle { radius: f64 },
|
||||
@ -33,3 +33,9 @@ enum Shape {
|
||||
extern "C" fn hello_shape(s: Shape) -> () {
|
||||
say_hello(&format!("{:?}", s))
|
||||
}
|
||||
|
||||
/// cbindgen:prefix=__attribute__((const))
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn add(a: i64, b: i64) -> i64 {
|
||||
a + b
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user