Skip to content

Commit a0f9985

Browse files
authored
Fix alpine-docker build in different arch issue (#787)
* Fix alpine-docker build in different arch issue * Fix spc-gnu-docker for multi-arch build issue
1 parent f3f581f commit a0f9985

File tree

2 files changed

+80
-40
lines changed

2 files changed

+80
-40
lines changed

bin/spc-alpine-docker

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env bash
2+
3+
set -e
24

35
# This file is using docker to run commands
4-
SPC_DOCKER_VERSION=v3
6+
SPC_DOCKER_VERSION=v4
57

68
# Detect docker can run
79
if ! which docker >/dev/null; then
8-
echo "Docker is not installed, please install docker first !"
9-
exit 1
10+
echo "Docker is not installed, please install docker first !"
11+
exit 1
1012
fi
1113
DOCKER_EXECUTABLE="docker"
1214
# shellcheck disable=SC2046
@@ -22,27 +24,48 @@ if [ $(id -u) -ne 0 ]; then
2224
fi
2325
fi
2426

25-
26-
27-
# to check if qemu-docker run
28-
if [ "$SPC_USE_ARCH" = "" ]; then
29-
SPC_USE_ARCH=x86_64
27+
# Convert uname to gnu arch
28+
CURRENT_ARCH=$(uname -m)
29+
if [ "$CURRENT_ARCH" = "arm64" ]; then
30+
CURRENT_ARCH=aarch64
31+
fi
32+
if [ -z "$SPC_USE_ARCH" ]; then
33+
SPC_USE_ARCH=$CURRENT_ARCH
3034
fi
35+
# parse SPC_USE_ARCH
3136
case $SPC_USE_ARCH in
32-
x86_64)
33-
ALPINE_FROM=alpine:edge
37+
x86_64|amd64)
38+
SPC_USE_ARCH=x86_64
39+
if [ "$CURRENT_ARCH" != "x86_64" ]; then
40+
PLATFORM_ARG="--platform linux/amd64"
41+
ALPINE_FROM=multiarch/alpine:x86_64-edge
42+
fi
3443
;;
35-
aarch64)
36-
ALPINE_FROM=multiarch/alpine:aarch64-edge
37-
# shellcheck disable=SC2039
38-
echo -e "\e[033m* Using different arch needs to setup qemu-static for docker !\e[0m"
39-
$DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null
44+
aarch64|arm64)
45+
SPC_USE_ARCH=aarch64
46+
if [ "$CURRENT_ARCH" != "aarch64" ]; then
47+
PLATFORM_ARG="--platform linux/arm64"
48+
ALPINE_FROM=multiarch/alpine:aarch64-edge
49+
fi
4050
;;
4151
*)
4252
echo "Current arch is not supported to run in docker: $SPC_USE_ARCH"
4353
exit 1
4454
;;
4555
esac
56+
# if ALPINE_FROM is not set, use alpine:edge
57+
if [ -z "$ALPINE_FROM" ]; then
58+
ALPINE_FROM=alpine:edge
59+
fi
60+
if [ "$SPC_USE_ARCH" != "$CURRENT_ARCH" ]; then
61+
echo "* Using different arch needs to setup qemu-static for docker !"
62+
ALPINE_FROM=multiarch/alpine:$SPC_USE_ARCH-edge
63+
if [ "$(uname -s)" = "Linux" ]; then
64+
$DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null
65+
fi
66+
else
67+
ALPINE_FROM=alpine:edge
68+
fi
4669

4770
if [ "$SPC_USE_MIRROR" = "yes" ]; then
4871
SPC_USE_MIRROR="RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories"
@@ -53,7 +76,7 @@ fi
5376
# Detect docker env is setup
5477
if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION; then
5578
echo "Docker container does not exist. Building docker image ..."
56-
$DOCKER_EXECUTABLE build -t cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <<EOF
79+
$DOCKER_EXECUTABLE build $PLATFORM_ARG -t cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <<EOF
5780
FROM $ALPINE_FROM
5881
$SPC_USE_MIRROR
5982
RUN apk update; \
@@ -148,7 +171,7 @@ if [ "$SPC_DOCKER_DEBUG" = "yes" ]; then
148171
echo "* ./pkgroot: $(pwd)/pkgroot"
149172
echo "*"
150173
set -ex
151-
$DOCKER_EXECUTABLE run --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION
174+
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION /bin/bash
152175
else
153-
$DOCKER_EXECUTABLE run --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc $@
176+
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc $@
154177
fi

bin/spc-gnu-docker

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env bash
22

3+
set -e
4+
5+
# This file is using docker to run commands
6+
SPC_DOCKER_VERSION=v4
7+
38
# Detect docker can run
49
if ! which docker >/dev/null; then
510
echo "Docker is not installed, please install docker first !"
@@ -19,35 +24,47 @@ if [ $(id -u) -ne 0 ]; then
1924
fi
2025
fi
2126

