Maybe make cross-compiling work?

This commit is contained in:
Neil Alexander 2022-10-03 17:25:46 +01:00
parent 61aaad087e
commit 0e4c0035b6
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 36 additions and 2 deletions

View file

@ -4,7 +4,7 @@
# base installs required dependencies and runs go mod download to cache dependencies
#
FROM --platform=${BUILDPLATFORM} docker.io/golang:1.19-alpine AS base
RUN apk --update --no-cache add bash build-base
RUN apk --update --no-cache add bash build-base curl
#
# build creates all needed binaries
@ -14,9 +14,11 @@ 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 \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="${FLAGS}" -trimpath -o /out/ ./cmd/...
sh ./build/docker/crossbuild.sh
#
# The dendrite base image; mainly creates a user and switches to it

View file

@ -0,0 +1,32 @@
#!/bin/sh
set -ex
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
;;
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
;;
i386)
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
;;
*)
echo "Unknown GOARCH=${GOARCH}"
exit 1
;;
esac
go env
CGO_ENABLED=1 go build -v -ldflags="-linkmode external -extldflags -static ${FLAGS}" -trimpath -o /out/ ./cmd/...