============================
Python-SWIG extension sample
============================

Introduction
============
This folder contains a set of sample program how to use SWIG to
make python extension of C++ code.

Files
=====
The library swigpy is extended to python.

================  =====================================================
CMakeLists.txt    For compilation for cmake to make Makefile.
----------------  -----------------------------------------------------
Makefile.manual   For compilation.  make -f Makefile.manual
----------------  -----------------------------------------------------
README.txt        This file, in restructured document format.
                  *rst2html README.txt > README.html*
                  helps formatting.
----------------  -----------------------------------------------------
SwigPy.py         Automatically generated python code for reference..
----------------  -----------------------------------------------------
main.cpp          A sample main file.  This calls library of swigpy.
----------------  -----------------------------------------------------
main.py           A sample main python code.  This calls extended
                  library of swigpy.
----------------  -----------------------------------------------------
setup.py          Python installation program.
----------------  -----------------------------------------------------
swigpy.cpp        Implementation of swigpy library.
----------------  -----------------------------------------------------
swigpy.h          Header file of swigpy library.
----------------  -----------------------------------------------------
swigpy.i          Interface file of swigpy library for SWIG.
================  =====================================================

How to compile
==============

There are 4 ways found to compile SwigPy module.
Sometimes you may find "unexpected" failure, especially
if you are installing several python execution, so please
try-and-error which way si the best in your environment.

CMake
-----
Using cmake is straightforward.::

  $ cmake .
  $ make

Manually
--------
Using prepared makefile.::

  $ make -f Makefile.manual

setup.py
--------
Using setup.py, you can compile the module as well.::

  $ swig -c++ -python swigpy.i
  $ python setup.py build
  $ cp build/lib.macosx-10.6-universal-2.6/* .

Step by step
------------
First of all, try to find the library in C++ works.

Compile main.cpp together with swigpy.cpp as::

  $ g++ -o main.exe swigpy.cpp main.cpp

Run it::

  $ ./main.exe
  show() in SimpleTest class are triggered

Next, use SWIG.

  $ swig -c++ -python Sadppac.i

SWIG generates *swigpy_wrap.cxx* and *SwigPy.py*.

Wrapper file and implementation of libray shoudl be compiled into library::

  $ g++ -fPIC -c swigpy_wrap.cxx swigpy.cpp -I/System/Library/Frameworks/Python.framework/Headers
  $ g++ -O2 -dynamiclib -o _SwigPy.so swigpy.o swigpy_wrap.o -framework Python

The second line changes according to OS or version.
For example, older Mac or linux should be::

  $ ld -dylib -flat_namespace -undefined suppress *.o -o _SwigPy.so -framework Python -framework CoreFoundation
  $ g++ -shared -o _SwigPy.so *.o

Anyway, _SwigPy.so is compiled.  Then simply check it::

  $ ipython
  >>> import SwigPy
  >>> x = SwigPy.SimpleText()
  >>> x.show()
  show() in SimpleTest class are triggered



How to execute
==============
Just execute main.py::

  $ python main.py
  show() in SimpleTest class are triggered

