SWIG on Mac OS X

From Wiki for iCub and Friends
Jump to: navigation, search

Trying to get the example in $YARP_ROOT/example/swig on Mac OS X Tiger require somes hacks.

Yet to be solved

Well... There was a bus error while running example.py :-( which disappeared when using system original python instead of DarwinPorts one. However the problem itself is worked around, it would be nice to know the reason why it happenend...

The point is that the distutils are able to build a working extention for the Darwin Ports version of Python using SWIG... So the problem seems to be more specificaly linked to the CMake module for SWIG on Mac OS X. I can't provide any solution for now, but anyway for building a Python binding I would consider either of :

  • using Boost::Python
  • using the 'SWIG support in the distutils : beeing a build manager in Python it is portable to any platform on which Python is available -- and if it is not on yours, you are not interested by that anymay ;-)

Below is the stuff about what was tried to make a Python wrapper around YARP using CMake and SWIG on Mac OS X. It finaly worked, but only the the Apple provided old Python Framework which has no useful library :-(

CMakeLists.txt

In .../swig/CMakeLists.txt to should add a line in order the Python section looks like :

...
IF (CREATE_PYTHON)
   SET(CMAKE_SWIG_FLAGS "-Wall;-module;yarp")
   FIND_PACKAGE(PythonLibs)
   INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
   LINK_LIBRARIES(${PYTHON_LIBRARY})
   SWIG_ADD_MODULE(yarp python yarp.i)
ENDIF (CREATE_PYTHON)
...

yarp.i

In the SWIG interface file yarp.i you have to change ... to look like :

// Parse the original header files
// Redefine a few things that SWIG currently chokes on
//%define __LINUX__
%define _YARP2_NETINT32_
%enddef
// %define NetInt32 int
//%enddef
%typedef int yarp::os::NetInt32;

%define _YARP2_VOCAB_ 1
%enddef

FindPythonLibs

This is the standard CMake module to find Python. As I'm using DarwinPorts, Pyhon libs are in /opt/local/lib. You need either to edit directly the module file (/opt/local/share/CMake/Modules for me), or copy it to the swig directory and edit this local copy in order it looks like :

FIND_LIBRARY(PYTHON_LIBRARY
  NAMES python24 python2.4
        python23 python2.3
 ...

  PATHS
    [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\1.5\\InstallPath]/libs
    /opt/local/lib
 ...
)

DO NO DO IT !!! In fact there is a mysterious bus error when using DarwinPorts python 2.4 : use the python provided with the system instead (so you don't need this hack, but as it can be useful I don't erasse it...)