Skip to content

Commit

Permalink
enable (by default) omitting debug symbols to shrink binary size
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Apr 5, 2024
1 parent e21b85e commit 856c11b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion build-go-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ gobuildmaybe() {

local workdir="$(pwd)"

local ldFlagsAdditional=""
if [ "${BUILDOPT_DEBUG:-}" = "" ]; then # not doing debug build?
# https://www.codingexplorations.com/blog/reducing-binary-size-in-go-strip-unnecessary-data-with-ldflags-w-s
# where:
# -w: This flag omits the DWARF symbol table, effectively removing debugging information.
# -s: This strips the symbol table and debug information from the binary.
#
# ldflags go to `$ go tool link`. you can see these flags by running that command to see help.
ldFlagsAdditional="-w -s"
fi

# NOTE: setting the "dynversion.Version" doesn't work when code under vendor/, but seems
# to work now fine with Go modules. https://github.com/golang/go/issues/19000

Expand All @@ -129,7 +140,7 @@ gobuildmaybe() {

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

Expand Down

0 comments on commit 856c11b

Please sign in to comment.