Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Automatic C binding generation


Seamless C interop is really a killer feature. Thanks for the hard work.

On Thu, Sep 13, 2018 at 9:21 PM Ori Bernstein <ori@xxxxxxxxxxxxxx> wrote:

> I've got automatic C binding generation working enough for
> demoware! I can compile and link to libsqlite3 and libc,
> and interface with it using entirely too much casting.
>
> The implementation lives here:
>
>         https://git.eigenstate.org/ori/cbind.git/tree/
>
> And installs a binary named cbind, which takes these
> arguments:
>
>      cbind [-h?] [-b pkg] [-I inc] [-D def] [-l lib] file.h...
>          -h      print this help message
>          -?      print this help message
>          -b pkg  generate bindings
>          -I inc  add 'inc' to your include path
>          -D def  define a macro in the preprocessor e.g. -Dfoo=bar
>          -l lib  link against library 'lib'
>
> You can use it like this:
>
>         cbind -lc -lsqlite3 \
>                 -I /usr/include -I/usr/local/include \
>                 -D__GNUCLIKE_BUILTIN_STDARG \
>                 -b sqlite3 \
>                 sqlite3.h
>
> That command generates a rather giant module with all of the
> sqlite3 types (and everything included by sqlite3.h) shoved
> into one sqlite3 module.
>
> You can call it like this:
>
>         use "sqlite3"
>
>         const main = {
>                 rc = sqlite3.sqlite3_open(("db.sqlite\0" : byte#), &db)
>                 if rc != 0
>                         std.fatal("error: {}\n", rc)
>                 ;;
>                 rc = sqlite3.sqlite3_exec(db,
>                     ("SELECT * FROM table;\0" : byte#),
>                     (callback : sqlite3.cfunc#),
>                     (0 : void#), &errmsg)
>                 if rc != 0
>                         std.fatal("error: {} ({})\n", rc,
> std.cstrconvp(errmsg))
>                         sqlite3.sqlite3_free((errmsg : void#))
>                 ;;
>                 sqlite3.sqlite3_close(db)
>         }
>
> Note that the strings are explicitly null terminated, and
> cast to a C string explicitly. This is one pain point that
> would be nice to address.
>
> I've updated the cbind example code to reflect the current
> state of the art:
>
>         https://git.eigenstate.org/ori/cbind-example.git
>
> It's been tested on one platform with a small handful of
> headers, so expect bugs.
>
> Most of the code in this was written by people other than
> me.  Thanks to Andrew, who wrote the C compiler that this is
> based off of, and to Frank who laid the groundwork for this.
>
> --
>     Ori Bernstein
>
>

References:
Automatic C binding generationOri Bernstein <ori@xxxxxxxxxxxxxx>