Hi,
Has anyone succeeded in generating python bindings for SystemC, using PyBindGen?
I've managed to run through the process, but can't seem to figure out why I can't find any classes or functions inside the scanned submodules (e.g. sc_core).
I've outlined the procedure that I've followed so far, below. It would be great if someone could help me finish it, but figuring out what's missing.
Overview
This document describes how to generate Python-2.7 bindings for SystemC-2.3 using PyBindGen.
PyBindGen is a Python bindings generator; it is geared to generating C/C++ code that binds a C/C++ library for Python.
PyBindGen is a Python module that is geared to generating C/C++ code that binds a C/C++ library for Python. It does so without extensive use of either C++ templates or C pre-processor macros. It has modular handling of C/C++ types, and can be extended with Python plugins. The generated code is almost as clean as what a human programmer would write.
It was designed to be an alternative to some other bindings generators out there, including:
- Boost::Python, which uses C++ templates extensively, which are difficult to understand or extend, and which generates relatively large Python modules;
- SWIG, which makes extensive use of macros, generates very ugly code, and is itself written in C++, therefore difficult to extend;
- Python SIP, which is also written in C++;
- The PyGtk code generator, which doesn't support wrapping C++ code, and is not very flexible;
Procedure
Step 01.00: Install pre-requisite packages.
Step 01.01: Install SystemC-2.3.0.
Download the systemc library.
http://www.accellera.org/downloads/standards/systemc
Extract systemc-2.3.0.tar.gz to /tool/accelera folder
Configure and build the systemc library.
cd /tool/accelera/systemc-2.3.0
mkdir build; cd build
../configure
In the build directory, modify the systemc-2.3.0/build/src/sysc/kernel/Makefile and remove
am_objects_2 =
sc_main.lo, sc_main_main.lo
CXX_FILES = \
sc_main.cpp \
sc_main_main.cpp \
to avoid issues with unresolved external symbol sc_main, while importing the python module that you will generate, in subsequent steps.
make -j8
make install
The libraries will be installed to the following location by default:
/tool/accelera/systemc-2.3.0/include
/tool/accelera/systemc-2.3.0/lib-linux64
Set the SYSTEMC environment variable to point to the SystemC installation.
export SYSTEMC=/tool/accelera/systemc-2.3.0
Set the LD_LIBRARY_PATH
export LD_LIBRARY_PATH="${SYSTEMC}/lib-linux64:${LD_LIBRARY_PATH}"
Step 01.02: Install gccxml and pygccxml
sudo apt-get install gccxml python-pygccxml
Step 02.00: Install pybindgen.
Step 02.01: Download pybindgen-0.16.0
Step 02.02: Configure and install pybindgen.
cd /tool/pybindgen-0.16.0
./waf configure
./waf
./waf check
sudo ./waf install
Step 02.03: Test pybindgen using the first-example program.
Download the first-example.zip file from
http://pybindgen.googlecode.com/svn/trunk/tutorial/
Modify the makefile to create a shared library using ld, instead of gcc
PYTHON_INCLUDE=/usr/include/python2.7
PYBINDGEN_LOCATION=../../
all: MyModule.so
# build my C library
libmymodule.so: my-module.o
ld -shared -o libmymodule.so my-module.o
my-module.o: my-module.h my-module.c
gcc -fPIC -c -o my-module.o my-module.c
# generate the binding code
my-module-binding.c: my-module.h my-module.py
PYTHONPATH=$$PYTHONPATH:$(PYBINDGEN_LOCATION) python my-module.py > my-module-binding.c
# build the binding code
my-module-binding.o: my-module-binding.c
gcc -fPIC -I$(PYTHON_INCLUDE) -c -o my-module-binding.o my-module-binding.c
# build the final python module
MyModule.so: libmymodule.so my-module-binding.o
ld -shared -o MyModule.so -L. -lmymodule my-module-binding.o
clean:
rm -f *.o *.so my-module-binding.c *~ 2>/dev/null
Step 03.00: Create a pybindgen script to automatically scan and generate the python systemc bindings.
Step 03.01: Create a pybindgen autoscan module.
Filename: pysystemc-autoscan.py
#! /usr/bin/env python
import sys
from pybindgen import FileCodeSink
from pybindgen.gccxmlparser import ModuleParser
def systemc_gen():
module_parser = ModuleParser('pysystemc', '::')
module_parser.parse([sys.argv[1]], includes=['"systemc.h"'], pygen_sink=FileCodeSink(sys.stdout))
if __name__ == '__main__':
systemc_gen()
Create the generator
python pysystemc-autoscan.py systemc.h > pysystemc-codegen.py
Create the python binding file
python pysystemc-codegen.py systemc.h > pysystemc-binding.cc
Compile the python binding file:
c++ -fPIC -I/usr/include/python2.7 -I/tool/accelera/systemc-2.3.0/src -c -o pysystemc-binding.o pysystemc-binding.cxx
Build the shared library:
c++ -shared -o pysystemc.so -L. -L/tool/accelera/systemc-2.3.0/lib-linux64 pysystemc-binding.o -Wl,--whole-archive /tool/accelera/systemc-2.3.0/lib-linux64/libsystemc.a -Wl,--no-whole-archive
After this, test the installation by typing
python
>>> import pysystemc
The sc_core submodule is present in the pysystemc module, but it doesn't have any classes or methods.
Best regards,
Elvis Dowson