From 8ff6f243a217b372b9d082483b4f23d4cabc0fc5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 29 Oct 2026 18:58:08 +0100 Subject: [PATCH] scripts/build: omit DWARF debug information by default Pass -w to the Go linker to reduce binary size while retaining the symbol table for tools that inspect Go binaries. For a linux/arm64 build, this reduces the CLI binary from 39,809,791 to 31,310,197 bytes: a saving of 8.5 MB (21.4%). Place the default before caller-supplied GO_LDFLAGS so builds can retain DWARF by passing GO_LDFLAGS="-w=0". Before ls -l /go/src/github.com/docker/cli/build/docker-linux-arm64 -rwxr-xr-x 1 root root 39809791 Oct 29 17:34 /go/src/github.com/docker/cli/build/docker-linux-arm64 After ls -l /go/src/github.com/docker/cli/build/docker-linux-arm64 -rwxr-xr-x 1 root root 31310197 Oct 29 17:36 /go/src/github.com/docker/cli/build/docker-linux-arm64 Signed-off-by: Sebastiaan van Stijn --- scripts/build/.variables | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/build/.variables b/scripts/build/.variables index f6ebfbe971..808e2c8786 100755 --- a/scripts/build/.variables +++ b/scripts/build/.variables @@ -88,7 +88,9 @@ if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then fi export GO_BUILDMODE -GO_LDFLAGS="${GO_LDFLAGS:-}" +# Omit DWARF debug information to reduce binary size, but retain the symbol +# table for tools that inspect Go binaries. Pass GO_LDFLAGS="-w=0" to retain DWARF. +GO_LDFLAGS="-w ${GO_LDFLAGS:-}" # Quotes are interpreted by Go's -ldflags parser, not the shell. # shellcheck disable=SC2089 @@ -133,7 +135,7 @@ if [ -n "$GO_STRIP" ]; then # /usr/local/go/pkg/tool/linux_amd64/link: /usr/local/go/pkg/tool/linux_amd64/link: running strip failed: exit status 1 # llvm-strip: error: unsupported load command (cmd=0x5) # more info: https://github.com/docker/cli/pull/3717 - GO_LDFLAGS="$GO_LDFLAGS -s -w" + GO_LDFLAGS="$GO_LDFLAGS -s" fi export GO_LDFLAGS="$GO_LDFLAGS" # https://github.com/koalaman/shellcheck/issues/2064