From 7a96634b5a60b6b42adb9f0097c0db2a2cc09380 Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Thu, 22 Feb 2024 16:36:20 +0100 Subject: [PATCH] Add option to use with zigbuild Signed-off-by: Jacob Kiers --- Dockerfile-zig | 25 ++++++++++++++++++ README.md | 13 ++++++---- build.sh | 48 +++++++++++++++++++++++++++++----- zig/install-dependencies.sh | 51 +++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 Dockerfile-zig mode change 100755 => 100644 build.sh create mode 100644 zig/install-dependencies.sh diff --git a/Dockerfile-zig b/Dockerfile-zig new file mode 100644 index 0000000..abc94eb --- /dev/null +++ b/Dockerfile-zig @@ -0,0 +1,25 @@ +FROM docker.io/library/rust:${RUST_VERSION}-slim +LABEL maintainer 'Jacob Kiers ' +ENV ZIG_VERSION=${ZIG_VERSION} + +# Install zig, binstall and zigbuild +COPY zig/install-dependencies.sh /tmp/install-dependencies.sh +RUN chmod +x /tmp/install-dependencies.sh && /tmp/install-dependencies.sh && rm /tmp/install-dependencies.sh + +# Install macOS SDKs +RUN curl -L "https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX10.9.sdk.tar.xz" | tar -J -x -C /opt +RUN curl -L "https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz" | tar -J -x -C /opt +ENV SDKROOT=/opt/MacOSX11.3.sdk + +# Install Rust targets +RUN rustup target add \ + x86_64-unknown-linux-gnu \ + x86_64-unknown-linux-musl \ + aarch64-unknown-linux-gnu \ + aarch64-unknown-linux-musl \ + arm-unknown-linux-gnueabihf \ + arm-unknown-linux-musleabihf \ + x86_64-apple-darwin \ + aarch64-apple-darwin \ + x86_64-pc-windows-gnu + diff --git a/README.md b/README.md index eb5896e..95739c7 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,17 @@ By default, it uses the latest [Rust][2] and [`cross`][3] versions. ## Usage ``` -USAGE: ./build.sh [rust version] [cross version] +USAGE: ./build.sh [rust version] [cross version] [zig version] Arguments: - Slim build (without targets) or Full build (with targets) - [rust version] Version of rust (https://github.com/rust-lang/rust) - [cross version] Version of cross (https://github.com/cross-rs/cross) + Slim build (without targets) or Full build (with targets) -Both versions default to the latest version. + [rust version] Version of rust (https://github.com/rust-lang/rust) + [cross version] Version of cross (https://github.com/cross-rs/cross) + [zig version] Version of zig (https://ziglang.org/download) + Use 'master' for master version of zig + +All versions default to the latest release. ``` [1]: https://code.kiers.eu/newsletter-to-web/newsletter-to-web diff --git a/build.sh b/build.sh old mode 100755 new mode 100644 index a75b751..34d8b20 --- a/build.sh +++ b/build.sh @@ -2,14 +2,17 @@ print_usage() { - echo "USAGE: $0 [rust version] [cross version]" + echo "USAGE: $0 [rust version] [cross version] [zig version]" echo echo "Arguments:" echo -e "\t\tSlim build (without targets) or Full build (with targets)" + echo echo -e "\t[rust version]\tVersion of rust (https://github.com/rust-lang/rust)" echo -e "\t[cross version]\tVersion of cross (https://github.com/cross-rs/cross)" + echo -e "\t[zig version]\tVersion of zig (https://ziglang.org/download)" + echo -e "\t\t\tUse 'master' for master version of zig" echo - echo "Both versions default to the latest version." + echo "All versions default to the latest release." } get_latest_release() { @@ -18,6 +21,18 @@ get_latest_release() { sed -E 's/.*"v?([^"]+)".*/\1/' # Pluck JSON value } +get_version() { + case $2 in + latest | "") + echo $(get_latest_release $1) + ;; + *) + echo $2 + ;; + + esac +} + if command -v podman &> /dev/null then @@ -42,7 +57,7 @@ if [ $# -lt 1 ]; then fi case ${1} in - full | slim) + full | slim | zig) ;; *) @@ -56,7 +71,7 @@ esac set -e -o pipefail if [ -z "$2" ]; then - RUST_VERSION=$(get_latest_release 'rust-lang/rust') + RUST_VERSION=$(get_version 'rust-lang/rust' $2) else RUST_VERSION=$2 fi @@ -67,12 +82,25 @@ else CROSS_VERSION=$3 fi -echo "Creating a ${1} build with Rust ${RUST_VERSION} and cross ${CROSS_VERSION}" +ZIG_VERSION=$(get_latest_release 'ziglang/zig') export RUST_VERSION export CROSS_VERSION +export ZIG_VERSION -IMAGE="img.kie.rs/jjkiers/rust-dind-cross:rust${RUST_VERSION}-cross${CROSS_VERSION}-${1}" +echo "Creating a ${1} build with the following versions:" +echo -e "\tRust:\t${RUST_VERSION}" +echo -e "\tcross:\t${CROSS_VERSION}" +echo -e "\tzig:\t${ZIG_VERSION}" + +case $1 in + full | slim) + IMAGE="img.kie.rs/jjkiers/rust-crossbuild:rust${RUST_VERSION}-cross${CROSS_VERSION}-${1}" + ;; + zig) + IMAGE="img.kie.rs/jjkiers/rust-crossbuild:rust${RUST_VERSION}-zig${ZIG_VERSION}-${1}" + ;; +esac envsubst < Dockerfile-${1} | ${BUILDER} build -f - \ -t ${IMAGE} \ @@ -80,6 +108,12 @@ envsubst < Dockerfile-${1} | ${BUILDER} build -f - \ echo "Built image ${IMAGE}" +exit 0 + ${BUILDER} push ${IMAGE} -echo "Pushed image ${IMAGE}" +if [ $? -ne 0 ]; then + echo "Pushing failed: ${BUILDER} push ${IMAGE}" +else + echo "Pushed image ${IMAGE}" +fi diff --git a/zig/install-dependencies.sh b/zig/install-dependencies.sh new file mode 100644 index 0000000..f7a4afb --- /dev/null +++ b/zig/install-dependencies.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -xo pipefail + +apt-get update && apt-get install -y jq curl xz-utils + +TMP_DIR=$(mktemp -d) + +pushd ${TMP_DIR} + +# Install minisign +curl -Ls https://github.com/jedisct1/minisign/releases/download/0.11/minisign-0.11-linux.tar.gz -o - | tar --strip-components 2 -C /usr/local/bin/ -vxzf - minisign-linux/x86_64/minisign + +# Installing zig, checking its validity +mkdir -p /usr/local/bin +curl -Ls https://ziglang.org/download/index.json -o zig-versions.json + +ZIG_SHASUM=".\"${ZIG_VERSION}\".\"x86_64-linux\".shasum" +ZIG_TARBALL_URL=".\"${ZIG_VERSION}\".\"x86_64-linux\".tarball" + +echo "$(jq -r ${ZIG_SHASUM} zig-versions.json) zig.tar.xz" > zig.tar.xz.shasum +curl -Ls "$(jq -r ${ZIG_TARBALL_URL} zig-versions.json)" -o zig.tar.xz +curl -Ls "$(jq -r ${ZIG_TARBALL_URL} zig-versions.json).minisig" -o zig.tar.xz.minisig +sha256sum -c zig.tar.xz.shasum + +if [ $? -ne 0 ]; then + echo "Error in SHA256 hash!" + exit 1 +else + echo "SHA256 hash OK" +fi + +minisign -P 'RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U' -Vm zig.tar.xz + +if [ $? -ne 0 ]; then + echo "Error in signature!" + exit 1 +else + echo "Signature OK" +fi + +tar -C /usr/local/bin --strip-components 1 -xf zig.tar.xz + +# Installing binstall and zigbuild +curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash +cargo binstall -y cargo-zigbuild + +popd +rm -rf ${TMP_DIR} + +apt-get clean +rm -rf /var/lib/apt/lists/*