haxiomic / haxe-c-bridge

Easily interact with haxe classes from C with an automatically generated C header
MIT License
51 stars 5 forks source link

Extern generator for C #15

Open haxiomic opened 3 years ago

haxiomic commented 3 years ago

need to be able to call into C code from haxe with automatically generated externs and bindings

ideally as a macro but otherwise as a command line tool

PXshadow commented 3 years ago

I really like the field operator macro style, a reference from the cgo docs for golang's c bridge looks similar.

package main

// typedef int (*intFunc) ();
//
// int
// bridge_int_func(intFunc f)
// {
//      return f();
// }
//
// int fortytwo()
// {
//      return 42;
// }
import "C"
import "fmt"

func main() {
    f := C.intFunc(C.fortytwo)
    fmt.Println(int(C.bridge_int_func(f)))
    // Output: 42
}

I also think header file identification for each declaration adds potentially an extra layer of unneeded complexity.

haxiomic commented 3 years ago

Sweet, I’d not see the go bridge before but I will look into it for inspiration!

haxiomic commented 3 years ago

Ideally I’d like to be able to generate externs automatically from c headers - perhaps a c parser as an ocaml eval plug-in? https://github.com/let-def/C11parser

PXshadow commented 3 years ago

I found a few others. This does c headers to json: https://github.com/jbreeden/clang2json

There's also some binding generators from what I've seen use: https://github.com/doxygen/doxygen

On the go front they are using this: https://github.com/xlab/c-for-go

I hope it helps.

mundusnine commented 3 years ago

Maybe tree-sitter would be a good fit ? it would support c and c++ headers if I am not mistaken. libclang could also work but it's a huge dependency(it is in the end the clang compiler used as a library).

haxiomic commented 3 years ago

That's a good suggestion! I've never heard of tree-sitter but I like that it's dependency free C! I'll give it a spin, thank you :)

I made a start on this using libclang, indeed it's a huge dependency so I'd have to ship compiled binaries for each platforms (got them down to about 10mb each)

I'd love something simple and slim enough that it could enable auto-complete into C header files

I saw @aurel300 is working on a libclang extern generator too

Aurel300 commented 3 years ago

Yes, I did a very preliminary test with ammer binding to libclang to parse header files. I think using an off-the-shelf compiler for parsing C (and other supported languages) is the way to go when generating more ammer definitions.

This is not quite the same use case as just wanting completion from header files. Nevertheless, if external C libraries are used via ammer, then the ammer definitions can be made once (with the relatively heavy-duty clang toolchain involved), generating regular Haxe files that then provide completion as expected.

I am happy to collaborate on this with somebody, although it is a busy time for me right now.

haxiomic commented 3 years ago

That's really cool! I think the value from just being able to use C-header projects like sokol and stb headers frictionlessly in haxe is massive. If we can use also binary libs via ammer that's even better!

I'll open up my repo when I get back to this – similarly, other projects have taken up my schedule right now

The main challenge has been just figuring out the libclang API, I started traversing the AST ok, but next I need figure out exploring type information

mundusnine commented 3 years ago

Or better yet, use Kinc directly and implement most of the engine core in C and game logic scripted in haxe(well thats my goal upon seeing your wonderful lib @haxiomic).

Btw I was also thinking of using libclang for kmake(kincmake and khamake replacement), and as you said generating the binaries and maintaining that seems like a pain.