Compare commits

...

2 Commits

Author SHA1 Message Date
George Thomas
7bbba202af basic enum example 2026-03-30 22:44:31 +01:00
George Thomas
209f674541 wip file dependency reload stuff 2026-03-30 22:44:31 +01:00
7 changed files with 63 additions and 0 deletions

View File

@ -9,6 +9,8 @@ main = do
hello "Haskell"
helloStruct T{a = True, b = 42}
helloStruct T{a = False, b = maxBound}
helloEnum E1
helloEnum E3
helloShape $ Circle 3.14
helloShape $ Rectangle 10.0 5.0
putStrLn $ "3 + 4 = " <> show (add 3 4)

View File

@ -6,6 +6,13 @@ author: Patrick Aldis
maintainer:
george.thomas@obsidian.systems
patrick.aldis@obsidian.systems
-- aha, nice, this does fix recompilation checking
extra-source-files:
rust/target/debug/garnet_rs.h
-- that could be problematic given file is gitignored? unfortunately this doesn't work
-- tbf, I haven't even looked up the docs, just saw the autocompletion
-- extra-tmp-files:
-- rust/target/debug/garnet_rs.h
common common
default-language: GHC2024
@ -43,6 +50,9 @@ library
GarnetRs.Wrapped
hs-source-dirs: lib
include-dirs: rust/target/debug
-- HLS gives up entirely when the header is malformed if we do this
-- and anyway, I don't think it gives us dependency tracking like `extra-source-files` does
-- includes: garnet_rs.h
extra-bundled-libraries: Cgarnet_rs
build-depends:
hs-bindgen,

View File

@ -29,7 +29,15 @@ import HsBindgen.TH
import Language.Haskell.TH
import System.Process
import Control.Monad.IO.Class (MonadIO (liftIO))
import Language.Haskell.TH.Syntax (addDependentFile)
import System.Directory.Extra (getCurrentDirectory)
do
-- not sure this does anything - hs-bindgen should already be doing the tracking, and the issues are from elsewhere
-- dir <- liftIO getCurrentDirectory
-- liftIO $ print dir
-- addDependentFile $ dir <> "/rust/target/debug/garnet_rs.h"
systemDirs <- -- TODO bit of a hack
map (Dir . T.unpack . T.strip)
. concatMap (takeWhile (maybe False ((== ' ') . fst) . T.uncons) . dropWhile T.null . T.lines)

View File

@ -1,11 +1,20 @@
{-# 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 (
T (..),
Raw.E (..),
-- TODO hmm, we don't really want to have to list all of these...
-- is there an option to make them not be patterns at all?
pattern Raw.E1,
pattern Raw.E2,
pattern Raw.E3,
Shape (..),
BTree (..),
hello,
helloStruct,
helloEnum,
helloShape,
add,
sumTree,
@ -62,6 +71,9 @@ hello = flip useAsCString $ Raw.hello . unsafeFromPtr
helloStruct :: T -> IO ()
helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
helloEnum :: Raw.E -> IO ()
helloEnum = flip with (Raw.hello_enum . unsafeFromPtr)
helloShape :: Shape -> IO ()
helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape

View File

@ -19,6 +19,8 @@
};
overrides = [
({ pkgs, ... }: {
# TODO get this upstreamed:
# https://input-output-hk.github.io/haskell.nix/tutorials/pkg-map.html#mapping-in-haskellnix
packages.libclang-bindings.components.library = {
build-tools = [ pkgs.llvmPackages.llvm ];
libs = [ pkgs.llvmPackages.libclang ];

View File

@ -2,6 +2,22 @@ use std::env;
use std::path::PathBuf;
fn main() {
// this doesn't make _much_ difference really, since this is our only Rust source file
// but it seems it's probably better than not having it
// what we really want is to tell Rust to only regenerate the header file if the Rust code actually compiles
// but we don't have that flexibility
// and it's an issue because cbindgen tries to be fault-tolerant in some ways that don't even seem to make sense
//
// e.g. mis-spell "Option" as "Option" and you get
// void print_optional(Optio<const int8_t*> x);
// instead of
// void print_optional(const int8_t *x);
// and that's only an issue because in HLS TH dependent-file watching gives up after an error
// i.e. once the containing splice has thrown an exception once, the containing file needs a manual edit to kick it
// P.S. strings to stdout?! what a terrible API
// don't get me started in the discoverability of actually then using the terminal for debugging:
// println!("cargo::warning={:?}", env::var("OUT_DIR"));
println!("cargo::rerun-if-changed=lib.rs");
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let profile = env::var("PROFILE").unwrap();
cbindgen::Builder::new()

View File

@ -27,6 +27,19 @@ extern "C" fn hello_struct(t: &T) {
say_hello(&format!("{:?}", t))
}
#[repr(C)]
#[derive(Debug)]
enum E {
E1,
E2,
E3,
}
#[unsafe(no_mangle)]
extern "C" fn hello_enum(e: &E) {
say_hello(&format!("{:?}", e))
}
#[repr(C)]
#[derive(Debug)]
enum Shape {