tech-pkg archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

go: shrink binaries



Hello,

To all go modules, can we add:

-ldflags='-s -w' to omit symbol table (-s) and strip debug information (-w), and also 
-trimpath to avoid baking full GOPATH/module paths into binaries (without it, there is a work directory reference, so CHECK_WRKREF_SKIP can possible be removed in lang/go/go-module.mk)

With these flags, my binaries are about 2/3 the normal size.

I created a patch

diff -u -r1.25 go-module.mk
--- lang/go/go-module.mk 24 Jan 2026 16:50:59 -0000 1.25
+++ lang/go/go-module.mk 25 May 2026 06:52:23 -0000
@@ -45,6 +45,7 @@
   GO_BUILD_PATTERN?= ./...
 GO_EXTRA_MOD_DIRS?=
+GOFLAGS?= -ldflags='-s -w'
   MAKE_JOBS_SAFE= no
 INSTALLATION_DIRS+= bin
@@ -90,7 +91,7 @@
  ${RUN} cd ${WRKSRC} && ${_ULIMIT_CMD} ${PKGSRC_SETENV} ${MAKE_ENV} \
  ${GO} telemetry off >/dev/null 2>&1 || ${TRUE}
  ${RUN} cd ${WRKSRC} && ${_ULIMIT_CMD} ${PKGSRC_SETENV} ${MAKE_ENV} \
- ${GO} install -v ${GOFLAGS} ${GO_BUILD_PATTERN}
+ ${GO} install -v -trimpath ${GOFLAGS} ${GO_BUILD_PATTERN}
 .endif
   .if !target(do-test)


But GOFLAGS won't propagate, because many packages override it.

Is there a better way to achieve this?

Kind regards,
Adam



Home | Main Index | Thread Index | Old Index