blake / external-mdns

A service to advertise records for Kubernetes resources on a LAN over multicast DNS.
Apache License 2.0
65 stars 9 forks source link

Cross-compile to avoid a lot of QEMU #16

Closed Stelminator closed 7 months ago

Stelminator commented 10 months ago

In the interests of getting more target platforms upstream (https://github.com/blake/external-mdns/issues/15).

Leverage Golang's cross-compilation to run compilation natively, but target different architectures. This alleviates the need to run the compiler under emulation of the targets. Effectively, any target runs as fast as the native arch.

docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5 .

Takes under a minute to run on my Core i7-11700KF.

TARGETVARIANT will be v5/v6/v7 for the above arm targets, but was empty for the rest. I'm unsure about other architectures with variants, but this could be adapted further to only set GOARM for arm targets.

If this doesn't work out of the box (buildx seems to cache some arguments), then adding ARG BUILDPLATFORM at the top of Dockerfile and --build-arg BUILDPLATFORM=<arch> to the command should do the trick, which for me resulted in:

docker buildx build --build-arg BUILDPLATFORM=amd64 --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5 .