Skip to content

Commit 856c11b

Browse files
committed
enable (by default) omitting debug symbols to shrink binary size
1 parent e21b85e commit 856c11b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

build-go-project.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ gobuildmaybe() {
119119

120120
local workdir="$(pwd)"
121121

122+
local ldFlagsAdditional=""
123+
if [ "${BUILDOPT_DEBUG:-}" = "" ]; then # not doing debug build?
124+
# https://www.codingexplorations.com/blog/reducing-binary-size-in-go-strip-unnecessary-data-with-ldflags-w-s
125+
# where:
126+
# -w: This flag omits the DWARF symbol table, effectively removing debugging information.
127+
# -s: This strips the symbol table and debug information from the binary.
128+
#
129+
# ldflags go to `$ go tool link`. you can see these flags by running that command to see help.
130+
ldFlagsAdditional="-w -s"
131+
fi
132+
122133
# NOTE: setting the "dynversion.Version" doesn't work when code under vendor/, but seems
123134
# to work now fine with Go modules. https://github.com/golang/go/issues/19000
124135

@@ -129,7 +140,7 @@ gobuildmaybe() {
129140

130141
# compile statically so this works on Alpine Linux that doesn't have glibc
131142
(cd "$dir_in_which_to_compile" && GOARM=6 GOOS="$os" GOARCH="$architecture" CGO_ENABLED=0 go build \
132-
-ldflags "-extldflags \"-static\" -X github.com/function61/gokit/app/dynversion.Version=$FRIENDLY_REV_ID" \
143+
-ldflags "$ldFlagsAdditional -extldflags \"-static\" -X github.com/function61/gokit/app/dynversion.Version=$FRIENDLY_REV_ID" \
133144
-o "$projectroot/rel/${BINARY_NAME}${binSuffix}")
134145
}
135146

0 commit comments

Comments
 (0)