#!/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" ] && [ "$SPC_DOCKER_DEBUG" != "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 SPC_USE_ARCH_DOCKER=amd64 if [ "$CURRENT_ARCH" != "x86_64" ]; then PLATFORM_ARG="--platform linux/amd64" fi ;; aarch64|arm64) SPC_USE_ARCH=aarch64 SPC_USE_ARCH_DOCKER=arm64 if [ "$CURRENT_ARCH" != "aarch64" ]; then PLATFORM_ARG="--platform linux/arm64" fi ;; *) echo "Current arch is not supported to run in docker: $SPC_USE_ARCH" exit 1 ;; esac # detect if we need to use qemu-static if [ "$SPC_USE_ARCH" != "$CURRENT_ARCH" ]; then if [ "$(uname -s)" = "Linux" ]; then echo "* Using different arch needs to setup qemu-static for docker !" $DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static --reset -p yes > /dev/null fi fi # Detect docker env is setup if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION; then echo "Docker container does not exist. Building docker image ..." $DOCKER_EXECUTABLE buildx build $PLATFORM_ARG --no-cache -t cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <> /etc/bashrc RUN source /etc/bashrc RUN yum install -y which RUN curl -fsSL -o patchelf.tgz https://github.com/NixOS/patchelf/releases/download/0.18.0/patchelf-0.18.0-$SPC_USE_ARCH.tar.gz && \ mkdir -p /patchelf && \ tar -xzf patchelf.tgz -C /patchelf --strip-components=1 && \ cp /patchelf/bin/patchelf /usr/bin/ RUN curl -o cmake.tgz -fsSL https://github.com/Kitware/CMake/releases/download/v3.31.4/cmake-3.31.4-linux-$SPC_USE_ARCH.tar.gz && \ mkdir /cmake && \ tar -xzf cmake.tgz -C /cmake --strip-components 1 WORKDIR /app ADD ./src /app/src COPY ./composer.* /app/ ADD ./bin/setup-runtime /app/bin/setup-runtime ADD ./bin/spc /app/bin/spc RUN /app/bin/setup-runtime RUN /app/bin/php /app/bin/composer install --no-dev ENV PATH="/app/bin:/cmake/bin:$PATH" ENV SPC_TARGET=glibc ADD ./config/env.ini /app/config/env.ini RUN CC=gcc bin/spc doctor --auto-fix --debug RUN curl -o make.tgz -fsSL https://ftp.gnu.org/gnu/make/make-4.4.tar.gz && \ tar -zxvf make.tgz && \ cd make-4.4 && \ ./configure && \ make && \ make install && \ ln -sf /usr/local/bin/make /usr/bin/make RUN curl -o automake.tgz -fsSL https://ftp.gnu.org/gnu/automake/automake-1.17.tar.xz && \ tar -xvf automake.tgz && \ cd automake-1.17 && \ ./configure && \ make && \ make install && \ ln -sf /usr/local/bin/automake /usr/bin/automake EOF fi # Check if in ci (local terminal can execute with -it) if [ -t 0 ]; then INTERACT=-it else INTERACT='' fi # Mounting volumes MOUNT_LIST="" # shellcheck disable=SC2089 MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/config:/app/config" MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/src:/app/src" MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/buildroot:/app/buildroot" MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/source:/app/source" MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/dist:/app/dist" MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/downloads:/app/downloads" MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/pkgroot:/app/pkgroot" if [ -f "$(pwd)/craft.yml" ]; then MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/craft.yml:/app/craft.yml" fi # Apply env in temp env file echo 'CC=/opt/rh/devtoolset-10/root/usr/bin/gcc' > /tmp/spc-gnu-docker.env echo 'CXX=/opt/rh/devtoolset-10/root/usr/bin/g++' >> /tmp/spc-gnu-docker.env echo 'AR=/opt/rh/devtoolset-10/root/usr/bin/ar' >> /tmp/spc-gnu-docker.env echo 'LD=/opt/rh/devtoolset-10/root/usr/bin/ld' >> /tmp/spc-gnu-docker.env echo 'SPC_DEFAULT_C_FLAGS=-fPIC' >> /tmp/spc-gnu-docker.env echo 'SPC_TARGET=glibc' >> /tmp/spc-gnu-docker.env echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LDFLAGS_PROGRAM="-Wl,-O1 -pie"' >> /tmp/spc-gnu-docker.env echo 'SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS="-ldl -lpthread -lm -lresolv -lutil -lrt"' >> /tmp/spc-gnu-docker.env # Environment variable passthrough ENV_LIST="" ENV_LIST="$ENV_LIST -e SPC_FIX_DEPLOY_ROOT="$(pwd)"" if [ ! -z "$GITHUB_TOKEN" ]; then ENV_LIST="$ENV_LIST -e GITHUB_TOKEN=$GITHUB_TOKEN" fi # Run docker # shellcheck disable=SC2068 # shellcheck disable=SC2086 # shellcheck disable=SC2090 if [ "$SPC_DOCKER_DEBUG" = "yes" ]; then echo "* Debug mode enabled, run docker in interactive mode." echo "* You can use 'exit' to exit the docker container." echo "* You can use 'bin/spc' like normal builds." echo "*" echo "* Mounted directories:" echo "* ./config: $(pwd)/config" echo "* ./src: $(pwd)/src" echo "* ./buildroot: $(pwd)/buildroot" echo "* ./source: $(pwd)/source" echo "* ./dist: $(pwd)/dist" echo "* ./downloads: $(pwd)/downloads" echo "* ./pkgroot: $(pwd)/pkgroot" echo "*" set -ex $DOCKER_EXECUTABLE run $PLATFORM_ARG --privileged --rm -it $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION /bin/bash else $DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc $@ fi