How can I cross-build my application for Cumulus Linux on PPC/ARM architectures?


Userlevel 1
Attached is a bash script that provides the ability to create (via cross-building) a 32-bit PPC or 64-bit ARM architecture build of an application that originated on x86 platforms.

The following is known to work on Debian and Ubuntu. For Wheezy versions of Debian, a backported version of qemu-user-static is required.

The first time this is run, it creates and prepares a crossroot for the target architecture. Subsequent runs are much faster, as it just runs the command(s) in the cross chroot.

NOTE: The following example uses building of Chef as an example, and should be replaced.

Usage:
# Do these once, to create the cross-chroot and prepare it.
$ mkdir /tmp/wheezy_ppc
$ sudo ./cross_run powerpc /tmp/wheezy_ppc "apt-get -y build-dep chef"

# Get source and do the build. This can be done multiple times.
$ sudo ./cross_run powerpc /tmp/wheezy_ppc "cd /tmp; apt-get -y source chef"
$ sudo ./cross_run powerpc /tmp/wheezy_ppc "cd /tmp/chef-*; dpkg-buildpackage -us -uc -b"

On a typical Haswell laptop, the prep takes ~5 minutes. This will take more or less time depending on the exact build dependencies of the package you're building. Actually getting the source and building an application can take a minute or two.

#!/bin/bash
#Copyright 2015 Cumulus Networks, Inc. All rights reserved.
#License: MIT https://opensource.org/licenses/MIT

#XXX In theory, we could make this not need root w/ fakeroot/fakechroot...
#XXX If we get an apporpriate cross-compiler in the chroot, this
# wouldn't even be that slow compared to native.

if [ 'whoami' != "root" ]; then
echo "$0 must be run as root" >&2
exit 10
fi
if [ -z "$1" -o -z "$2" ]; then
echo "Usage: $0 arch cross_root_dir" >&2
exit 11
fi
if [ ! -d "$2" ]; then
echo "$2 is not a directory" >&2
exit 12
fi

ARCH="$1"
DIR="$2"
REL="wheezy"
MIRROR="http://ftp.debian.org/debian/";

shift; shift

#Annoyingly, qemu-user-static uses differetn names than Debian.
QEMU="qemu-$ARCH-static"
if [ "$ARCH" == "powerpc" ]; then
QEMU="qemu-ppc-static"
fi
if echo $ARCH | grep -q '^arm'; then #Debian doesn't support BE ARM
QEMU="qemu-arm-static"
fi

#Check some requirements...
if [ 'whoami' != "root" ]; then
echo "$0 must be run as root" >&2
exit 10
fi
if [ ! -x 'which debootstrap' ]; then
echo "debootstrap is not installed" >&2
exit 20
fi
if ! grep -q "$QEMU" /proc/sys/fs/binfmt_misc/qemu-*; then
echo "binfmt-support is not installed" >&2
exit 21
fi
if [ ! -x 'which "$QEMU"' ]; then
echo "qemu-user-static is not installed" >&2
exit 22
fi
QEMU_MAJ='$QEMU -version | cut -d' ' -f3 | cut -d'.' -f1'
QEMU_MIN='$QEMU -version | cut -d' ' -f3 | cut -d'.' -f2'
if [ $QEMU_MAJ -lt 2 -a $QEMU_MIN -lt 4 ]; then
echo "qemu-user-static must be 1.4.0 or later" >&2
echo "use backports if your build-host is wheezy" >&2
exit 23
fi

run () {
LC_ALL=C LANGUAGE=C LANG=C chroot $DIR /bin/bash -c "$*"
}

#Prepare the cross chroot if it isn't already.
if [ ! -d "$DIR/etc" ]; then
#XXX set a trap to umount /proc and /sys on ctrl-c/if something goes wrong
debootstrap --variant=buildd --foreign --arch $ARCH $REL $DIR $MIRROR
cp 'which $QEMU' $DIR/usr/bin/
run /debootstrap/debootstrap --second-stage
sed 's/^deb /deb-src /1' $DIR/etc/apt/sources.list >> $DIR/etc/apt/sources.list
run apt-get update
fi

run $*

2 replies

Would be interested to know how what toolkits are using to cross-build if they are.
trapier wrote:

Would be interested to know how what toolkits are using to cross-build if they are.

derp!

Would be interested to know what toolkits folks are using to cross-build if they are.

Reply