< Development‎ | CMake
Revision as of 21:13, 26 September 2010 by Wrohdewald (Talk | contribs) (cmake and Python projects - initial version)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

How to integrate a Python project into the KDE4 cmake system

This is work in progress - right now there seem to be very different views about how runtime dependencies for projects should be handled. This is much more of a problem for Python projects because almost all of their dependencies are runtime only.

Here are some links to discussions:

Build time dependencies

There is not much that needs to be compiled for a Python project:

  • Mainly the byte-compiled *.pyc files. They should be generated for all project modules. However the main .py file (or files) like which are executed first should not be bytecompiled. The Python interpreter would not use that anyway, it always recompiles the main .py file when loading it. For shorter startups, make it as small as possible. See the example below. Since cmake bytecompiles all python source files at build time, this main file (like should not be added to the list of source files. Instead simply install those main .py files directly in the right place.
  • And maybe some *.ui files to be processed by pyuic4. If you only have few small *.ui files you may prefer to simply distribute the *.ui files and have them processed at runtime (I did not measure how much time that actually takes). This of course simplifies cmake configuration.

from PyQt4 import uic uic.loadUi('mywidget.ui')

So you need a Python interpreter installed for the generation of byte-compiled *.pyc files. Make sure it is a version that can also execute your program. If the version installed at the client side is too different, your precompiled bytecode is incompatible and cannot be used, so the client system will recompile your sources. And if it cannot write them because of missing access rights, it will do that at every program start (there are ways how to tell python where to write its local *.pyc files but that is out of scope for here).

And optionally you need pyuic4 installed. This macro calls pyuic4 and installs the resulting * files:PYKDE4_ADD_UI_FILES(src/*.ui)However I believe I had some problems with that macro, that may have been one reason why I now do that at runtime for Kajongg. This would need to be checked.

Runtime dependencies

In C/C++ most runtime dependencies are also build time dependencies - the KDE4 cmake macros do not even let you differentiate them (yet). However Python source code can be bytecompiled without the imported modules being installed - so the set of build time dependencies is very small and about the same for all Python projects.

Since cmake is mostly used by distribution packagers, you should respect their preferences. Developers do not need cmake for Python development at all.

Distribution packagers do NOT want cmake to error out on missing runtime dependencies, they do not even want warnings about them because they just might not want to install them. Packagers want to have their build system as simple (and as fast) as possible with a minimal set of dependencies. Of course they still need to know the runtime dependencies. For now there is no nice way of specifying this with existing cmake macros. Currently the best solution for you is to write runtime dependency information into a file README.packagers in the home of your project.


In theory, the (like in the example below) could directly be installed in whatever */bin directory but it is nicer to only put a symbolic link there like /usr/bin/kajongg -> /usr/share/kde4/apps/kajongg/ Of course cmake comes with a macro for this: PYKDE4_ADD_EXECUTABLE( kajongg)

Example projects

  • Kajongg, a board game. This project is not yet conforming to the above text - I will convert it as soon as nobody objects to this.

Content is available under Creative Commons License SA 4.0 unless otherwise noted.