[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 <github@gone.nl>
(cherry picked from commit 391e6ad944)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-03-31 10:30:56 +02:00
parent 16169434a4
commit 6f7a931a2d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 10 additions and 10 deletions

View File

@ -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)"

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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