From e32ac73660c875259344ac6536b7bbe775ff4620 Mon Sep 17 00:00:00 2001 From: Antony Chazapis Date: Fri, 23 Jun 2023 21:13:58 +0300 Subject: [PATCH] Integrate with Dapper, cross-compile in Alpine --- Dockerfile.dapper | 2 +- scripts/build | 4 ++-- scripts/ci | 3 +++ scripts/cross-compile-riscv.sh | 34 ----------------------------- scripts/package | 4 +++- scripts/prepare-cross | 39 ++++++++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 38 deletions(-) delete mode 100755 scripts/cross-compile-riscv.sh create mode 100755 scripts/prepare-cross diff --git a/Dockerfile.dapper b/Dockerfile.dapper index 4fdb55efed68..a100b6fa0306 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -44,7 +44,7 @@ ARG SELINUX=true ENV SELINUX $SELINUX ENV DAPPER_RUN_ARGS --privileged -v k3s-cache:/go/src/github.com/k3s-io/k3s/.cache -v trivy-cache:/root/.cache/trivy -ENV DAPPER_ENV REPO TAG DRONE_TAG IMAGE_NAME SKIP_VALIDATE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER DEBUG +ENV DAPPER_ENV REPO TAG DRONE_TAG IMAGE_NAME ARCH SKIP_VALIDATE SKIP_IMAGE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER DEBUG ENV DAPPER_SOURCE /go/src/github.com/k3s-io/k3s/ ENV DAPPER_OUTPUT ./bin ./dist ./build/out ./build/static ./pkg/static ./pkg/deploy diff --git a/scripts/build b/scripts/build index b4ca5dd20e2d..7328048ae84e 100755 --- a/scripts/build +++ b/scripts/build @@ -101,8 +101,8 @@ fi if [ ${ARCH} = riscv64 ]; then export GOARCH="riscv64" # These are needed as we are cross-compiling - export CC=riscv64-linux-gnu-gcc - export PKG_CONFIG_PATH=/usr/lib/riscv64-linux-gnu/pkgconfig/ + export CC="riscv64-alpine-linux-musl-gcc" + export PKG_CONFIG_PATH=/home/builder/sysroot-riscv64/usr/lib/pkgconfig/ fi rm -f \ diff --git a/scripts/ci b/scripts/ci index 9fb3ec5fb6bd..db3d3f0d5816 100755 --- a/scripts/ci +++ b/scripts/ci @@ -6,6 +6,9 @@ pushd $SCRIPT_DIR ./download ./validate +if [ ${ARCH} = riscv64 ]; then + ./prepare-cross +fi ./build ./package diff --git a/scripts/cross-compile-riscv.sh b/scripts/cross-compile-riscv.sh deleted file mode 100755 index e39fd5e0bcd6..000000000000 --- a/scripts/cross-compile-riscv.sh +++ /dev/null @@ -1,34 +0,0 @@ -#/bin/bash - -# Cross-compile for RISC-V in an Ubuntu 22.04 container - -# To run mount a clean checkout of the code inside the container and run the script: -# -# docker run --rm --mount type=bind,source=${PWD},target=/k3s ubuntu:22.04 /bin/bash -c /k3s/scripts/cross-compile-riscv.sh - -dpkg --add-architecture riscv64 -apt-get update -apt-get install -y wget curl git gcc-riscv64-linux-gnu g++-riscv64-linux-gnu pkg-config libseccomp-dev:riscv64 make zstd - -HOST_ARCH=$(uname -m) -if [ ${HOST_ARCH} = aarch64 ]; then - HOST_ARCH=arm64 -fi - -GOLANG_VERSION=1.20.4 -wget -P /tmp "https://dl.google.com/go/go${GOLANG_VERSION}.linux-${HOST_ARCH}.tar.gz" -(cd /; tar -C /usr/local -xzf "/tmp/go${GOLANG_VERSION}.linux-${HOST_ARCH}.tar.gz") -rm -rf /tmp/go${GOLANG_VERSION}.linux-${HOST_ARCH}.tar.gz -mkdir -p /go/src /go/bin - -export GOPATH=/go -export PATH=$GOPATH/bin:/usr/local/go/bin:$PATH - -wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/${HOST_ARCH} -chmod +x /usr/local/bin/yq - -cd /k3s -export ARCH=riscv64 -bash -x ./scripts/download -bash -x ./scripts/build -bash -x ./scripts/package-cli diff --git a/scripts/package b/scripts/package index 51b8f417bb65..ca2123657e39 100755 --- a/scripts/package +++ b/scripts/package @@ -8,7 +8,9 @@ if [ ! -e ../bin/containerd ]; then fi ./package-cli -./package-image +if [ -z "$SKIP_IMAGE" ]; then + ./package-image +fi if [ -z "$SKIP_AIRGAP" ]; then ./package-airgap fi diff --git a/scripts/prepare-cross b/scripts/prepare-cross new file mode 100755 index 000000000000..9fa4c3c285f7 --- /dev/null +++ b/scripts/prepare-cross @@ -0,0 +1,39 @@ +#/bin/bash + +# This script prepare a cross-compilation environment for $ARCH + +if [ -z "$ARCH" ]; then + echo "Error: ARCH not defined" + exit 1; +fi + +if [ "$ARCH" != "riscv64" ]; then + echo "Error: Cross-compilation only available for riscv64" + exit 1; +fi + +if [ "$ARCH" = "riscv64" ]; then + ALPINE_APK_REPO=https://dl-cdn.alpinelinux.org/alpine/edge/main +fi + +# Build packages for cross-compiling (bootstrap.sh requires a non-root user) +adduser -D builder +addgroup builder abuild + +su builder -c "abuild-keygen -an" +cp ~builder/.abuild/builder-*.rsa.pub /etc/apk/keys/ + +ALPINE_VERSION=$(. /etc/os-release; echo $VERSION_ID | cut -f1,2 -d.) +su builder -c "cd; git clone --single-branch --branch=$ALPINE_VERSION-stable --depth 1 https://github.com/alpinelinux/aports" +sed -i "/Cross building base system/i exit 0\n" /home/builder/aports/scripts/bootstrap.sh +apk update +su builder -c "CBUILDROOT=~/sysroot-$ARCH ~/aports/scripts/bootstrap.sh $ARCH" + +# Install the cross-compiler in the host +apk -X /home/builder/packages/main add gcc-$ARCH + +# Install headers and libraries in the gcc sysroot (upstream packages) +apk -X $ALPINE_APK_REPO -U --allow-untrusted \ + -p /home/builder/sysroot-$ARCH --arch $ARCH add \ + musl musl-dev linux-headers libseccomp-dev libseccomp-static sqlite-dev sqlite-static zlib-static libgcc +sed -i "s/^prefix=.*/prefix=\/home\/builder\/sysroot-$ARCH\/usr/g" /home/builder/sysroot-riscv64/usr/lib/pkgconfig/libseccomp.pc