From a507048471cf2865ccce800b2a5d2a4e59dc7063 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Sat, 17 Jul 2021 12:52:50 +0800 Subject: [PATCH 1/2] go: reduce package size by stripping binaries and removing pkg A very long time ago strip didn't always work well on Go binaries, but this has been fixed for almost a decade[1], so there is no need to keep this. Just the standard strip hook won't work, as it will try (and fail) to strip files in testdata and some other things. You can add nostrip_files, but that would be a long list. [1]: http://dominik.honnef.co/posts/2016/10/go-and-strip/ --- This also removes the pkg/linux_arch directory from the installation. This is a pre-compiled cache of the standard library, and isn't really needed. The only downside of not installing it is that the first compile will take a few seconds longer (after which it's in the cache), and since the Go compiler is fairly fast it's not a big deal IMO. Alternatively, only pkg/cmd could be removed. This contains the cache for the go command and "go tool [..]" commands, but you can never import this as it's all "package main", so it's a useless cache AFAIK. It's also the biggest contributor to the size. --- Comparison of sizes (each one relative to the previous one): current Package: 111M Installed: 369M rm pkg/cmd Package: 90M (-21M) Installed: 268M (-101M) strip Package: 62M (-28M) Installed: 236M (-32M) rm pkg Package: 45M (-17M) Installed: 158M (-78M) So that's a total saving of 66M in the package size, and 211M for the installed size. --- srcpkgs/go/template | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/srcpkgs/go/template b/srcpkgs/go/template index 6ac24704525d..3964e616d32d 100644 --- a/srcpkgs/go/template +++ b/srcpkgs/go/template @@ -1,7 +1,7 @@ # Template file for 'go' pkgname=go version=1.16.5 -revision=1 +revision=2 create_wrksrc=yes build_wrksrc=go hostmakedepends="go1.12-bootstrap" @@ -64,6 +64,13 @@ do_install() { cp -r doc misc -t ${DESTDIR}/usr/share/go ln -s /usr/share/go/doc ${DESTDIR}/usr/lib/go/doc + if [ -z "$XBPS_DEBUG_PKGS" ]; then + /usr/bin/$STRIP \ + ${DESTDIR}/usr/bin/go \ + ${DESTDIR}/usr/bin/gofmt \ + ${DESTDIR}/usr/lib/go/pkg/tool/linux_${_goarch}/* + fi + # This is to make go get code.google.com/p/go-tour/gotour and # then running the gotour executable work out of the box. # @@ -81,6 +88,7 @@ do_install() { rm -f ${DESTDIR}/usr/lib/go/pkg/tool/*/api rm -rf ${DESTDIR}/usr/lib/go/pkg/bootstrap rm -rf ${DESTDIR}/usr/lib/go/pkg/obj + rm -rf ${DESTDIR}/usr/lib/go/pkg/linux_${_goarch} vlicense LICENSE } From ecda54ff8a59bfc292e5acc806d1754868339460 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Sun, 18 Jul 2021 13:53:44 +0800 Subject: [PATCH 2/2] Also remove some stuff from src Don't really need _test.go files or testdata The cmd package is only useful for "go doc cmd/.."; which is referenced in various places in "go help [..]". We can't really remove all of that without breaking it, but we can at least remove the internal and vendored stuff, which is quite a lot of data. --- srcpkgs/go/template | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/srcpkgs/go/template b/srcpkgs/go/template index 3964e616d32d..80ff8c94d1b6 100644 --- a/srcpkgs/go/template +++ b/srcpkgs/go/template @@ -12,7 +12,7 @@ homepage="http://golang.org/" changelog="https://golang.org/doc/devel/release.html" distfiles="https://golang.org/dl/go${version}.src.tar.gz" checksum=7bfa7e5908c7cc9e75da5ddf3066d7cbcf3fd9fa51945851325eebc17f50ba80 -nostrip=yes +nopie=yes noverifyrdeps=yes case "${XBPS_TARGET_MACHINE}" in @@ -64,12 +64,12 @@ do_install() { cp -r doc misc -t ${DESTDIR}/usr/share/go ln -s /usr/share/go/doc ${DESTDIR}/usr/lib/go/doc - if [ -z "$XBPS_DEBUG_PKGS" ]; then - /usr/bin/$STRIP \ - ${DESTDIR}/usr/bin/go \ - ${DESTDIR}/usr/bin/gofmt \ - ${DESTDIR}/usr/lib/go/pkg/tool/linux_${_goarch}/* - fi + # Remove things from src that we don't need. + find ${DESTDIR}/usr/lib/go/src -name '*_test.go' -o -name 'testdata' -exec rm -rf {} \+ + rm ${DESTDIR}/usr/lib/go/src/*.{bash,rc,bat,dist} + + # cmd source is only useful for "go doc cmd/...". + find ${DESTDIR}/usr/lib/go/src/cmd -name 'internal' -o -name 'vendor' -exec rm -rf {} \+ # This is to make go get code.google.com/p/go-tour/gotour and # then running the gotour executable work out of the box. @@ -77,7 +77,6 @@ do_install() { # Also, /usr/bin is the place for system-wide executables, # not /usr/lib/go/bin. Users should use different paths by # setting the appropriate environment variables. - # ln -sf /usr/bin ${DESTDIR}/usr/lib/go/bin # sigh. well, someone fix the template and add