From 21958a27f6962ca8acc32876b352b62a9990a98d Mon Sep 17 00:00:00 2001 From: Andrew Randrianasulu Date: Mon, 21 Oct 2024 01:32:39 +0300 Subject: [PATCH] Add chroot build on x86/x86-64 --- scripts/chroot/build.sh | 75 +++++++++++++++++++++++++++ scripts/chroot/chroot_build.sh | 94 ++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100755 scripts/chroot/build.sh create mode 100755 scripts/chroot/chroot_build.sh diff --git a/scripts/chroot/build.sh b/scripts/chroot/build.sh new file mode 100755 index 0000000..a7875fa --- /dev/null +++ b/scripts/chroot/build.sh @@ -0,0 +1,75 @@ +#! /bin/sh + +set -euxo pipefail + +source arch.src + +if [[ "${ARCH:-}" == "" ]]; then + echo "Usage: env ARCH=... bash $0" + exit 1 +fi + +build_dir="$(mktemp -d -t appimagetool-build-XXXXXX)" + +cleanup () { + if [ -d "$build_dir" ]; then + rm -rf "$build_dir" + fi +} +trap cleanup EXIT + + +apk add bash git gcc g++ cmake make file desktop-file-utils wget \ + gpgme-dev libgcrypt-dev libgcrypt-static argp-standalone zstd-dev zstd-static util-linux-static \ + glib-static libassuan-static zlib-static libgpg-error-static squashfs-tools \ + curl-dev curl-static nghttp2-static libidn2-static openssl-libs-static brotli-static c-ares-static libunistring-static + + +# store repo root as variable +#repo_root="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")"/..)" +repo_root=/src +old_cwd="$(readlink -f "$PWD")" + +#pushd "$build_dir" + +cmake "$repo_root" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_STATIC=ON + +if [[ "${GITHUB_ACTIONS:-}" != "" ]]; then + jobs="$(nproc)" +else + jobs="$(nproc --ignore=1)" +fi + +make -j"$jobs" + +make install DESTDIR=AppDir + +find AppDir + +cp "$(which mksquashfs)" AppDir/usr/bin + +cp "$repo_root"/resources/AppRun.sh AppDir/AppRun +chmod +x AppDir/AppRun + +if [[ "$ARCH" == "x86" ]]; then +RELEASE_ARCH="i686" +fi + +if [[ "$ARCH" == "x86_64" ]]; then +RELEASE_ARCH=${ARCH} +fi + +wget https://github.com/AppImage/type2-runtime/releases/download/continuous/runtime-"$RELEASE_ARCH" + +#pushd AppDir +ln -s usr/share/applications/appimagetool.desktop AppDir +ln -s usr/share/icons/hicolor/128x128/apps/appimagetool.png AppDir +ln -s usr/share/icons/hicolor/128x128/apps/appimagetool.png AppDir/.DirIcon +#popd + +find AppDir +cat AppDir/appimagetool.desktop + +AppDir/AppRun --runtime-file runtime-"$RELEASE_ARCH" AppDir + +mv appimagetool-*.AppImage "$old_cwd" diff --git a/scripts/chroot/chroot_build.sh b/scripts/chroot/chroot_build.sh new file mode 100755 index 0000000..c9bdafb --- /dev/null +++ b/scripts/chroot/chroot_build.sh @@ -0,0 +1,94 @@ +#! /bin/sh + +set -ex + +if [ -z "${ALPINE_ARCH}" ]; then + echo "Usage: env ALPINE_ARCH= $0" + echo "Example values: x86_64 x86 armhf aarch64" + exit 2 +fi + +# build in a temporary directory +# this makes sure that subsequent runs do not influence each other +# also makes cleaning up easier: just dump the entire directory +tempdir="$(mktemp -d)" + +# need to memorize the repository root directory's path so that we can copy files from it +repo_root_dir=$(dirname "$(readlink -f "$0")")/../../ + +# cleanup takes care of unmounting and removing all downloaded files +cleanup() { + for i in dev proc sys; do + sudo umount "$tempdir"/miniroot/"$i" + done + sudo rm -rf "$tempdir" +} +trap cleanup EXIT + +cd "$tempdir" + +############################################# +# Download and extract minimal Alpine system +############################################# + +wget "http://dl-cdn.alpinelinux.org/alpine/v3.19/releases/${ALPINE_ARCH}/alpine-minirootfs-3.19.4-${ALPINE_ARCH}.tar.gz" +mkdir -p ./miniroot +cd ./miniroot +sudo tar xf ../alpine-minirootfs-*-"${ALPINE_ARCH}".tar.gz +cd - + +############################################# +# Prepare chroot +############################################# +sudo mkdir miniroot/src +sudo cp -r "$repo_root_dir" miniroot/src +#sudo cp -r "$repo_root_dir"/patches miniroot/patches + +sudo mount -o bind /dev miniroot/dev +sudo mount -t proc none miniroot/proc +sudo mount -t sysfs none miniroot/sys +sudo cp -p /etc/resolv.conf miniroot/etc/ + +############################################# +# Run build.sh in chroot +############################################# + +# copy build scripts so that they are available within the chroot environment +# build.sh combines existing scripts shared by all available build environments +sudo cp -R "$repo_root_dir"/scripts miniroot/scripts + +if [ "$ALPINE_ARCH" = "x86" ] || [ "$ALPINE_ARCH" = "x86_64" ]; then + echo "Architecture is x86 or x86_64, hence not using qemu-arm-static" + echo "export ARCH=${ALPINE_ARCH}" > miniroot/arch.src + sudo chroot miniroot /bin/sh -ex /scripts/chroot/build.sh +elif [ "$ALPINE_ARCH" = "aarch64" ] ; then + echo "Architecture is aarch64, hence using qemu-aarch64-static" + sudo cp "$(which qemu-aarch64-static)" miniroot/usr/bin + sudo chroot miniroot qemu-aarch64-static /bin/sh -ex /scripts/chroot/build.sh +elif [ "$ALPINE_ARCH" = "armhf" ] ; then + echo "Architecture is armhf, hence using qemu-arm-static" + sudo cp "$(which qemu-arm-static)" miniroot/usr/bin + sudo cp "$repo_root_dir"/scripts/chroot/build.sh miniroot/build.sh && sudo chroot miniroot qemu-arm-static /bin/sh -ex /scripts/chroot/build.sh +else + echo "Edit chroot_build.sh to support this architecture as well, it should be easy" + exit 1 +fi + +############################################# +# Copy build artifacts out +############################################# + +# Use the same architecture names as https://github.com/AppImage/AppImageKit/releases/ +case "$ALPINE_ARCH" in + x86) + appimage_arch=i686 + ;; + *) + appimage_arch="$ALPINE_ARCH" + ;; +esac + +cd "$repo_root_dir" +mkdir -p ./out/ +sudo find "$tempdir"/miniroot/ -type f -executable -name "appimagetool-${appimage_arch}.AppImage" -exec cp {} "out/appimagetool-${appimage_arch}.AppImage" \; +# sudo find "$tempdir"/miniroot/ -type f -executable -name "runtime-${appimage_arch}.debug" -exec cp {} "out/runtime-${appimage_arch}.debug" \; -- 2.46.2