Cross-compile ITK’s Python Wrapping for the Raspberry Pi 2

This post describes how to cross-compile the Insight Toolkit (ITK)'s Python wrapping for the Raspberry Pi 2. The default operating system on the Raspberry Pi 2 is Raspbian, a Debian-based Linux distribution. While Debian provides packages for ITK., these packages are not currently available on Raspbian. We will create a Debian package optimized for the Raspberry Pi 2 that can be installed and uninstalled on our Pi. The process is similar to the previous post on cross-compiling for the Raspberry Pi, with the following additions:

  1. Optimization flags specific to the Raspberry Pi 2.
  2. Build wrapping against the Raspbian Python installation.
  3. Create a Debian package.

Let's go!

Image credit: the PyScience blog

Get the cross-compiler

If Docker is installed, download the cross-compiler image. This cross-compiler with work with both the Raspberry Pi and the Raspberry Pi 2, even though Raspberry Pi is ARMv7.

 

Get the code

Download the ITK source tree if it not already available, and create a build directory:

We also need the Python headers and library from the Pi so we can compile and link against them. We will log into the Pi, create a tarball of the needed files and directories, and transfer that tarball to the build host.

 

Build and package

Start up the docker container, and mount the source directory, build directory, and Python filesystem directory as volumes in the container:

Next, clear the build tree and configure the build:

The CMAKE_TOOLCHAIN_FILE option informs CMake about the build toolchain. BUILD_EXAMPLES, BUILD_TESTING, and ITK_WRAP_PYTHON are options for ITK. PYTHON_LIBRARY and PYTHON_INCLUDE_DIR inform CMake of the Python we want to build against. PY_SITE_PACKAGES_PATH tells CMake where to install the Python package. The value used, lib/python2.7/dist-packages, is obtained by running the following command on the Pi:

Optimization flags to use features available on the Raspberry Pi 2 processor are passed with the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS options. The cross-compiling target triple, used by the wrapping system, is passed via the CMAKE_CXX_COMPILER_TARGET through the value of the CROSS_TRIPLE enviromental variable. CMAKE_INSTALL_PREFIX, CPACK_DEBIAN_PACKAGE_NAME, and CPACK_DEBIAN_PACKAGE_ARCHITECTURE are options for creating the Debian package. We also tell CMake to use our favorite generator, Ninja.

Build the library:

Build the package:

This generates a .deb package. Copy it to the Pi:

And install it:

Start up Python, and import the itk package:

Enjoy ITK!

Questions or comments are always welcome!