22-
23-
24-
# to check if qemu-docker run
25-
if [ "$SPC_USE_ARCH" = "" ]; then
26-
SPC_USE_ARCH=current
27+
# Convert uname to gnu arch
28+
CURRENT_ARCH=$(uname -m)
29+
if [ "$CURRENT_ARCH" = "arm64" ]; then
30+
CURRENT_ARCH=aarch64
2731
fi
32+
if [ -z "$SPC_USE_ARCH" ]; then
33+
SPC_USE_ARCH=$CURRENT_ARCH
34+
fi
35+
# parse SPC_USE_ARCH
2836
case $SPC_USE_ARCH in
29-
current)
30-
BASE_ARCH=$(uname -m)
31-
if [ "$BASE_ARCH" = "arm64" ]; then
32-
BASE_ARCH=aarch64
37+
x86_64|amd64)
38+
SPC_USE_ARCH=x86_64
39+
SPC_USE_ARCH_DOCKER=amd64
40+
if [ "$CURRENT_ARCH" != "x86_64" ]; then
41+
PLATFORM_ARG="--platform linux/amd64"
3342
fi
3443
;;
35-
aarch64)
36-
BASE_ARCH=aarch64
37-
# shellcheck disable=SC2039
38-
echo -e "\e[033m* Using different arch needs to setup qemu-static for docker !\e[0m"
39-
$DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null
44+
aarch64|arm64)
45+
SPC_USE_ARCH=aarch64
46+
SPC_USE_ARCH_DOCKER=arm64
47+
if [ "$CURRENT_ARCH" != "aarch64" ]; then
48+
PLATFORM_ARG="--platform linux/arm64"
49+
fi
4050
;;
4151
*)
4252
echo "Current arch is not supported to run in docker: $SPC_USE_ARCH"
4353
exit 1
4454
;;
4555
esac
56+
# detect if we need to use qemu-static
57+
if [ "$SPC_USE_ARCH" != "$CURRENT_ARCH" ]; then
58+
if [ "$(uname -s)" = "Linux" ]; then
59+
echo "* Using different arch needs to setup qemu-static for docker !"
60+
$DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static --reset -p yes > /dev/null
61+
fi
62+
fi
4663

4764
# Detect docker env is setup
48-
if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-gnu-$SPC_USE_ARCH; then
65+
if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION; then
4966
echo "Docker container does not exist. Building docker image ..."
50-
$DOCKER_EXECUTABLE build -t cwcc-spc-gnu-$SPC_USE_ARCH -f- . <<EOF
67+
$DOCKER_EXECUTABLE buildx build $PLATFORM_ARG --no-cache -t cwcc-spc-gnu-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <<EOF
5168
FROM centos:7
5269
RUN sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/*.repo && \
5370
sed -i 's/^#.*baseurl=http/baseurl=http/g' /etc/yum.repos.d/*.repo && \
@@ -59,7 +76,7 @@ RUN yum clean all && \
5976
6077
RUN yum install -y centos-release-scl
6178
62-
RUN if [ "$BASE_ARCH" = "aarch64" ]; then \
79+
RUN if [ "$SPC_USE_ARCH" = "aarch64" ]; then \
6380
sed -i 's|mirror.centos.org/centos|vault.centos.org/altarch|g' /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo ; \
6481
sed -i 's|mirror.centos.org/centos|vault.centos.org/altarch|g' /etc/yum.repos.d/CentOS-SCLo-scl.repo ; \
6582
else \
@@ -74,12 +91,12 @@ RUN echo "source scl_source enable devtoolset-10" >> /etc/bashrc
7491
RUN source /etc/bashrc
7592
RUN yum install -y which
7693
77-
RUN curl -fsSL -o patchelf.tgz https://github.com/NixOS/patchelf/releases/download/0.18.0/patchelf-0.18.0-$BASE_ARCH.tar.gz && \
94+
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 && \
7895
mkdir -p /patchelf && \
7996
tar -xzf patchelf.tgz -C /patchelf --strip-components=1 && \
8097
cp /patchelf/bin/patchelf /usr/bin/
8198
82-
RUN curl -o cmake.tgz -fsSL https://github.com/Kitware/CMake/releases/download/v3.31.4/cmake-3.31.4-linux-$BASE_ARCH.tar.gz && \
99+
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 && \
83100
mkdir /cmake && \
84101
tar -xzf cmake.tgz -C /cmake --strip-components 1
85102
@@ -173,7 +190,7 @@ if [ "$SPC_DOCKER_DEBUG" = "yes" ]; then
173190
echo "* ./pkgroot: $(pwd)/pkgroot"
174191
echo "*"
175192
set -ex
176-
$DOCKER_EXECUTABLE run --rm -it --privileged $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH
193+
$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
177194
else
178-
$DOCKER_EXECUTABLE run --rm $INTERACT $ENV_LIST --env-file /tmp/spc-gnu-docker.env $MOUNT_LIST cwcc-spc-gnu-$SPC_USE_ARCH bin/spc $@
195+
$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 $@
179196
fi

0 commit comments

Comments
 (0)