From 6b672863c7ceb1b4bd62ce189e9cf214e6f2753c Mon Sep 17 00:00:00 2001 From: bnrjee Date: Mon, 31 Aug 2020 17:42:27 +0000 Subject: [PATCH] Always set relevant variables for cross compiling 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, 29 insertions(+), 23 deletions(-) diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index acc3cb4d2ac..a96e88e8958 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -392,30 +392,36 @@ kube::golang::set_platform_envs() { export GOOS=${platform%/*} export GOARCH=${platform##*/} - - # 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=arm-linux-gnueabihf-gcc - ;; - "linux/arm64") - export CGO_ENABLED=1 - export CC=aarch64-linux-gnu-gcc - ;; - "linux/ppc64le") - export CGO_ENABLED=1 - export CC=powerpc64le-linux-gnu-gcc - ;; - "linux/s390x") - export CGO_ENABLED=1 - export CC=s390x-linux-gnu-gcc - ;; - esac + # 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 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() {