crystal-lang / crystal_lib

Automatic binding generator for native libraries in Crystal
138 stars 30 forks source link

crystal_lib_gen #2

Open fazibear opened 9 years ago

fazibear commented 9 years ago

Here is my quite working tool for generating C bindings in Crystal Written in ruby, based on gen_ffi gem.

https://github.com/fazibear/crystal_lib_gen

Hope you like it :)

asterite commented 9 years ago

Wow, that's AWESOME!!

I followed the README instructions and it worked flawlessly :-)

I think that eventually we'll want a tool like this to be integrated in the compiler (or at least as a companion to the main binary) because it will make it so much easier to write C bindings. So preferably we would have this written in Crystal, but for now it's really, really good to have this and we will definitely use it and encourage others to use it until we get to the point when it is written in Crystal.

Just a small thing: I tried to run it with libuv and I get an undefined methoddowncase' for nil:NilClass`. I tried to debug it without much luck. Here's the code I was using:

FFIGen.generate(
  module_name: "LibUV",
  ffi_lib:     "libuv",
  headers:     %w[
                uv.h
               ],
  cflags:      ['-I/usr/local/opt/libuv/include/'],
  prefixes:    [],
  output:      "out/libuv.cr"
)

And I installed it with brew install libuv.

And a request: I tried to put "git" on the list of prefixes and it generated the functions like this:

@[Link("libgit2")]
lib Git2
    fun repository_open(out : Void**, path : UInt8*) : Int16
end

Would it be possible to generate this instead:

@[Link("libgit2")]
lib Git2
    fun repository_open = git_repository_open(out : Void**, path : UInt8*) : Int16
end

Because the lib already acts as a namespace and it'll be shorter and easier to write.

Again, thanks!

fazibear commented 9 years ago

You're right! Prefix support added.

This should work to all possibile function names.

@[Link("libgit2")]
lib Git2
    fun repository_open = "git_repository_open"(out : Void**, path : UInt8*) : Int16
end
fazibear commented 9 years ago

I have to look at libuv, this one not work.

asterite commented 9 years ago

That was fast! Thanks!