(WIP) Generate C FFI bindings by parsing the clang AST dump.
As it stands, this is just my small effort in automating some of the work for https://github.com/bradrn/wlhs. The point for the time being is just to handle the trivial stuff automatically, and keep this tool as simple as possible.
I figure that, at some point in the future, this could emit proper hsc files, but for now I’m emitting the bespoke templating language used in WLHS.
clang -I/path/to/includes \
-DANY_NECESSARY_DEFINES \
-x c -Xclang -ast-dump=json -fsyntax-only \
header_file_to_bind_to.h > header_file_to_bind_to.json
ast2hsc header_file_to_bind_to.jsonI’m not sure if there are any substantial differences in the AST between recent versions of clang, but I’ve been using clang-18.
If you’d like to help, there’s still so much that can be done. In terms of functionality:
- Supporting nested structs is the biggest priority
- More conversions in convertType
- Remove dependency on aeson-lens
- Start adding options for rendered output (genuine hsc instead of just the templating language)
- Tests! I’m not sure how I want to do these. Ideally, they call compiled C code.
- Longer term: import all that’s needed. Instead of just generating bindings to what’s in a single header, bind recursively to all of the bindings that those bindings depend on.
For formatting I’ve been using ormolu.
I’m still a noob when it comes to Haskell. Any help cleaning up my code would be much appreciated.