From 0e4c0035b6a64855541f6b4886b9455272d3cba0 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 3 Oct 2022 17:25:46 +0100 Subject: [PATCH] Maybe make cross-compiling work? --- Dockerfile | 6 ++++-- build/docker/crossbuild.sh | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 build/docker/crossbuild.sh diff --git a/Dockerfile b/Dockerfile index 10861a95c..2f1686672 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/build/docker/crossbuild.sh b/build/docker/crossbuild.sh new file mode 100644 index 000000000..35b2fe85a --- /dev/null +++ b/build/docker/crossbuild.sh @@ -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/... \ No newline at end of file