scala-native / scala-native-bindgen

Scala Native Binding Generator

Home Page:https://scala-native.github.io/scala-native-bindgen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add sqlite binding

jonas opened this issue · comments

I would like to generate bindings for library which is using SQLite.
My first problem is that "sqlite3.h" includes stdarg.h like this: #include <stdarg.h>.
Thus when I call "sbt compile" on my project sbt complains that stdarg.h is missing.

So as a hack I copy pasted stdarg.h in my project and I changed the include to #include "stdarg.h".
But I would like to know what would be the proper way to do this with the plugin configuration.

Then, after having performed the hack above the situation is worse because 118 errors are emitted.
Any experience relative to SQLite bindings generation?

Thanks.

Hi @david-bouyssie, I'll take a look at your problem tomorrow

Would be awesome.
In the meantime I found a way to only expose the function of my library without exposing the sqlite library.
But I think it would be nice if sqlite bindings generation could work properly.

I started work on sqlite bindings back in August. I just pushed my WIP branch https://github.com/scala-native/scala-native-bindgen/compare/sqlite-binding

I don't remember if I got any tests working, but you could use it as a starting point. As long as you use the docker build infrastructure for generating the bindings (which might not be well documented) you shouldn't run into issues with stdarg.h.

Thank you so much.
At least even if I cannot generate my own bindings I can use your definitions.
About the ".so" files how do I specify the location of my library?
I searched in the scala-native documentation (https://scala-native.readthedocs.io/en/v0.3.8/user/interop.html#linking-with-native-libraries) but it is not that clear.

For sbt you can use the nativeLinkingOptions setting (https://scala-native.readthedocs.io/en/v0.3.8/user/sbt.html#sbt-settings-and-tasks) to pass non-standard /lib folders.

Hi.

I have some good progress regarding SQLite bindings usage.
I think I could provide a wrapper soon.

I have however some issues with some specific C typedefs.
For instance these types were not translated automatically by bindgen:

typedef void (*sqlite3_destructor_type)(void*);
#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)

And I don't know how to do it manually. Do I have to define a C function pointer?
The signature of this parameter in sqlite3_bind_blob is:
CFunctionPtr1[native.Ptr[Byte], Unit]

But I don't figure out how the SQLite library will understand the function pointer definition.

Additionally I also observed that sqlite3_win32_xxx functions and SQLITE_WIN32_XXX constants were not exported by bindgen.

I'm surprised that the typedef is not translates. For the macro, it is supposed to handle constants, but those macros looks like black magic. 🙀

If we cannot fix this in the bindings generator, one option would be to allow some custom code to be included.

Actually I had a short discussion with @densh on gitter.
He suggested to do:

val SQLITE_STATIC = 0.cast[CFunctionPtr1[Ptr[Byte], Unit]]
val SQLITE_TRANSIENT = 1.cast[CFunctionPtr1[Ptr[Byte], Unit]]

But with the tag 0.3.8 this trick doesn't seem to work.
I end up with a java.lang.RuntimeException: Nonzero exit code.

I didn't try with the latest version of the github master branch.
So I don't know if it works or not.

Moreover I don't know why but the section named CAPI3REF: Extended Result Codes of the sqlite.h contains several defines (from SQLITE_IOERR_READ to SQLITE_READONLY_CANTLOCK ) which are not exported by bindgen. Weird isn't it?

FYI, sqlite4s 0.1.0 is now out 😄
See david-bouyssie/sqlite4s