Reduce binary size with build flags#102
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the Go build command in the Makefile to include CGO_ENABLED=0, -trimpath, and -ldflags="-s -w" for optimized and reproducible builds. Feedback suggests extracting these hardcoded flags into variables to improve maintainability and allow for easier local debugging, as well as injecting version information into the binary using linker flags.
|
|
||
| %: | ||
| GOOS=$(basename $@) go build -mod=mod -o bin/smartling.$@ | ||
| CGO_ENABLED=0 GOOS=$(basename $@) go build -mod=mod -trimpath -ldflags="-s -w" -o bin/smartling.$@ |
There was a problem hiding this comment.
Hardcoding build flags like -trimpath and -ldflags="-s -w" directly in the pattern rule reduces the maintainability of the Makefile. It is recommended to extract these into variables (e.g., LDFLAGS and GO_BUILD_FLAGS) at the top of the file. This would allow developers to easily override them for local development (e.g., to keep symbols for debugging) and provides a central place to manage build configuration.
Additionally, since a VERSION variable is already being calculated in the _pkg-init target (lines 65-66), moving its definition to the top level and injecting it into the binary via -X in ldflags would be a significant improvement for this CLI tool, ensuring the version information is baked into the binary during the build process.
dimitrystd
left a comment
There was a problem hiding this comment.
I was lazy to read docs about these flags. I hope we will get developer-friendly stack traces from the clients (in case of error).
-trimpath -ldflags="-s -w"andCGO_ENABLED=0to the cross-platform build rule in the Makefile