#!/bin/bash
# Debian package dependencies for the host
HOST_DEPENDENCIES="debootstrap qemu-user-static binfmt-support sbuild autoconf m4 git"
# Debian package dependencies for the chrooted environment
GUEST_DEPENDENCIES="gawk,m4,git,autotools-dev"
# Command used to build/run the tests
BUILD_COMMAND="./travis-ci.sh"
CHROOT_ARCH=$1
QEMU_ARCH=$2
MIRROR=http://ftp.debian.org/debian
DEB_VERSION=$3
CHROOT_DIR="/var/tmp/${CHROOT_ARCH}-chroot"
CUR_DIR=$(pwd)
function setup_chroot {
# Host dependencies
echo "Installing host dependencies"
sudo apt-get install -y ${HOST_DEPENDENCIES}
# Create chrooted environment
sudo mkdir ${CHROOT_DIR}
echo "Running debootstrap"
sudo debootstrap --verbose --keep-debootstrap-dir --foreign --variant=buildd --no-check-gpg --include=${GUEST_DEPENDENCIES}\
--arch=${CHROOT_ARCH} ${DEB_VERSION} ${CHROOT_DIR} ${MIRROR}
echo "Finish debootstrap"
sudo cp /usr/bin/qemu-${QEMU_ARCH}-static ${CHROOT_DIR}/usr/bin/
echo "Running chroot second-stage"
sudo chroot ${CHROOT_DIR} ./debootstrap/debootstrap --second-stage
#sudo chroot ${CHROOT_DIR} /bin/pwd --second-stage
sudo sbuild-createchroot --arch=${CHROOT_ARCH} --foreign --setup-only \
${DEB_VERSION} ${CHROOT_DIR} ${MIRROR}
# Create build dir and copy travis build files to our chroot environment
TARGET_BUILD_DIR=${CHROOT_DIR}/${CUR_DIR}/
sudo mkdir -p $TARGET_BUILD_DIR
sudo rsync -av ${CUR_DIR}/ $TARGET_BUILD_DIR
echo "Bootstrapping project"
cd $TARGET_BUILD_DIR && ./bootstrap
# Indicate chroot environment has been set up
sudo touch ${CHROOT_DIR}/.chroot_is_done
# Call ourselves again which will cause tests to run
sudo chroot ${CHROOT_DIR} /bin/bash -c "cd ${CUR_DIR} && ./emulate.sh ${CHROOT_ARCH} ${QEMU_ARCH} chroot_done"
}
if [ "$#" -ne 3 ]; then
echo "Usage: $0 [Chroot target architecture] [QEMU emulation architecture] [Debian version]"
exit 1
fi
if [ "$3" = "chroot_done" ]; then
# We are inside chroot
echo "Running inside chrooted environment"
echo "Environment: $(uname -a)"
${BUILD_COMMAND}
else
if [ ! "$1" = "amd64" ]; then
echo "Setting up chrooted ${CHROOT_ARCH} environment"
setup_chroot
fi
fi