PySide Binding Generation Tutorial: Module 5 Building the generator
English French [qt-devnet.developpez.com]
- Note: this article is a member of the multipart PySide Binding Generation Tutorial [developer.qt.nokia.com]
PySide Binding Generation Tutorial
Three Steps to Build the Binding
As mentioned before, the build system used must perform the following tasks in the correct order:
- Gather data about locations of headers and needed type systems from other projects.
- Run the generator with the correct parameters.
- Compile and link the binding.
Gather Information
There are two options to gather data about locations of headers and needed type systems:
Collect Information with pkg-config
The Qt bindings include compile and build information through the pkg-config mechanism. The pkg-config name for Qt Python bindings is
pyside
and a simple
pkg-config pyside —cflags —libs
will retrieve information required to build the new binding. The Qt bindings file
pyside.pc
for the use of pkg-config requires Qt’s
.pc
files to be installed. If the library is in an unusual location, e.g.
/opt/qt47
, remember to export it to the
<span class="caps">PKG</span>_CONFIG_PATH
environment variable. For example:
export <span class="caps">PKG</span>_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/qt47/lib/pkgconfig
Information is also available through pkg-config: the
typesystemdir
variable. It is used like this:
pkg-config pyside —variable=typesystemdir
This provides information where to find the type system files used to create the Qt bindings. As mentioned before, the binding being created needs this to complement its own binding information for the generation proccess. Information from the Shiboken binding generator is also needed for the build, it’s pkg-config name is
shiboken
. More details on this later.
Collect Information with CMake
When building your binding with CMake the relevant information can be included from your project’s
CMakeLists.txt
using: Similarly
ShibokenConfig.cmake
provides needed information:
Run the Generator
The generator is called with the following parameters and options:
Note that the variables for include and type system paths could be determined at build time with the pkg-config tool or with information provided by CMake configuration files.
Build
This section will alternate in presenting the two build methods: Makefile and CMake.
The Makefile Version
Below is a plain Makefile for the binding project.
foobinding-makefile/Makefile
: Keep in mind that the Makefile above expects the
libfoo
and
foobinding-makefile
directories to be in the same level in the directory hierarchy. Remember to change any path references accordingly if you elect to change things.
Build and Test
Now generate, compile and link the binding with make:
The
make test
causes the Python interpreter to run the line
import foo; m = foo.Math(); print ’5 squared is %d’ % m.squared(5)
, which will import the binding module, instantiate the class from it, run a method and print its result (which should be 25).
The CMake Version
foobinding-cmake/CMakeLists.txt
: This is the main project’s
CMakeLists.txt
, it is a regular CMake file and general doubts can be checked in the CMake documentation [cmake.org] Notice the we’re going to have tests in this project so we have to enable them with
enable_testing()
.
foobinding-cmake/foo/CMakeLists.txt
: This is the
CMakeLists.txt
file for the binding directory proper, the
add_custom_command
statement is responsible for the calling of Shiboken generator with the proper parameters and variables. Notice that the command line options
—enable-parent-ctor-heuristic —enable-pyside-extensions —enable-return-value-heuristic
are directly related to Qt bindings idiosyncrasies, for a pure C++ binding none of those will be necessary.
foobinding-cmake/tests/CMakeLists.txt
: This not very elaborate
CMakeLists.txt
informs CMake which tests should be executed, and with which variables.
Build and Test
The best thing to do when building with CMake is to create a build directory and run
cmake
from there.
Ah, let’s not forget the unit test. It’s a very simple one.
foobinding-cmake/tests/math.py
To run the test:
The output will be something like this:
For a more verbose output use
ctest -V
Conclusion
That’s pretty much it. More examples of CMakeLists.txt files and binding unit tests check the PySide sources [qt.gitorious.org].