PySide Binding Generation Tutorial: Module 2 Binding libfoo using Shiboken
[toc align_right="yes" depth="3"]
English "French":http://qt-devnet.developpez.com/tutoriels/python/pyside/binding-shiboken/#LIV
- Note: this article is a member of the multipart "PySide Binding Generation Tutorial":http://developer.qt.nokia.com/wiki/Category:LanguageBindings::PySide::Shiboken::PySide_Binding_Generation_Tutorial
Binding libfoo using Shiboken
In order to create bindings for a library based on Qt, a number of components must be available on the system:
- Qt library (with headers and pkg-config <code>.pc</code> files for development – the -dev packages in a Debian-like distribution).
- Qt Python bindings made with Shiboken, i.e. PySide.
- Type systems for the Qt Python bindings.
- Headers for the library to be wrapped.
With the items listed above the developer must collect information used by the generator to create the binding source code:
- Type system file describing the way the binding must be done.
- <code>global.h</code> including all the libfoo headers and defining required macros.
- A build system to direct the process of generating, compiling and linking the binding.
libfoo bindings with Makefile
The directory structure and contents for the Makefile binding project could be something like the tree shown below:
<br />foobinding-makefile/<br />|— foo/<br />|— global.h<br />|— Makefile<br />|— pyside_global.h<br />`— typesystem_foo.xml<br />
The <code>foobinding-makefile</code> directory should contain the <code>global.h</code>, which is a central point to include all the libfoo headers (ok, it's just one header, but a real life library will certainly have more headers), it could also contain #define clauses that will influence the parsing of the headers by the generator. Nevermind the <code>pyside_global.h</code> file, it'll be explained later. The <code>typesystem_foo.xml</code> describes how to export the wrapped C++ library to Python as explained in "Module 3":https://developer.qt.nokia.com/wiki/PySide_Binding_Generation_Tutorial:_Module_3_Creating_Type_System_Description.
The <code>foo</code> directory is the place where the generated sources will be placed. It starts empty and its name will be the same as the package name found in the type system file:
<br /><typesystem package="foo&quot;><br />
If there is any need for handwritten source code longer than a couple of lines, making it inconvenient to put them in the type system xml file, the sources could be orderly placed in a <code>glue</code> directory. For the foo bindings no custom code will be needed.
When writing the type system file (more on this later&#41; there is no need to refer to the other required type system files with absolute paths, the locations where they can be found could be passed to the generator through a command line option (—typesystem-paths=PATH1:PATH2:[…]) or the environment variable TYPESYSTEMPATH.
libfoo bindings with CMake
The directory structure for the CMake based libfoo bindings is a bit more elaborate:
<br />foobinding-cmake/<br />|— CMakeLists.txt<br />|— foo/<br />| |— CMakeLists.txt<br />| |— global.h<br />| |— pyside_global.h<br />| `— typesystem_foo.xml<br />`— tests/<br /> `— CMakeLists.txt<br /> `— math_test.py<br />