From 5dca1d9f155a441d93ed8367fb86e4f037fe32bb Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 26 Oct 2023 14:40:39 +0200 Subject: [PATCH] ci: add crossbuild scripts (drafts) --- scripts/ci.crossbuild-alpine.sh | 64 +++++++++++++++++++++++++++++ scripts/ci.crossbuild-debian.sh | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100755 scripts/ci.crossbuild-alpine.sh create mode 100755 scripts/ci.crossbuild-debian.sh diff --git a/scripts/ci.crossbuild-alpine.sh b/scripts/ci.crossbuild-alpine.sh new file mode 100755 index 0000000..9a870e6 --- /dev/null +++ b/scripts/ci.crossbuild-alpine.sh @@ -0,0 +1,64 @@ +#!/bin/sh -eu +# vim: set ts=2 sw=2 et: + +LOCAL_PROJECT_PATH="${1-$PWD}" + +TARGET_ARCH="${2-amd64}" + +DOCKER_IMAGE="" + +BUILD_COMMAND=" \ + shards build --static --release \ + && chown 1000:1000 -R bin \ + && find bin -type f -maxdepth 1 -exec mv {} {}_${TARGET_ARCH} \; \ +" +INSTALL_CRYSTAL=" \ + echo '@edge http://dl-cdn.alpinelinux.org/alpine/edge/community' >>/etc/apk/repositories \ + && apk add --update --no-cache --force-overwrite \ + crystal@edge \ + g++ \ + gc-dev \ + libxml2-dev \ + llvm16-dev \ + llvm16-static \ + make \ + musl-dev \ + openssl-dev \ + openssl-libs-static \ + pcre-dev \ + shards@edge \ + yaml-dev \ + yaml-static \ + zlib-dev \ + zlib-static \ +" + +# setup arch +case "$TARGET_ARCH" in + amd64) DOCKER_IMAGE="alpine" ;; + arm64) DOCKER_IMAGE="multiarch/alpine:aarch64-edge" ;; + armel) DOCKER_IMAGE="multiarch/alpine:armv7-edge" ;; + # armhf) DOCKER_IMAGE="multiarch/alpine:armhf-edge" ;; + # i386) DOCKER_IMAGE="multiarch/alpine:x86-edge" ;; + mips) DOCKER_IMAGE="multiarch/alpine:mips-edge" ;; + mipsel) DOCKER_IMAGE="multiarch/alpine:mipsel-edge" ;; + powerpc) DOCKER_IMAGE="multiarch/alpine:powerpc-edge" ;; + ppc64el) DOCKER_IMAGE="multiarch/alpine:ppc64el-edge" ;; + s390x) DOCKER_IMAGE="multiarch/alpine:s390x-edge" ;; +esac + +# Compile Crystal project statically for target architecture +docker pull multiarch/qemu-user-static:register +docker run \ + --rm \ + --privileged \ + multiarch/qemu-user-static:register \ + --reset +docker run \ + -it \ + -v "$LOCAL_PROJECT_PATH:/app" \ + -w /app \ + --rm \ + "$DOCKER_IMAGE" \ + /bin/sh -c "$INSTALL_CRYSTAL && $BUILD_COMMAND" + diff --git a/scripts/ci.crossbuild-debian.sh b/scripts/ci.crossbuild-debian.sh new file mode 100755 index 0000000..379052c --- /dev/null +++ b/scripts/ci.crossbuild-debian.sh @@ -0,0 +1,71 @@ +#!/bin/sh -eu +# vim: set ts=2 sw=2 et: + +LOCAL_PROJECT_PATH="${1-$PWD}" + +TARGET_ARCH="${2-amd64}" + +DOCKER_IMAGE="" + +BUILD_COMMAND=" \ + shards build --static --release \ + && chown 1000:1000 -R bin \ + && find bin -type f -maxdepth 1 -exec mv {} {}_${TARGET_ARCH} \; \ +" + +# crystal +INSTALL_CRYSTAL=" \ + sed -i -e 's/Types: deb/Types: deb deb-src/' /etc/apt/sources.list.d/debian.sources \ + && echo 'deb http://deb.debian.org/debian unstable main' > /etc/apt/sources.list.d/sid.list \ + && echo 'deb-src http://deb.debian.org/debian unstable main' >> /etc/apt/sources.list.d/sid.list \ + && apt-get update \ + && apt-get install -y \ + g++ \ + libxml2-dev \ + llvm-dev \ + make \ + libssl-dev \ + libpcre3-dev \ + libyaml-dev \ + zlib1g-dev \ + dpkg-dev \ + debuild \ + && apt source crystal \ + && apt build-dep crystal \ + && ls -lF \ + && debuild -b -uc -us \ +" + +# setup arch +case "$TARGET_ARCH" in + amd64) DOCKER_IMAGE="debian" ;; + arm64) DOCKER_IMAGE="arm64v8/debian" ;; + armel) DOCKER_IMAGE="arm32v7/debian" ;; + armhf) DOCKER_IMAGE="armhf/debian" ;; + i386) DOCKER_IMAGE="x86/debian" ;; + mips) DOCKER_IMAGE="mips/debian" ;; + mipsel) DOCKER_IMAGE="mipsel/debian" ;; + powerpc) DOCKER_IMAGE="powerpc/debian" ;; + ppc64el) DOCKER_IMAGE="ppc64el/debian" ;; + s390x) DOCKER_IMAGE="s390x/debian" ;; +esac + +# Compile Crystal project statically for target architecture +docker pull multiarch/qemu-user-static + +docker run \ + --rm \ + --privileged \ + multiarch/qemu-user-static \ + --reset -p yes + +set -x +docker run \ + -it \ + -v "$LOCAL_PROJECT_PATH:/app" \ + -w /app \ + --rm \ + --platform linux/arm64 \ + "$DOCKER_IMAGE" \ + /bin/sh -c "$INSTALL_CRYSTAL && $BUILD_COMMAND" +