Skip to content

Instantly share code, notes, and snippets.

@xentec
Last active October 21, 2023 12:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save xentec/dbbf3cfdc3342a14000db4bedce193bd to your computer and use it in GitHub Desktop.
Save xentec/dbbf3cfdc3342a14000db4bedce193bd to your computer and use it in GitHub Desktop.
Alpine Linux cross-compiler setup script
#!/bin/sh
CTARGET="$1"
if [ -z "$CTARGET" ]; then
program=$(basename $0)
echo "usage: $program TARGET_ARCH"
return 1
fi
# get abuild configurables
[ -e /usr/share/abuild/functions.sh ] || (echo "abuild not found" ; exit 1)
. /usr/share/abuild/functions.sh
[ -z "$CBUILD_ARCH" ] && die "abuild is too old"
# deduce aports directory
[ -z "$APORTSDIR" ] && [[ APORTSDIR=$(realpath $(dirname $0)/../) && \
[ -e "$APORTSDIR/main/build-base" ] || die "Unable to deduce aports base checkout" ]]
APK="apk --root $CBUILDROOT"
msg() {
local name="${BLUE}bootstrap-${CTARGET_ARCH}${NORMAL}"
printf "$GREEN>>>${NORMAL} ${name}: %s\n" "$1" >&2
}
err() {
printf "$RED>>> ERROR${NORMAL}: %s\n" "$*" >&2
}
die() {
err "$*"
exit 2
}
if [ ! -d "$CBUILDROOT" ]; then
msg "Creating sysroot in $CBUILDROOT"
if [ -z "$PACKAGER_PRIVKEY" ] || [ ! -f "$PACKAGER_PRIVKEY" ]; then
abuild-keygen -i -a || exit 2
. ~/.abuild/abuild.conf
fi
APK_ROOT_CONF="$CBUILDROOT/etc/apk"
mkdir -p "$APK_ROOT_CONF/keys"
cp /etc/apk/repositories "$APK_ROOT_CONF/"
case $CTARGET_ARCH in # HACK
armv7) keypath=/usr/share/apk/keys/armhf ;;
*) keypath=/usr/share/apk/keys/$CTARGET_ARCH ;;
esac
cp $keypath/* "$APK_ROOT_CONF/keys/"
cp $PACKAGER_PRIVKEY.pub "$APK_ROOT_CONF/keys/"
$APK add --update --initdb --arch $CTARGET_ARCH libc-dev libgcc ||
die "failed to initialize target APK repository"
fi
msg "Building cross-compiler"
export CBUILD CHOST CTARGET BOOTSTRAP=nobase
# Build and install cross binutils (--with-sysroot)
APKBUILD=$(aports_buildscript binutils) abuild -r ||
die "failed to build binutils-$CTARGET_ARCH"
# Full cross GCC
EXTRADEPENDS_TARGET="musl musl-dev" \
LANG_ADA=false LANG_D=false LANG_OBJC=false LANG_GO=false LANG_FORTRAN=false \
APKBUILD=$(any_buildscript gcc) abuild -r ||
die "failed to build gcc-$CTARGET_ARCH"
# Cross build-base
APKBUILD=$(aports_buildscript build-base) abuild -r ||
die "failed to build build-base"
msg "Done"
echo "To install the cross-compiler use 'apk --repository $REPODEST/main --keys-dir ~/.abuild add build-base-$CTARGET_ARCH'"
echo "To build Alpine packages for '$CTARGET_ARCH' use 'CHOST=$CTARGET_ARCH abuild -r'"
echo "To explicitly install cross-build libraries use '$APK add --no-scripts <pkg>'"
@sjlongland
Copy link

https://gist.github.com/xentec/dbbf3cfdc3342a14000db4bedce193bd#file-bootstrap-sh-L74
I think on that line you have a typo… it aborts complaining -r: command not found.

Otherwise, I have your script bootstrapping armv5… needs a bit of babysitting… e.g. it bailed out after building binutils because binutils-armel wasn't installed, installed it manually and it proceeded (same for gcc-stage2), but otherwise it seems to be getting there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment