Skip to content

Generating libsrcml Python bindings using SWIG

Michael L. Collard edited this page Jul 25, 2017 · 12 revisions
  • Install swig. For MacOS:

    % brew install swig
    ...
    % swig -version
  • Going to have to create some files. These files can be found in a gist

    Makefile:

    all:
      swig -Wall -python srcml.i
      gcc -fPIC -Wall -Wextra -shared srcml_wrap.c -o _srcml.so -L/usr/local/lib/ -lsrcml -I/usr/include/python2.7/ -lpython2.7
    
    clean:
      rm -f srcml_wrap.c _srcml.so srcml.py srcml_wrap.c

    srcml.i

    %module srcml
    
    %{
    #include "srcml.h"
    %}
    
    %include "srcml.h"

    Also going to have to get the srcml.h that you want, e.g.,

    cp ~/srcML/src/libsrcml/srcml.h .

    So at this point you have the following files:

    $ ls -1
    Makefile
    srcml.h
    srcml.i
  • Now run make

    $ make
    swig -Wall -python srcml.i
    gcc -fPIC -Wall -Wextra -shared srcml_wrap.c -o _srcml.so -L/usr/local/lib/ -lsrcml -I/usr/include/python2.7/ -lpython2.7

    and you should now have the following files:

    $ ls -1
    Makefile
    _srcml.so
    srcml.h
    srcml.i
    srcml.py
    srcml_wrap.c
  • Now you can use the libsrcml functions from python

    % python
    >>> import srcml
    >>> srcml.srcml_version_number()
    9005
    >>> srcml.srcml("srcml.h", "srcml.h.xml")
    0
    >>> srcml.srcml("srcml.h.xml", "srcml2.h")
    0

    If you look on your directory, you should see srcml.h.xml, and srcml2.h, which is identical to srcml.h

Clone this wiki locally