diff --git a/Dockerfile b/Dockerfile index 4cdcd40e8..bf5831832 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/build/docker/crossbuild.sh b/build/docker/crossbuild.sh index 3e5e3eb04..5ab09793d 100644 --- a/build/docker/crossbuild.sh +++ b/build/docker/crossbuild.sh @@ -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/... \ No newline at end of file + +# Build Dendrite and tools, statically linking them. +CGO_ENABLED=1 go build -v -ldflags="-linkmode external -extldflags -static ${FLAGS}" -trimpath -o /out/ ./cmd/...