Skip to content

Reduce binary size with build flags#102

Open
az-smartling wants to merge 1 commit into
masterfrom
reduce_binary_size
Open

Reduce binary size with build flags#102
az-smartling wants to merge 1 commit into
masterfrom
reduce_binary_size

Conversation

@az-smartling
Copy link
Copy Markdown
Contributor

  • Added -trimpath -ldflags="-s -w" and CGO_ENABLED=0 to the cross-platform build rule in the Makefile
  • Binary size drops from ~15 MB to ~10 MB (~33%)

@az-smartling az-smartling requested review from dimitrystd and junky May 15, 2026 11:13
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Makefile

%:
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.$@
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Copy link
Copy Markdown
Contributor

@dimitrystd dimitrystd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants