Cross compiling CMake-based projects using Ubuntu/Debian's multi arch

As you probably already know Ubuntu (and then Debian) added Multi-Arch support quite some time ago. This means that you can install library packages from multiple architectures on the same machine.

Thanks to the work of many people, in which I would like to specially mention Helmut Grohne, we are now able to cross compile Debian packages using standard sbuild chroots. He even was kind enough to provide me with some numbers:

We have 28790 source packages in Debian unstable.
Of those, 13358 (46.3%) build architecture-dependent binary packages.
Of those, 7301 (54.6%) have satisfiable cross Build-Depends.
Of those, 3696 (50.6% of buildable, 27.6% of sources) were attempted.
Of those, 2695 (72.9% of built, 36.9% of buildable, 20.1% of sources) were successful.
633 bugs affecting 772 packages (7.23% of 10663 unsuccessful) are reported.

Now I asked myself if I could use this to cross compile the code I'm working on without the need of doing a full Debian package build.

My projects uses CMake, so we can cross compile by providing a suitable CMAKE_TOOLCHAIN_FILE.

And so the first question is:

How do we create the necessary file using what Multi-Arch brings to our table?

I asked Helmut and he did not only provide me with lots of tips, he also provided me with the following script, which I modified a little:

Now we can run the script providing it with the desired host arch and voilá, we have our toolchain file.

[#!/bin/sh]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
]{style="font-family: \"courier new\" , \"courier\" , monospace;"}[#set -x]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
]{style="font-family: \"courier new\" , \"courier\" , monospace;"}[ARCH=\$1]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
]{style="font-family: \"courier new\" , \"courier\" , monospace;"}[DEB_HOST_GNU_TYPE=\$(dpkg-architecture -f "-a\$1" -qDEB_HOST_GNU_TYPE)]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[DEB_HOST_GNU_CPU=\$(dpkg-architecture -f "-a\$1" -qDEB_HOST_GNU_CPU)]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[case "\$(dpkg-architecture -f "-a\$1" -qDEB_HOST_ARCH_OS)" in]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[        linux) system_name=Linux; ;;]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[        kfreebsd) system_name=kFreeBSD; ;;]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[        hurd) system_name=GNU; ;;]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[        *) exit 1; ;;]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[esac]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
]{style="font-family: \"courier new\" , \"courier\" , monospace;"}[cat \<<eof>> cmake_toolchain_\$1.cmake</eof>]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[# Use it while calling CMake:]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[#   mkdir build; cd build]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[#   cmake -DCMAKE_TOOLCHAIN_FILE="../cmake_toolchain_<arch>.cmake" ../</arch>]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[set(CMAKE_SYSTEM_NAME "\$system_name")]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[set(CMAKE_SYSTEM_PROCESSOR "\$DEB_HOST_GNU_CPU")]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[set(CMAKE_C_COMPILER "\$DEB_HOST_GNU_TYPE-gcc")]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[set(CMAKE_CXX_COMPILER "\$DEB_HOST_GNU_TYPE-g++")]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[set(PKG_CONFIG_EXECUTABLE "\$DEB_HOST_GNU_TYPE-pkg-config")]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[set(PKGCONFIG_EXECUTABLE "\$DEB_HOST_GNU_TYPE-pkg-config")]{style="font-family: \"courier new\" , \"courier\" , monospace;"}
[EOF]{style="font-family: \"courier new\" , \"courier\" , monospace;"}

Can we improve this?

Helmut mentioned that meson provides debcrossgen, a script that automates this step. Meson is written in python, so it only needs to know the host architecture to create the necessary definitions.

CMake is not interpreted, but maybe it has a way to know the host arch in advance. If this is true maybe a helper could be added to help in the process. Ideas (or even better, patches/code!) welcomed.


There are no comments yet.

Add a Comment

You can use the Markdown syntax to format your comment.

Comment Atom Feed