#!/usr/bin/env bash set -e # This file is using docker to run commands SPC_DOCKER_VERSION=v4 # Detect docker can run if ! which docker >/dev/null; then echo "Docker is not installed, please install docker first !" exit 1 fi DOCKER_EXECUTABLE="docker" # shellcheck disable=SC2046 if [ $(id -u) -ne 0 ]; then if ! docker info > /dev/null 2>&1; then if [ "$SPC_USE_SUDO" != "yes" ]; then echo "Docker command requires sudo" # shellcheck disable=SC2039 echo -n 'To use sudo to run docker, run "export SPC_USE_SUDO=yes" and run command again' exit 1 fi DOCKER_EXECUTABLE="sudo docker" fi fi # Convert uname to gnu arch CURRENT_ARCH=$(uname -m) if [ "$CURRENT_ARCH" = "arm64" ]; then CURRENT_ARCH=aarch64 fi if [ -z "$SPC_USE_ARCH" ]; then SPC_USE_ARCH=$CURRENT_ARCH fi # parse SPC_USE_ARCH case $SPC_USE_ARCH in x86_64|amd64) SPC_USE_ARCH=x86_64 if [ "$CURRENT_ARCH" != "x86_64" ]; then PLATFORM_ARG="--platform linux/amd64" ALPINE_FROM=multiarch/alpine:x86_64-edge fi ;; aarch64|arm64) SPC_USE_ARCH=aarch64 if [ "$CURRENT_ARCH" != "aarch64" ]; then PLATFORM_ARG="--platform linux/arm64" ALPINE_FROM=multiarch/alpine:aarch64-edge fi ;; *) echo "Current arch is not supported to run in docker: $SPC_USE_ARCH" exit 1 ;; esac # if ALPINE_FROM is not set, use alpine:edge if [ -z "$ALPINE_FROM" ]; then ALPINE_FROM=alpine:edge fi if [ "$SPC_USE_ARCH" != "$CURRENT_ARCH" ]; then echo "* Using different arch needs to setup qemu-static for docker !" ALPINE_FROM=multiarch/alpine:$SPC_USE_ARCH-edge if [ "$(uname -s)" = "Linux" ]; then $DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null fi else ALPINE_FROM=alpine:edge fi if [ "$SPC_USE_MIRROR" = "yes" ]; then SPC_USE_MIRROR="RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories" else SPC_USE_MIRROR="RUN echo 'Using original repo'" fi # Detect docker env is setup if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION; then echo "Docker container does not exist. Building docker image ..." $DOCKER_EXECUTABLE build $PLATFORM_ARG -t cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <