From 462326afba0395aa28e094e29339b025354af8f7 Mon Sep 17 00:00:00 2001 From: Abhisek Banerjee Date: Tue, 20 Oct 2020 23:17:49 +0000 Subject: [PATCH] Support cross compilation only on amd64. Based on a patch to Bottlerocket by Ben Cressey https://github.com/bottlerocket-os/bottlerocket/blob/41a72fe21400fa227543fcbed2e59eb0a8b633c1/packages/kubernetes-1.17/0001-always-set-relevant-variables-for-cross-compiling.patch --- hack/lib/golang.sh | 52 ++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index a96e88e8958f..bef1d837703e 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -392,36 +392,30 @@ kube::golang::set_platform_envs() { export GOOS=${platform%/*} export GOARCH=${platform##*/} - # Apply standard values for CGO_ENABLED and CC unless KUBE_BUILD_PLATFORMS is set. - if [ -z "${KUBE_BUILD_PLATFORMS:-}" ] ; then - export CGO_ENABLED=0 - export CC=gcc - return + + # Do not set CC when building natively on a platform, only if cross-compiling from linux/amd64 + if [[ $(kube::golang::host_platform) == "linux/amd64" ]]; then + # Dynamic CGO linking for other server architectures than linux/amd64 goes here + # If you want to include support for more server platforms than these, add arch-specific gcc names here + case "${platform}" in + "linux/arm") + export CGO_ENABLED=1 + export CC=${KUBE_LINUX_ARM_CC:-arm-linux-gnueabihf-gcc} + ;; + "linux/arm64") + export CGO_ENABLED=1 + export CC=${KUBE_LINUX_ARM64_CC:-aarch64-linux-gnu-gcc} + ;; + "linux/ppc64le") + export CGO_ENABLED=1 + export CC=${KUBE_LINUX_PPC64LE_CC:-powerpc64le-linux-gnu-gcc} + ;; + "linux/s390x") + export CGO_ENABLED=1 + export CC=${KUBE_LINUX_S390X_CC:-s390x-linux-gnu-gcc} + ;; + esac fi - # Dynamic CGO linking for other server architectures goes here - # If you want to include support for more server platforms than these, add arch-specific gcc names here - case "${platform}" in - "linux/amd64") - export CGO_ENABLED=1 - export CC=${LINUX_AMD64_CC:-gcc} - ;; - "linux/arm") - export CGO_ENABLED=1 - export CC=${LINUX_ARM_CC:-arm-linux-gnueabihf-gcc} - ;; - "linux/arm64") - export CGO_ENABLED=1 - export CC=${LINUX_ARM64_CC:-aarch64-linux-gnu-gcc} - ;; - "linux/ppc64le") - export CGO_ENABLED=1 - export CC=${LINUX_PPC64LE_CC:-powerpc64le-linux-gnu-gcc} - ;; - "linux/s390x") - export CGO_ENABLED=1 - export CC=${LINUX_S390X_CC:-s390x-linux-gnu-gcc} - ;; - esac } kube::golang::unset_platform_envs() {