English French [qt-devnet.developpez.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 PKG_CONFIG_PATH environment variable. For example: export PKG_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:

  1. find_package(Shiboken REQUIRED)
  2. find_package(PySide REQUIRED)

Requiring the Shiboken and PySide packages will set the values of a number of variables, according to

  1. PySideConfig.cmake
file:

  1. PYSIDE_INCLUDE_DIR   - Directories to include to use PySide
  2. PYSIDE_LIBRARY       - Files to link against to use PySide
  3. PYSIDE_PYTHONPATH    - Path to where the PySide Python module files could be found
  4. PYSIDE_TYPESYSTEMS   - Type system files that should be used by other bindings extending PySide

Similarly ShibokenConfig.cmake provides needed information:

  1. SHIBOKEN_INCLUDE_DIR        - Directories to include to use SHIBOKEN
  2. SHIBOKEN_LIBRARIES          - Files to link against to use SHIBOKEN
  3. SHIBOKEN_BUILD_TYPE         - Tells if Shiboken was compiled in Release or Debug mode.
  4. SHIBOKEN_PYTHON_INTERPRETER - Python interpreter (regular or debug) to be used with the bindings.
  5. SHIBOKEN_PYTHON_LIBRARIES   - Python libraries (regular or debug) Shiboken is linked against.

Run the Generator

The generator is called with the following parameters and options:

  1. generatorrunner --generatorSet=shiboken \
  2.                 global_header.h \
  3.                 --include-paths=$(PATHS_TO_HEADERS)) \
  4.                 --typesystem-paths=$(PATHS_TO_TYPESYSTEMS) \
  5.                 --output-directory=. \
  6.                 typesystem.xml

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:

  1. LIBFOO_DIR = `pwd`/../libfoo
  2. LIBS = `pkg-config pyside --libs` \
  3.   -L$(LIBFOO_DIR) -lfoo
  4. CXXFLAGS = -I/usr/share/qt4/mkspecs/linux-g++ -I. \
  5.    -I$(LIBFOO_DIR) \
  6.    -I`pwd`/foo \
  7.    -I`pkg-config --variable=includedir pyside`/QtCore/ \
  8.    -I`pkg-config --variable=includedir QtCore` \
  9.    -I`pkg-config --variable=includedir QtCore`/.. \
  10.    -I`pkg-config --variable=includedir QtGui` \
  11.    `pkg-config pyside --cflags`
  12.  
  13. QT4TYPESYSTEM_DIR = `pkg-config pyside --variable=typesystemdir`
  14. QT4HEADER_DIRS = `pkg-config --variable=includedir QtCore`:`pkg-config --variable=includedir QtCore`/..
  15. PYSIDE_PYTHONPATH = `pkg-config --variable=pythonpath PySide`
  16. PYTHON_INTERPRETER = `pkg-config --variable=python_interpreter shiboken`
  17.  
  18. all: generate compile link
  19.  
  20. generate:
  21.  generatorrunner --generatorSet=shiboken \
  22.   global.h \
  23.   --include-paths=$(LIBFOO_DIR):$(QT4HEADER_DIRS):/usr/include \
  24.   --typesystem-paths=.:$(QT4TYPESYSTEM_DIR) \
  25.   --output-directory=. \
  26.   typesystem_foo.xml
  27.  
  28. compile:
  29.  g++ foo/foo_module_wrapper.cpp foo/math_wrapper.cpp -Wall -fPIC $(CXXFLAGS) -c
  30.  
  31. link:
  32.  g++ foo_module_wrapper.o math_wrapper.o $(LIBS) -fPIC -shared -Wl,-soname,foo.so -o foo.so
  33.  
  34. test:
  35.  LD_LIBRARY_PATH=$(LIBFOO_DIR):$(LD_LIBRARY_PATH) PYTHONPATH=$(PYSIDE_PYTHONPATH):$(PYTHONPATH) $(PYTHON_INTERPRETER) -c \
  36.   "import foo; m = foo.Math(); print '5 squared is %d' % m.squared(5)"
  37.  
  38. clean:
  39.  rm -rf *.o *.so *.?pp *.log *.log foo/*

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:

  1. cd foobinding-makefile
  2. make
  3. make test

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:

  1. project(foobinding)
  2.  
  3. cmake_minimum_required(VERSION 2.6)
  4.  
  5. find_package(PythonLibs REQUIRED)
  6. find_package(Shiboken REQUIRED)
  7. find_package(PySide REQUIRED)
  8. find_package(Qt4 4.6.2 REQUIRED)
  9.  
  10. set(LIBFOO_DIR ${CMAKE_SOURCE_DIR}/../libfoo)
  11.  
  12. find_program(GENERATOR generatorrunner REQUIRED)
  13. if (NOT GENERATOR)
  14.     message(FATAL_ERROR "You need to specify GENERATOR variable (-DGENERATOR=value)")
  15. endif()
  16.  
  17. if(CMAKE_HOST_UNIX)
  18.     option(ENABLE_GCC_OPTIMIZATION "Enable specific GCC flags to optimization library size and performance. Only available on Release Mode" 0)
  19.     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fvisibility=hidden -Wno-strict-aliasing")
  20.     set(CMAKE_CXX_FLAGS_DEBUG "-g")
  21.     if(ENABLE_GCC_OPTIMIZATION)
  22.         set(CMAKE_BUILD_TYPE Release)
  23.         set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -Os -Wl,-O1")
  24.         if(NOT CMAKE_HOST_APPLE)
  25.             set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--hash-style=gnu")
  26.         endif()
  27.     endif()
  28.  
  29.     if(CMAKE_HOST_APPLE)
  30.         if (NOT QT_INCLUDE_DIR)
  31.             set(QT_INCLUDE_DIR "/Library/Frameworks")
  32.          endif()
  33.     endif()
  34. endif()
  35.  
  36. include(${QT_USE_FILE})
  37.  
  38. enable_testing()
  39.  
  40. add_subdirectory(foo)
  41. add_subdirectory(tests)

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:

  1. project(foo)
  2.  
  3. set(foo_SRC
  4.     ${CMAKE_CURRENT_BINARY_DIR}/foo/foo_module_wrapper.cpp
  5.     ${CMAKE_CURRENT_BINARY_DIR}/foo/math_wrapper.cpp
  6. )
  7.  
  8. set(foo_INCLUDE_DIRECTORIES
  9.     ${SHIBOKEN_INCLUDE_DIR}
  10.     ${PYTHON_INCLUDE_PATH}
  11.     ${PYSIDE_INCLUDE_DIR}
  12.     ${PYSIDE_INCLUDE_DIR}/QtCore
  13.     ${QT_INCLUDE_DIR}
  14.     ${QT_QTCORE_INCLUDE_DIR}
  15.     ${LIBFOO_DIR}
  16. )
  17.  
  18. set(foo_LINK_LIBRARIES
  19.     ${QT_QTCORE_LIBRARY}
  20.     ${SHIBOKEN_PYTHON_LIBRARIES}
  21.     ${SHIBOKEN_LIBRARY}
  22.     ${PYSIDE_LIBRARY}
  23.     ${LIBFOO_DIR}/libfoo.so
  24. )
  25.  
  26. include_directories(foo ${foo_INCLUDE_DIRECTORIES})
  27. add_library(foo MODULE ${foo_SRC})
  28. set_property(TARGET foo PROPERTY PREFIX "")
  29. target_link_libraries(foo ${foo_LINK_LIBRARIES})
  30.  
  31. add_custom_command(OUTPUT ${foo_SRC}
  32.                    COMMAND ${GENERATOR}
  33.                    --generatorSet=shiboken --enable-parent-ctor-heuristic --enable-pyside-extensions --enable-return-value-heuristic
  34.                    ${CMAKE_SOURCE_DIR}/foo/global.h
  35.                    --include-paths=${QT_INCLUDE_DIR}:${LIBFOO_DIR}
  36.                    --typesystem-paths=${typesystem_path}:${PYSIDE_TYPESYSTEMS}
  37.                    --output-directory=${CMAKE_CURRENT_BINARY_DIR}
  38.                    ${CMAKE_CURRENT_SOURCE_DIR}/typesystem_foo.xml
  39.                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  40.                    COMMENT "Running generator for libfoo..."
  41.                   )

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:

  1. if(WIN32)
  2.     set(TEST_PYTHONPATH     "${foo_BINARY_DIR};${PYSIDE_PYTHONPATH}")
  3.     set(TEST_LIBRARY_PATH   "${LIBFOO_DIR};$ENV{PATH}")
  4.     set(LIBRARY_PATH_VAR    "PATH")
  5.     string(REPLACE "\\" "/" TEST_PYTHONPATH "${TEST_PYTHONPATH}")
  6.     string(REPLACE "\\" "/" TEST_LIBRARY_PATH "${TEST_LIBRARY_PATH}")
  7.  
  8.     string(REPLACE ";" "\\;" TEST_PYTHONPATH "${TEST_PYTHONPATH}")
  9.     string(REPLACE ";" "\\;" TEST_LIBRARY_PATH "${TEST_LIBRARY_PATH}")
  10. else()
  11.     set(TEST_PYTHONPATH     "${foo_BINARY_DIR}:${PYSIDE_PYTHONPATH}")
  12.     set(TEST_LIBRARY_PATH   "${LIBFOO_DIR}:$ENV{LD_LIBRARY_PATH}")
  13.     set(LIBRARY_PATH_VAR    "LD_LIBRARY_PATH")
  14. endif()
  15.  
  16. add_test(math ${SHIBOKEN_PYTHON_INTERPRETER} ${CMAKE_CURRENT_SOURCE_DIR}/math_test.py)
  17. set_tests_properties(math PROPERTIES ENVIRONMENT "PYTHONPATH=${TEST_PYTHONPATH};${LIBRARY_PATH_VAR}=${TEST_LIBRARY_PATH}")

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.

  1. cd foobinding-cmake
  2. mkdir build
  3. cd build
  4. cmake ..
  5. make

Ah, let’s not forget the unit test. It’s a very simple one.

foobinding-cmake/tests/math.py

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. '''Test cases for foo bindings module.'''
  5.  
  6. import unittest
  7. import foo
  8.  
  9. class MathTest(unittest.TestCase):
  10.  
  11.     def testMath(self):
  12.         '''Test case for Math class from foo module.'''
  13.         val = 5
  14.         math = foo.Math()
  15.         self.assertEqual(math.squared(5), 5 * 5)
  16.  
  17. if __name__ == '__main__':
  18.     unittest.main()

To run the test:

  1. ctest

The output will be something like this:

  1. Test project YOURPATH/binding-tutorial/foobinding-cmake/build
  2.     Start 1: math
  3. 1/1 Test #1: math .............................   Passed    0.10 sec
  4.  
  5. 100% tests passed, 0 tests failed out of 1
  6.  
  7. Total Test time (real) =   0.11 sec

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].

Categories:

  • PySide_Binding_Generation_Tutorial
  •