Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile.gen.go: -trimpath for binary size and reproducibility #1045

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lyind
Copy link
Contributor

@lyind lyind commented Jul 15, 2024

Summary

Adds the -trimpath flag to the generated Makefile for Go projects.

Go's -trimpath feature removes the local segments from dependency paths when building. This can reduce binary size by megabytes (for some projects) and generally aids reproducability.

This is the help entry for the compiler feature:

	-trimpath
		remove all file system paths from the resulting executable.
		Instead of absolute file system paths, the recorded file names
		will begin either a module path@version (when using modules),
		or a plain import path (when using the standard library, or GOPATH).

The feature should be stable, at least since Go1.18, and a handful of our projects are already using it.

I could find only one known issue when using the feature: LDFLAGS are currently not retained, e.g. not displayed in go version -m {SOME_BINARY}. However, this should mostly matter when enabling CGO, which our Makefiles explicitly disable.

EDIT: I stole this trick from here: gohugoio/hugo#12406 (comment)

Checklist

  • Update changelog in CHANGELOG.md.

@lyind lyind added the enhancement New feature or request label Jul 15, 2024
@lyind lyind requested a review from marians July 15, 2024 11:33
@lyind lyind self-assigned this Jul 15, 2024
@lyind lyind requested a review from a team as a code owner July 15, 2024 11:33
@lyind
Copy link
Contributor Author

lyind commented Jul 15, 2024

Successfully tested devctl on devctl via:

$ make
$ ls -al devctl*
-rwxr-xr-x  1 lyind lyind 23375604 Jul 15 13:35 devctl
-rwxr-xr-x  1 lyind lyind 23375604 Jul 15 13:35 devctl-v-linux-amd64
$ ./devctl gen makefile --flavour generic --language go .
$ rm -rf devctl*
$ make
$ ls -al devctl*
-rwxr-xr-x 1 lyind lyind 23305922 Jul 15 13:40 devctl
-rwxr-xr-x 1 lyind lyind 23305922 Jul 15 13:40 devctl-v-linux-amd64
$ ./devctl version
Version:        6.26.5-dev
Git Commit:     11f7544be3e5d90058c6bc253e18ae191f653f3c
Go Version:     go1.22.5
OS / Arch:      linux / amd64
Source:         https://github.com/giantswarm/devctl
$ git checkout -f
$ ./devctl gen makefile --flavour generic --language go .
$ git diff
# matches expected output: devctl seems to work

@lyind
Copy link
Contributor Author

lyind commented Jul 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant