SWIG and C++ on Solaris

Here at WeSC we have a potentially very useful piece of kit from Cybula which is essentially a set of neural-net based pattern matching algorithms blown into some FPGAs.

Now I have to advise some researchers about how easy it will be to use this kit for a particular project. My ability to advise is limited by the fact that the API for this kit is written in C++. This makes it hard for a mere sysadmin like myself to play with it.

I have been vaguely aware of SWIG for a while as a mechanism for building language-bindings to C and C++ so I decided to give it a whirl.

I installed swig from sunfreeware. After browsing the tutorial for a while I decided to take the simple approach to building an interface file.

%module aura
%{
/* Includes the header in the wrapper code */
#include "aura.h"
%}

/* Parse the header file to generate wrappers */
%include "aura.h"

Then I used swig to generate the python and C++ code.

swig -python -c++ -I/opt/Aura/2.4.2/include aura.i

And built the libraries from that

g++ -shared -I/usr/local/include/python2.4 -I/opt/Aura/2.4.2/include -c aura_wrap.cxx

This left me with aura.py and _aura.so which look like a python module to me.

Python 2.4 (#1, Dec  1 2004, 05:20:08)
[GCC 3.3.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>>import aura

Now I just need to work out how to use it. Assuming this module actually works and isn’t just an empty shell I may have to try building Java and Perl modules later.

Edited: 16:27 to include some bug-fixes

After playing around with this some more I realized that I had indeed created a completely empty binding. So I went through aura.h and manually included all the header files that it pulled in.

%module aura
%{
/* Includes the header in the wrapper code */
#include "aura.h"
#include "auraErrorTable.h"
#include "auraTypes.h"
#include "auraStructures.h"
#include "auraException.h"
#include "auraSearchEngine.h"
%}

/* Parse the header file to generate wrappers */
%include "aura.h"
%include "auraErrorTable.h"
%include "auraTypes.h"
%include "auraStructures.h"
%include "auraException.h"
%include "auraSearchEngine.h"

This produced much larger python and c++ files so I guessed I was on the right track. I also realized that the g++ command wasn’t linking in the aura library and that I would have to do it by hand

g++ -shared -c aura_wrap.cxx -o aura.o -I/usr/local/include/python2.4 -I/opt/Aura/library/include

ld -G -o _aura.so aura.o /opt/Aura/library/lib/SunOS/libaura.a

The path changes were introduced when I realised I was trying to build a 64bit version of libaura into a 32bit version of python.

Python 2.4 (#1, Dec  1 2004, 05:20:08)
[GCC 3.3.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import aura
>>> dir(aura)
['ERROR_CANNOT_READ_STREAM', 'ERROR_CANNOT_WRITE_STREAM', 'ERROR_COLUMN_INVALID',
'ERROR_CONSTRAINTS', 'ERROR_FORMAT_INVALID', 'ERROR_INITIALISED', 'ERROR_MASK_INVALID',
'ERROR_MAX_SCORE', 'ERROR_MEMORY', 'ERROR_MISMATCH', 'ERROR_OPTIMISATION_SETTING_INVALID',
'ERROR_PARTITION_INVALID', 'ERROR_PATTERN_INVALID', 'ERROR_RANGES_INVALID',
'ERROR_RETAINED_ID_INVALID', 'ERROR_ROW_INVALID', 'ERROR_SAME_OBJECT', 'ERROR_SIZES_INVALID',
'ERROR_UNIMPLEMENTED', 'ERROR_UNINITIALISED', 'Exception', 'Exception_swigregister', 'NULL', 'OK',
'Pattern', 'Pattern_swigregister', 'Preferences', 'Preferences_swigregister',
'RetainedInfo', 'RetainedInfo_swigregister', 'SearchCharacteristics', 'SearchCharacteristics_swigregister', 'SearchConstraints',
'SearchConstraints_swigregister', 'SearchControl', 'SearchControl_swigregister', 'SearchEngine', 'SearchEngine_swigregister',
'SerialiseControl', 'SerialiseControl_swigregister', 'Sizes', 'Sizes_swigregister', 'StorageType', 'StorageType_swigregister',
'WARN_COLUMN_IGNORED', 'WARN_NO_BITS_TO_STORE', 'WARN_RETAINED_ID_INVALID',
'WARN_ROW_IGNORED', 'WARN_SHRINKAGE_IGNORED', 'WARN_STORE_CHANGED', 'WARN_TRUNCATED',
'__builtins__', '__doc__', '__file__', '__name__', '_aura', '_newclass', '_object', '_swig_getattr', '_swig_repr',
'_swig_setattr', '_swig_setattr_nondynamic', 'cvar', 'new', 'new_instancemethod']
>>> x = aura.SearchEngine
>>> dir(x)
['ClearRetained', 'Copy', 'GetColumn', 'GetRetainedIds', 'GetRow', 'GetSizes', 'IncreaseSize', 'Initialise',
'Merge', 'Optimise', 'Remove', 'Search', 'Serialise', 'Store', '__class__', '__del__', '__delattr__', '__dict__',
'__doc__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__swig_destroy__', '__swig_getmethods__',
'__swig_setmethods__', '__weakref__']

Result!!