Detect native vs cross-compile

This commit is contained in:
Neil Alexander 2022-10-04 10:00:10 +01:00
parent cc034a59d8
commit f12ea645ec
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 59 additions and 26 deletions

View file

@ -14,8 +14,6 @@ WORKDIR /src
ARG TARGETOS
ARG TARGETARCH
ARG FLAGS
ENV GOOS $TARGETOS
ENV GOARCH $TARGETARCH
RUN --mount=target=. \
--mount=type=cache,target=/root/.cache/go-build \
sh ./build/docker/crossbuild.sh

View file

@ -1,32 +1,67 @@
#!/bin/sh
set -ex
set -e
case $GOARCH in
arm64)
curl -s https://musl.cc/aarch64-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=aarch64-linux-musl-gcc
;;
# In order to cross-compile with the multi-stage Docker builds, we need to
# ensure that the suitable toolchain for cross-compiling is installed. Since
# the images are Alpine-based, we will use musl. Download and install the
# toolchain inside the build container.
amd64)
curl -s https://musl.cc/x86_64-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=x86_64-linux-musl-gcc
;;
USERARCH=`go env GOARCH`
GOARCH="$TARGETARCH"
GOOS="linux"
386)
curl -s https://musl.cc/i686-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=i686-linux-musl-gcc
;;
echo "Target arch: $TARGETARCH"
echo "User arch: $USERARCH"
arm)
curl -s https://musl.cc/armv7l-linux-musleabihf-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=armv7l-linux-musleabihf-gcc
;;
if [ "$TARGETARCH" != "$USERARCH" ]; then
echo "Cross compile"
case $GOARCH in
arm64)
curl -s https://musl.cc/aarch64-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=aarch64-linux-musl-gcc
;;
*)
echo "Unknown GOARCH=${GOARCH}"
exit 1
;;
esac
amd64)
curl -s https://musl.cc/x86_64-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=x86_64-linux-musl-gcc
;;
386)
curl -s https://musl.cc/i686-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=i686-linux-musl-gcc
;;
arm)
curl -s https://musl.cc/armv7l-linux-musleabihf-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=armv7l-linux-musleabihf-gcc
;;
s390x)
curl -s https://musl.cc/s390x-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=s390x-linux-musl-gcc
;;
ppc64le)
curl -s https://musl.cc/powerpc64le-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=powerpc64le-linux-musl-gcc
;;
riscv64)
curl -s https://musl.cc/riscv64-linux-musl-cross.tgz | tar xz --strip-components=1 -C /usr
export CC=riscv64-linux-musl-gcc
;;
*)
echo "Unsupported GOARCH=${GOARCH}"
exit 1
;;
esac
else
echo "Native compile"
fi
# Output the go environment just in case it is useful for debugging.
go env
CGO_ENABLED=1 go build -v -ldflags="-linkmode external -extldflags -static ${FLAGS}" -trimpath -o /out/ ./cmd/...
# Build Dendrite and tools, statically linking them.
CGO_ENABLED=1 go build -v -ldflags="-linkmode external -extldflags -static ${FLAGS}" -trimpath -o /out/ ./cmd/...