Compare commits
10 Commits
4a8a3750fe
...
c1d30b0e61
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1d30b0e61 | ||
|
|
ea5f816425 | ||
|
|
5c3aa088f7 | ||
|
|
6203e7570f | ||
|
|
9e8bccdafd | ||
|
|
8cb0bf9c5e | ||
|
|
2ff3ad93e5 | ||
|
|
8a5bf61d6b | ||
|
|
7ca469a62d | ||
|
|
b5fd53b5ec |
63
flake.nix
63
flake.nix
@ -13,70 +13,20 @@
|
||||
};
|
||||
};
|
||||
outputs =
|
||||
inputs:
|
||||
inputs@{ nix-haskell, ... }:
|
||||
inputs.flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
|
||||
let
|
||||
pkgs = (import inputs.nixpkgs { inherit system; }).extend (import inputs.rust-overlay);
|
||||
nix-haskell = import inputs.nix-haskell { inherit system inputs; };
|
||||
crane = (inputs.crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.selectLatestNightlyWith (
|
||||
toolchain: toolchain.default.override {
|
||||
extensions = [ "rust-src" "rust-analyzer" ];
|
||||
extensions = [ "rust-src" ];
|
||||
targets = [ "x86_64-unknown-linux-gnu" ];
|
||||
}
|
||||
));
|
||||
addLibcIncludeDir =
|
||||
''
|
||||
export C_INCLUDE_PATH="${pkgs.stdenv.cc.libc.dev}/include''${C_INCLUDE_PATH:+:$C_INCLUDE_PATH}"
|
||||
'';
|
||||
project = (import inputs.nix-haskell { inherit system inputs; } {
|
||||
name = "garnet";
|
||||
src = ./.;
|
||||
compiler-nix-name = "ghc914";
|
||||
source-repository-packages = {
|
||||
# not on Hackage yet: https://well-typed.com/blog/2026/02/hs-bindgen-alpha
|
||||
# we're using more recent versions than in that post, because we want record-dot support:
|
||||
# https://github.com/well-typed/hs-bindgen/issues/1829#issuecomment-4081875451
|
||||
c-expr-dsl = inputs.hs-bindgen-src + "/c-expr-dsl";
|
||||
c-expr-runtime = inputs.hs-bindgen-src + "/c-expr-runtime";
|
||||
hs-bindgen = inputs.hs-bindgen-src + "/hs-bindgen";
|
||||
hs-bindgen-runtime = inputs.hs-bindgen-src + "/hs-bindgen-runtime";
|
||||
libclang-bindings = inputs.libclang-src;
|
||||
};
|
||||
overrides = [
|
||||
({ pkgs, ... }: {
|
||||
packages.libclang-bindings.components.library = {
|
||||
build-tools = [ pkgs.llvmPackages.llvm ];
|
||||
libs = [ pkgs.llvmPackages.libclang ];
|
||||
};
|
||||
packages.garnet.components.library = {
|
||||
preBuild = addLibcIncludeDir;
|
||||
preConfigure = ''
|
||||
mkdir -p rust/target/debug
|
||||
ln -s ${garnet-rs}/include/garnet_rs.h rust/target/debug/garnet_rs.h
|
||||
'';
|
||||
preInstall = ''
|
||||
cp ${garnet-rs}/lib/libCgarnet_rs.a dist/build/libCgarnet_rs.a
|
||||
'';
|
||||
configureFlags = [ "--extra-lib-dirs=${garnet-rs}/lib" ];
|
||||
};
|
||||
})
|
||||
];
|
||||
shell = {
|
||||
tools = {
|
||||
cabal = "latest";
|
||||
haskell-language-server = {
|
||||
src = inputs.hls-src;
|
||||
sha256map = {
|
||||
"https://github.com/snowleopard/alga" = {
|
||||
"d4e43fb42db05413459fb2df493361d5a666588a" = "0s1mlnl64wj7pkg3iipv5bb4syy3bhxwqzqv93zqlvkyfn64015i";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
withHoogle = false;
|
||||
withHaddock = true;
|
||||
shellHook = addLibcIncludeDir;
|
||||
};
|
||||
}).project.haskell-nix;
|
||||
project =
|
||||
(nix-haskell (import ./project.nix { inherit (inputs) hls-src hs-bindgen-src libclang-src; inherit garnet-rs; })
|
||||
).project.haskell-nix;
|
||||
garnet-rs = crane.buildPackage {
|
||||
src = crane.cleanCargoSource ./rust;
|
||||
doCheck = false;
|
||||
@ -102,6 +52,7 @@
|
||||
packages = with pkgs; [
|
||||
bacon
|
||||
ghcid
|
||||
rust-analyzer
|
||||
];
|
||||
};
|
||||
});
|
||||
|
||||
@ -29,7 +29,6 @@ common common
|
||||
NoMonomorphismRestriction
|
||||
OverloadedRecordDot
|
||||
OverloadedStrings
|
||||
PatternSynonyms
|
||||
RecordWildCards
|
||||
ViewPatterns
|
||||
ghc-options:
|
||||
|
||||
@ -29,9 +29,18 @@ import HsBindgen.TH
|
||||
import Language.Haskell.TH
|
||||
import System.Process
|
||||
|
||||
withHsBindgen
|
||||
do
|
||||
systemDirs <- -- TODO bit of a hack
|
||||
map (Dir . T.unpack . T.strip)
|
||||
. concatMap (takeWhile (maybe False ((== ' ') . fst) . T.uncons) . dropWhile T.null . T.lines)
|
||||
. drop 1
|
||||
. T.splitOn "search starts here:"
|
||||
. T.pack
|
||||
. thd3
|
||||
<$> runIO (readProcessWithExitCode "cpp" ["-v"] "")
|
||||
withHsBindgen
|
||||
def
|
||||
{ clang = def{extraIncludeDirs = [Pkg "rust/target/debug"]}
|
||||
{ clang = def{extraIncludeDirs = Pkg "rust/target/debug" : systemDirs}
|
||||
, fieldNamingStrategy = OmitFieldPrefixes
|
||||
}
|
||||
def
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
{-# LANGUAGE PatternSynonyms #-}
|
||||
|
||||
-- TODO automate this sort of high level wrapper boilerplate
|
||||
-- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22
|
||||
module GarnetRs.Wrapped (
|
||||
@ -64,27 +66,27 @@ withBTree =
|
||||
Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr)
|
||||
|
||||
hello :: ByteString -> IO ()
|
||||
hello bs = useAsCString bs \ptr -> Raw.hello $ unsafeFromPtr ptr
|
||||
hello = flip useAsCString $ Raw.hello . unsafeFromPtr
|
||||
|
||||
helloStruct :: T -> IO ()
|
||||
helloStruct t = with (convertT t) \ptr -> Raw.hello_struct $ unsafeFromPtr ptr
|
||||
helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
|
||||
|
||||
helloEnum :: Raw.E -> IO ()
|
||||
helloEnum e = with e \ptr -> Raw.hello_enum $ unsafeFromPtr ptr
|
||||
helloEnum = flip with (Raw.hello_enum . unsafeFromPtr)
|
||||
|
||||
helloShape :: Shape -> IO ()
|
||||
helloShape s = with (convertShape s) \ptr -> Raw.hello_shape $ unsafeFromPtr ptr
|
||||
helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape
|
||||
|
||||
add :: Int64 -> Int64 -> Int64
|
||||
add = Raw.add
|
||||
|
||||
sumTree :: BTree Int64 -> Int64
|
||||
sumTree t = unsafePerformIO $ withBTree t \tc -> with tc \ptr -> Raw.sum_tree $ unsafeFromPtr ptr
|
||||
sumTree = unsafePerformIO . flip withBTree (flip with $ Raw.sum_tree . unsafeFromPtr)
|
||||
|
||||
sumSlice :: V.Vector Int64 -> Int64
|
||||
sumSlice v = unsafePerformIO $ V.unsafeWith v \ptr -> Raw.sum_slice (unsafeFromPtr ptr) (fromIntegral $ V.length v)
|
||||
sumSlice v = unsafePerformIO $ V.unsafeWith v \p -> Raw.sum_slice (unsafeFromPtr p) (fromIntegral $ V.length v)
|
||||
|
||||
printOptional :: Maybe Int8 -> IO ()
|
||||
printOptional = \case
|
||||
Nothing -> Raw.print_optional $ unsafeFromPtr nullPtr
|
||||
Just t -> with t \ptr -> Raw.print_optional $ unsafeFromPtr ptr
|
||||
Nothing -> Raw.print_optional (unsafeFromPtr nullPtr)
|
||||
Just t -> with t (Raw.print_optional . unsafeFromPtr)
|
||||
|
||||
54
project.nix
Normal file
54
project.nix
Normal file
@ -0,0 +1,54 @@
|
||||
{ hls-src
|
||||
, hs-bindgen-src
|
||||
, libclang-src
|
||||
, garnet-rs
|
||||
}:
|
||||
{
|
||||
name = "garnet";
|
||||
src = ./.;
|
||||
compiler-nix-name = "ghc914";
|
||||
source-repository-packages = {
|
||||
# not on Hackage yet: https://well-typed.com/blog/2026/02/hs-bindgen-alpha
|
||||
# we're using more recent versions than in that post, because we want record-dot support:
|
||||
# https://github.com/well-typed/hs-bindgen/issues/1829#issuecomment-4081875451
|
||||
c-expr-dsl = hs-bindgen-src + "/c-expr-dsl";
|
||||
c-expr-runtime = hs-bindgen-src + "/c-expr-runtime";
|
||||
hs-bindgen = hs-bindgen-src + "/hs-bindgen";
|
||||
hs-bindgen-runtime = hs-bindgen-src + "/hs-bindgen-runtime";
|
||||
libclang-bindings = libclang-src;
|
||||
};
|
||||
overrides = [
|
||||
({ pkgs, ... }: {
|
||||
packages.libclang-bindings.components.library = {
|
||||
build-tools = [ pkgs.llvmPackages.llvm ];
|
||||
libs = [ pkgs.llvmPackages.libclang ];
|
||||
};
|
||||
})
|
||||
(_: {
|
||||
packages.garnet.components.library = {
|
||||
preConfigure = ''
|
||||
mkdir -p rust/target/debug
|
||||
ln -s ${garnet-rs}/include/garnet_rs.h rust/target/debug/garnet_rs.h
|
||||
ln -s ${garnet-rs}/lib/libCgarnet_rs.a rust/target/debug/libCgarnet_rs.a
|
||||
'';
|
||||
preInstall = ''
|
||||
cp ${garnet-rs}/lib/libCgarnet_rs.a dist/build/libCgarnet_rs.a
|
||||
'';
|
||||
configureFlags = [ "--extra-lib-dirs=${garnet-rs}/lib" ];
|
||||
};
|
||||
})
|
||||
];
|
||||
shell = {
|
||||
tools = {
|
||||
cabal = "latest";
|
||||
haskell-language-server = {
|
||||
src = hls-src;
|
||||
sha256map = {
|
||||
"https://github.com/snowleopard/alga"."d4e43fb42db05413459fb2df493361d5a666588a" = "0s1mlnl64wj7pkg3iipv5bb4syy3bhxwqzqv93zqlvkyfn64015i";
|
||||
};
|
||||
};
|
||||
};
|
||||
withHoogle = false;
|
||||
withHaddock = true;
|
||||
};
|
||||
}
|
||||
@ -11,7 +11,7 @@ fn say_hello(name: &str) {
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn hello(c: *const c_char) {
|
||||
extern "C" fn hello(c: *const c_char) -> () {
|
||||
say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap())
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ struct T {
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn hello_struct(t: &T) {
|
||||
extern "C" fn hello_struct(t: &T) -> () {
|
||||
say_hello(&format!("{:?}", t))
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ enum Shape {
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn hello_shape(s: &Shape) {
|
||||
extern "C" fn hello_shape(s: &Shape) -> () {
|
||||
say_hello(&format!("{:?}", s))
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user