Compare commits
19 Commits
c1d30b0e61
...
4a8a3750fe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a8a3750fe | ||
|
|
7853204758 | ||
|
|
a9c69c4927 | ||
|
|
14e0e85823 | ||
|
|
ca99358747 | ||
|
|
81793d5171 | ||
|
|
ea096b6945 | ||
|
|
356385cbd2 | ||
|
|
e2854bd486 | ||
|
|
57518cc3dc | ||
|
|
b2eaaf6b88 | ||
|
|
351640dc7a | ||
|
|
987fc65a34 | ||
|
|
705d604fdb | ||
|
|
6f58488033 | ||
|
|
00ffcc3df9 | ||
|
|
963b90f627 | ||
|
|
322ad4ee73 | ||
|
|
958332857d |
63
flake.nix
63
flake.nix
@ -13,20 +13,70 @@
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
outputs =
|
outputs =
|
||||||
inputs@{ nix-haskell, ... }:
|
inputs:
|
||||||
inputs.flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
|
inputs.flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
|
||||||
let
|
let
|
||||||
pkgs = (import inputs.nixpkgs { inherit system; }).extend (import inputs.rust-overlay);
|
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 (
|
crane = (inputs.crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.selectLatestNightlyWith (
|
||||||
toolchain: toolchain.default.override {
|
toolchain: toolchain.default.override {
|
||||||
extensions = [ "rust-src" ];
|
extensions = [ "rust-src" "rust-analyzer" ];
|
||||||
targets = [ "x86_64-unknown-linux-gnu" ];
|
targets = [ "x86_64-unknown-linux-gnu" ];
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
project =
|
addLibcIncludeDir =
|
||||||
(nix-haskell (import ./project.nix { inherit (inputs) hls-src hs-bindgen-src libclang-src; inherit garnet-rs; })
|
''
|
||||||
).project.haskell-nix;
|
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;
|
||||||
garnet-rs = crane.buildPackage {
|
garnet-rs = crane.buildPackage {
|
||||||
src = crane.cleanCargoSource ./rust;
|
src = crane.cleanCargoSource ./rust;
|
||||||
doCheck = false;
|
doCheck = false;
|
||||||
@ -52,7 +102,6 @@
|
|||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
bacon
|
bacon
|
||||||
ghcid
|
ghcid
|
||||||
rust-analyzer
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@ -29,6 +29,7 @@ common common
|
|||||||
NoMonomorphismRestriction
|
NoMonomorphismRestriction
|
||||||
OverloadedRecordDot
|
OverloadedRecordDot
|
||||||
OverloadedStrings
|
OverloadedStrings
|
||||||
|
PatternSynonyms
|
||||||
RecordWildCards
|
RecordWildCards
|
||||||
ViewPatterns
|
ViewPatterns
|
||||||
ghc-options:
|
ghc-options:
|
||||||
|
|||||||
@ -29,18 +29,9 @@ import HsBindgen.TH
|
|||||||
import Language.Haskell.TH
|
import Language.Haskell.TH
|
||||||
import System.Process
|
import System.Process
|
||||||
|
|
||||||
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
|
withHsBindgen
|
||||||
def
|
def
|
||||||
{ clang = def{extraIncludeDirs = Pkg "rust/target/debug" : systemDirs}
|
{ clang = def{extraIncludeDirs = [Pkg "rust/target/debug"]}
|
||||||
, fieldNamingStrategy = OmitFieldPrefixes
|
, fieldNamingStrategy = OmitFieldPrefixes
|
||||||
}
|
}
|
||||||
def
|
def
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
{-# LANGUAGE PatternSynonyms #-}
|
|
||||||
|
|
||||||
-- TODO automate this sort of high level wrapper boilerplate
|
-- 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
|
-- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22
|
||||||
module GarnetRs.Wrapped (
|
module GarnetRs.Wrapped (
|
||||||
@ -66,27 +64,27 @@ withBTree =
|
|||||||
Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr)
|
Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr)
|
||||||
|
|
||||||
hello :: ByteString -> IO ()
|
hello :: ByteString -> IO ()
|
||||||
hello = flip useAsCString $ Raw.hello . unsafeFromPtr
|
hello bs = useAsCString bs \ptr -> Raw.hello $ unsafeFromPtr ptr
|
||||||
|
|
||||||
helloStruct :: T -> IO ()
|
helloStruct :: T -> IO ()
|
||||||
helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
|
helloStruct t = with (convertT t) \ptr -> Raw.hello_struct $ unsafeFromPtr ptr
|
||||||
|
|
||||||
helloEnum :: Raw.E -> IO ()
|
helloEnum :: Raw.E -> IO ()
|
||||||
helloEnum = flip with (Raw.hello_enum . unsafeFromPtr)
|
helloEnum e = with e \ptr -> Raw.hello_enum $ unsafeFromPtr ptr
|
||||||
|
|
||||||
helloShape :: Shape -> IO ()
|
helloShape :: Shape -> IO ()
|
||||||
helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape
|
helloShape s = with (convertShape s) \ptr -> Raw.hello_shape $ unsafeFromPtr ptr
|
||||||
|
|
||||||
add :: Int64 -> Int64 -> Int64
|
add :: Int64 -> Int64 -> Int64
|
||||||
add = Raw.add
|
add = Raw.add
|
||||||
|
|
||||||
sumTree :: BTree Int64 -> Int64
|
sumTree :: BTree Int64 -> Int64
|
||||||
sumTree = unsafePerformIO . flip withBTree (flip with $ Raw.sum_tree . unsafeFromPtr)
|
sumTree t = unsafePerformIO $ withBTree t \tc -> with tc \ptr -> Raw.sum_tree $ unsafeFromPtr ptr
|
||||||
|
|
||||||
sumSlice :: V.Vector Int64 -> Int64
|
sumSlice :: V.Vector Int64 -> Int64
|
||||||
sumSlice v = unsafePerformIO $ V.unsafeWith v \p -> Raw.sum_slice (unsafeFromPtr p) (fromIntegral $ V.length v)
|
sumSlice v = unsafePerformIO $ V.unsafeWith v \ptr -> Raw.sum_slice (unsafeFromPtr ptr) (fromIntegral $ V.length v)
|
||||||
|
|
||||||
printOptional :: Maybe Int8 -> IO ()
|
printOptional :: Maybe Int8 -> IO ()
|
||||||
printOptional = \case
|
printOptional = \case
|
||||||
Nothing -> Raw.print_optional (unsafeFromPtr nullPtr)
|
Nothing -> Raw.print_optional $ unsafeFromPtr nullPtr
|
||||||
Just t -> with t (Raw.print_optional . unsafeFromPtr)
|
Just t -> with t \ptr -> Raw.print_optional $ unsafeFromPtr ptr
|
||||||
|
|||||||
54
project.nix
54
project.nix
@ -1,54 +0,0 @@
|
|||||||
{ 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)]
|
#[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())
|
say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ struct T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
extern "C" fn hello_struct(t: &T) -> () {
|
extern "C" fn hello_struct(t: &T) {
|
||||||
say_hello(&format!("{:?}", t))
|
say_hello(&format!("{:?}", t))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ enum Shape {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
extern "C" fn hello_shape(s: &Shape) -> () {
|
extern "C" fn hello_shape(s: &Shape) {
|
||||||
say_hello(&format!("{:?}", s))
|
say_hello(&format!("{:?}", s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user