Tag Archives: SWIG

SWIG for Native Boost FFI

Ronnie Salgado announced SWIG for NatvieBoost today:

I started working on extending SWIG to generate bindings for NativeBoost FFI, using as a reference the C# binding generator. I am committing my work on this into https://github.com/ronsaldo/swig/ .

Note: SWIG is a tool designed to create bindings, for which you provide an interface descripitions, which usually includes your C/C++ headers, and you provide some SWIG specific directives for improving the generated bindings.
It produces C side module and a language specific module. The C side is required, since some C stuff can only be detected by an actual C compiler. In these matter, maybe TalkFFI is better, because is based in an actual compiler(clang), but maybe you lose some bindings fine tuning.

The problem is when you add C++ to the mix. In C++ there’s not a standard defined ABI, so a module made in one C++ compiler is almost for sure going to be incompatible with a module made by another C++ compiler. For making bindings to a C++ module, you have to first make C bindings to that module, compile those bindings with the same compiler that was used for the original module and then you can start making NativeBoost bindings. This is the only portable way for C++.

For example, gcc and Microsoft Visual C++ compilers are completely incompatible for C++, but for C they can interact. Clang is apparently designed to use the same ABI as gcc, so maybe it could be used in Linux, Mac but not for a closed source library in Window.

What SWIG does is generating the C bindings and Smalltalk code that calls the C bindings ready to be filed in. In addition, you can add extra code to the bindings to make things that hard to do using directly NativeBoost but can be easy to do with an intermediate function in C, for example messing with some unions and things that required lot of pointer arithmetic, that could be used maybe for a bad written library that some project needs.

SWIG works by creating a C side code and Smalltalk that can be filed-in in a image, which contains a class with all the NativeBoost-FFI callouts, a class for the global module, classes for each C++ class and SWIGTYPE_* classes for opaque types or types that SWIG couldn’t wrap.

It currently can generate bindings for C/C++ functions, constants, define based constants, opaque structures, methods, enums, and for C++ classes, nested classes, member methods, member fields, member constants, member static functions, variables and constants.

The overloaded functions support currently is not so great, since I am currently using the arguments name to build the selector. I have to still decide a better strategy for the overloaded methods.

WARNING: automatic garbage collection of the C++ classes is not yet ready, but it will be soon. Downcasting of C++ objects is also not supported yet.

Greetings
Ronie Salgado

Advertisements
Tagged , ,