Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extern generator for C #15

Open
haxiomic opened this issue Jan 14, 2021 · 9 comments
Open

Extern generator for C #15

haxiomic opened this issue Jan 14, 2021 · 9 comments
Labels
enhancement New feature or request

Comments

@haxiomic
Copy link
Owner

haxiomic commented Jan 14, 2021

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
Copy link
Contributor

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
Copy link
Owner Author

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

@haxiomic
Copy link
Owner Author

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
Copy link
Contributor

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.

@haxiomic haxiomic added the enhancement New feature or request label Jan 31, 2021
@mundusnine
Copy link

mundusnine commented Apr 13, 2021

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
Copy link
Owner Author

haxiomic commented Apr 13, 2021

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

@haxiomic haxiomic changed the title Explore macro to make calling C functions in haxe easier Extern generator for C Apr 13, 2021
@Aurel300
Copy link

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
Copy link
Owner Author

haxiomic commented Apr 13, 2021

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
Copy link

mundusnine commented Apr 13, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants