toolchain-builder/build.sh
Jacob Kiers 7a96634b5a Add option to use with zigbuild
Signed-off-by: Jacob Kiers <code@kiers.eu>
2024-02-22 16:36:20 +01:00

120 lines
2.6 KiB
Bash

#!/usr/bin/env bash
print_usage()
{
echo "USAGE: $0 <slim|full|zig> [rust version] [cross version] [zig version]"
echo
echo "Arguments:"
echo -e "\t<slim|full>\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 "All versions default to the latest release."
}
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
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
BUILDER=podman
elif command -v docker &> /dev/null
then
BUILDER=docker
else
echo "Prerequisite failed: either podman or docker must be installed"
exit 1
fi
if ! command -v envsubst &> /dev/null
then
echo "Prerequisite failed: envsubst must be installed"
exit 1
fi
if [ $# -lt 1 ]; then
print_usage
exit 1
fi
case ${1} in
full | slim | zig)
;;
*)
echo "ERROR: The first argument mus either be 'full' or 'slim'."
echo
print_usage
exit 1
;;
esac
set -e -o pipefail
if [ -z "$2" ]; then
RUST_VERSION=$(get_version 'rust-lang/rust' $2)
else
RUST_VERSION=$2
fi
if [ -z "$3" ]; then
CROSS_VERSION=$(get_latest_release 'cross-rs/cross')
else
CROSS_VERSION=$3
fi
ZIG_VERSION=$(get_latest_release 'ziglang/zig')
export RUST_VERSION
export CROSS_VERSION
export ZIG_VERSION
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} \
.
echo "Built image ${IMAGE}"
exit 0
${BUILDER} push ${IMAGE}
if [ $? -ne 0 ]; then
echo "Pushing failed: ${BUILDER} push ${IMAGE}"
else
echo "Pushed image ${IMAGE}"
fi