Cross-compiling for Android with Docker

May 20, 2015

Next in the series of cross-compiling ITK with CMake and Docker, we target Android’s version of Linux for ARM processors.  This follows articles on using Docker to help us cross-compile for

​ITK on Android puts advanced image analysis capabilities in physician’s pockets, and it brings new image processing possibilities to low-cost, portable ultrasound systems.

As previously noted, there are three steps when cross-compiling with Docker and CMake.

1) Get the cross-compiling toolchain

Obtaining the toolchain with Docker is easy — just download it with docker pull:

docker pull thewtex/cross-compiler-android-arm

2) Start up a container, mounting the source and build tree

If there is a source tree and build tree, e.g.

cd ~/src
git clone http://itk.org/ITK.git
mkdir -p ~/bin/ITK-build

Then mount these directories as Docker volumes when the container is started:

docker run --rm -it \
  -v ~/src/ITK:/usr/src/ITK:ro \
  -v ~/bin/ITK-build:/usr/src/ITK-build:rw \
  thewtex/cross-compiler-android-arm

3) Point CMake to the CMAKE_TOOLCHAIN_FILE

Inside the container, the environmental variable CMAKE_TOOLCHAIN_FILE has been configured to point to the toolchain file CMake uses to inform itself about the build environment.  Pass this file to CMake during configuration.

cd /usr/src/ITK-build
cmake -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
  -G Ninja \
  -DBUILD_EXAMPLES=OFF \
  -DITK_DYNAMIC_LOADING=OFF \
  -DBUILD_SHARED_LIBS=OFF \
  -DCMAKE_EXE_LINKER_FLAGS=-static \
  ../ITK

The -G Ninja  flag tells CMake to use our favorite generator, the Ninja generator, which is fast and does parallel builds automatically.

The -DBUILD_EXAMPLES=OFF  flag disables building the examples.

The options for ITK_DYNAMIC_LOADING, BUILD_SHARED_LIBS, and CMAKE_EXE_LINKER_FLAGS generate static executables. Unlike when cross-compiling for the Raspberry Pi, which is a vanilla Linux target, static executables are used with Android to make it easier to build and test on the host Linux environment. Android ships with its own fancy-pants dynamic loader, /system/bin/linker, which is not present in the toolchain or compatible with a non-Android kernel.

Since the qemu-user emulator has limited multi-threading abilities, run the tests in single-threaded mode:

export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1

And start the Experimental dashboard build!

ctest -D Experimental -j$(nproc)

Enjoy ITK!

Leave a Reply