Building applications for other architectures with Flatpak

When I added Blender to Flathub, the build failed on ARM and Aarch64.

Not having access to a machine in any of these architectures, I was wondering how I could try and reproduce the build failure, in order to fix it.

Fortunately, Flatpak comes with fantastic tools.

I quickly found flatpak-builder had a --arch option, so I figured I'd just try it out and see what happened:

$ flatpak-builder --arch=arm app org.blender.Blender.json
error: org.freedesktop.Platform 1.6 not installed
Failed to init: Unable to find runtime org.freedesktop.Platform version 1.6

Fair enough, that's easily solved:

$ flatpak install --user gnome org.freedesktop.Platform/arm/1.6 \
                               org.freedesktop.Sdk/arm/1.6

Now let's try building again:

$ flatpak-builder --arch=arm app org.blender.Blender.json
Downloading sources
...
Starting build of org.musicbrainz.Picard
Cache miss, checking out last cache hit
========================================================================
Building module glu
========================================================================
execvp ./configure: Exec format error
Error: module glu: Child process exited with code 1

So flatpak-builder is happy now, it downloads the sources, and tries building the first module. That fails though, as could be expected, because running the ./configure script means running an ARM /bin/bash on my x86_64 laptop.

Fortunately, QEMU allows running binaries from foreign architectures transparently.

This might work differently on another OS, but on Fedora all I had to do was install a package and start a service:

$ sudo dnf install qemu-user-static
$ sudo systemctl start systemd-binfmt.service

Note that after a reboot the systemd-binfmt service will be automatically started as it detects the /usr/lib/binfmt.d/ folder is not empty. (it contains the files from the qemu-user-static package)

And behold:

$ flatpak-builder --arch=arm app org.blender.Blender.json
Downloading sources
...
Starting build of org.blender.BlenderNext
Cache miss, checking out last cache hit
========================================================================
Building module glu
========================================================================
checking build system type... armv7l-unknown-linux-gnueabihf
checking host system type... armv7l-unknown-linux-gnueabihf
...

Obviously, this runs extremely slowly. But it is so much easier than cross-compiling, and it is invaluable when trying to debug build failures.

The only way this could be even simpler would be for flatpak-builder to suggest installing the right package, and for systemd-binfmt.service to autostart when needed.

Now go and build applications on all sorts of architectures with Flatpak! 😁