From 6f7a931a2d4ce80eaedcd30043d6ba0ba57c928e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 31 Mar 2022 10:30:56 +0200 Subject: [PATCH] [20.10] use GO_LDFLAGS instead of LDFLAGS to prevent inheriting unrelated options When building on Fedora 36, the build failed. I suspect this is because the rpm tools also set LDFLAGS, but with options that cannot be used; GO_LINKMODE=dynamic + ./scripts/build/binary /go/src/github.com/docker/cli ~/rpmbuild/BUILD/src Building dynamic docker-linux-arm64 + go build -o build/docker-linux-arm64 -tags ' pkcs11' -ldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 -Wl,-dT,/root/rpmbuild/BUILD/src/.package_note-docker-ce-cli-0.0.0.20220330082637.68cad50-0.fc36.aarch64.ld -w -X "github.com/docker/cli/cli/version.GitCommit=68cad50" -X "github.com/docker/cli/cli/version.BuildTime=2022-03-30T20:05:36Z" -X "github.com/docker/cli/cli/version.Version=0.0.0-20220330082637-68cad50" -X "github.com/docker/cli/cli/version.PlatformName=Docker Engine - Community"' -buildmode=pie github.com/docker/cli/cmd/docker # github.com/docker/cli/cmd/docker flag provided but not defined: -Wl,-z,relro usage: link [options] main.o This patch changes the variable we use to `GO_LDFLAGS`, taking a similar approach as containerd, and various other projects using this name: https://grep.app/search?q=GO_LDFLAGS Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 391e6ad944b68720aea2c3975ec298eaaeeadd87) Signed-off-by: Sebastiaan van Stijn --- scripts/build/.variables | 4 ++-- scripts/build/binary | 6 +++--- scripts/build/plugins | 2 +- scripts/build/plugins-osx | 2 +- scripts/make.ps1 | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/scripts/build/.variables b/scripts/build/.variables index ece560174a..cc98a5f6dc 100755 --- a/scripts/build/.variables +++ b/scripts/build/.variables @@ -13,13 +13,13 @@ if test -n "${PLATFORM}"; then PLATFORM_LDFLAGS="-X \"github.com/docker/cli/cli/version.PlatformName=${PLATFORM}\"" fi -export LDFLAGS="\ +export GO_LDFLAGS="\ -w \ ${PLATFORM_LDFLAGS} \ -X \"github.com/docker/cli/cli/version.GitCommit=${GITCOMMIT}\" \ -X \"github.com/docker/cli/cli/version.BuildTime=${BUILDTIME}\" \ -X \"github.com/docker/cli/cli/version.Version=${VERSION}\" \ - ${LDFLAGS:-} \ + ${GO_LDFLAGS:-} \ " GOOS="$(go env GOOS)" diff --git a/scripts/build/binary b/scripts/build/binary index e4c5e12a6b..f12a5ac86f 100755 --- a/scripts/build/binary +++ b/scripts/build/binary @@ -47,11 +47,11 @@ if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then fi if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then - LDFLAGS="$LDFLAGS -extldflags -static" + GO_LDFLAGS="$GO_LDFLAGS -extldflags -static" fi if [ -n "$GO_STRIP" ]; then - LDFLAGS="$LDFLAGS -s -w" + GO_LDFLAGS="$GO_LDFLAGS -s -w" fi if [ "$(go env GOOS)" = "windows" ]; then @@ -75,6 +75,6 @@ echo "Building $GO_LINKMODE $(basename "${TARGET}")" export GO111MODULE=auto -go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" --ldflags "${LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}" +go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" --ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}" ln -sf "$(basename "${TARGET}")" "$(dirname "${TARGET}")/docker" diff --git a/scripts/build/plugins b/scripts/build/plugins index f4e9c74600..2ce249bd3d 100755 --- a/scripts/build/plugins +++ b/scripts/build/plugins @@ -17,5 +17,5 @@ for p in cli-plugins/examples/* "$@" ; do echo "Building statically linked $TARGET" export CGO_ENABLED=0 - GO111MODULE=auto go build -o "${TARGET}" --ldflags "${LDFLAGS}" "github.com/docker/cli/${p}" + GO111MODULE=auto go build -o "${TARGET}" --ldflags "${GO_LDFLAGS}" "github.com/docker/cli/${p}" done diff --git a/scripts/build/plugins-osx b/scripts/build/plugins-osx index 8e870f4f05..f0390b91ad 100755 --- a/scripts/build/plugins-osx +++ b/scripts/build/plugins-osx @@ -12,7 +12,7 @@ export GOOS=darwin export GOARCH=amd64 export CC=o64-clang export CXX=o64-clang++ -export LDFLAGS="$LDFLAGS -linkmode external -s" +export GO_LDFLAGS="$GO_LDFLAGS -linkmode external -s" export LDFLAGS_STATIC_DOCKER='-extld='${CC} source ./scripts/build/plugins diff --git a/scripts/make.ps1 b/scripts/make.ps1 index 7dd3fafb4d..0b899f772d 100644 --- a/scripts/make.ps1 +++ b/scripts/make.ps1 @@ -109,12 +109,12 @@ Function Execute-Build($additionalBuildTags, $directory) { Write-Host "INFO: Building..." $buildTime=$(Get-Date).ToUniversalTime() - $env:LDFLAGS="-linkmode=internal ` + $env:GO_LDFLAGS="-linkmode=internal ` -X \""github.com/docker/cli/cli/version.Version=$dockerVersion\"" ` -X \""github.com/docker/cli/cli/version.GitCommit=$gitCommit\"" ` -X \""github.com/docker/cli/cli/version.BuildTime=$buildTime\""" if ($env:PLATFORM) { - $env:LDFLAGS="$env:LDFLAGS -X \""github.com/docker/cli/cli/version.PlatformName=$env:PLATFORM\""" + $env:GO_LDFLAGS="$env:GO_LDFLAGS -X \""github.com/docker/cli/cli/version.PlatformName=$env:PLATFORM\""" } # Generate a version in the form major,minor,patch,build @@ -138,7 +138,7 @@ Function Execute-Build($additionalBuildTags, $directory) { # By using --% we can use \"key=%foo%\" and have a environment variable foo that contains spaces go build $raceParm $verboseParm $allParm $optParm -tags "$buildTags" ` -o "$root\build\$directory.exe" ` - -ldflags --% "%LDFLAGS%" + -ldflags --% "%GO_LDFLAGS%" if ($LASTEXITCODE -ne 0) { Throw "Failed to compile" } Pop-Location; $global:pushed=$False