Skip to content

Commit

Permalink
Strip CLI and plugin binaries to reduce their size
Browse files Browse the repository at this point in the history
Overall binary reduction achieved by this commit:
- 28% decrease for Darwin
- 36% decrease for Linux and Windows

This commit does two optimizations:

1. Remove symbol tables (reduce about 30% for Linux/Windows, 22% for Mac)
--
When compiling the CLI and plugins, this commit adds the "-s" and "-w"
flags.  The "-s" flag omits the symbol table and debug information
while "-w" omits the DWARF symbol table.  These symbol tables are used
by debuggers.  The impact in removing them will be that it will not be
possible to use a debugger on a production binary; this is acceptable
and it is actually a standard approach to ship production binaries
without debug symbols.  If a debugger needs to be used, a binary can
built with the symbol tables.

Note that the removal of debug symbols does not prevent proper traces
from being shown in a go "panic".

To build a CLI with debug symbols use TANZU_CLI_ENABLE_DEBUG=1
To build plugins with debug symbols use PLUGIN_ENABLE_DEBUG=1

2. Disable function inlining (reduce about 6% on every OS)
--
Adding the flag "-gcflags=all=-l" removes function inlining.  This
normally reduces performance a little but saves space.  Considering we
are dealing with a CLI tool which does not perform CPU intensive
operations, the slight possible performance degradation is not of
concern.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam committed Nov 16, 2023
1 parent 60a0903 commit 133f611
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-cli/pkg/buildinfo.Date=$(BUILD_DAT
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-cli/pkg/buildinfo.SHA=$(BUILD_SHA)'
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-cli/pkg/buildinfo.Version=$(BUILD_VERSION)'

# Remove debug symbols to reduce binary size
# To build with debug symbols: TANZU_CLI_ENABLE_DEBUG=1
ifeq ($(strip $(TANZU_CLI_ENABLE_DEBUG)),)
LD_FLAGS += -w -s
endif

APT_IMAGE=ubuntu
ifdef APT_BUILDER_IMAGE
APT_IMAGE=$(APT_BUILDER_IMAGE)
Expand Down Expand Up @@ -133,9 +139,9 @@ build-cli-%: ##Build the Tanzu Core CLI for a platform
fi

@if [ "$(OS)" = "windows" ]; then \
GOOS=$(OS) GOARCH=$(ARCH) $(GO) build --ldflags "$(LD_FLAGS)" -o "$(ARTIFACTS_DIR)/$(OS)/$(ARCH)/cli/core/$(BUILD_VERSION)/tanzu-cli-$(OS)_$(ARCH).exe" ./cmd/tanzu/main.go;\
GOOS=$(OS) GOARCH=$(ARCH) $(GO) build -gcflags=all="-l" --ldflags "$(LD_FLAGS)" -o "$(ARTIFACTS_DIR)/$(OS)/$(ARCH)/cli/core/$(BUILD_VERSION)/tanzu-cli-$(OS)_$(ARCH).exe" ./cmd/tanzu/main.go;\
else \
GOOS=$(OS) GOARCH=$(ARCH) $(GO) build --ldflags "$(LD_FLAGS)" -o "$(ARTIFACTS_DIR)/$(OS)/$(ARCH)/cli/core/$(BUILD_VERSION)/tanzu-cli-$(OS)_$(ARCH)" ./cmd/tanzu/main.go;\
GOOS=$(OS) GOARCH=$(ARCH) $(GO) build -gcflags=all="-l" --ldflags "$(LD_FLAGS)" -o "$(ARTIFACTS_DIR)/$(OS)/$(ARCH)/cli/core/$(BUILD_VERSION)/tanzu-cli-$(OS)_$(ARCH)" ./cmd/tanzu/main.go;\
fi

## --------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ endif
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Date=$(PLUGIN_BUILD_DATE)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.SHA=$(PLUGIN_BUILD_SHA)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Version=$(PLUGIN_BUILD_VERSION)'
PLUGIN_GO_FLAGS ?=

# Remove debug symbols to reduce binary size
# To build with debug symbols: PLUGIN_ENABLE_DEBUG=1
ifeq ($(strip $(PLUGIN_ENABLE_DEBUG)),)
PLUGIN_LD_FLAGS += -w -s
endif

# The below disables function inlining to reduce binary size
PLUGIN_GO_FLAGS += -gcflags=all=-l

# Add supported OS-ARCHITECTURE combinations here
PLUGIN_BUILD_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64 darwin-arm64 linux-arm64
Expand Down
11 changes: 11 additions & 0 deletions docs/dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ To run e2e tests for the repository:
make e2e-cli-core
```

### Debugging

By default the CLI is built without debug symbols to reduce its binary size;
this has the side-effect of preventing the use of a debugger towards the built
binary. However, when using an IDE, a different binary is used, one built by
the IDE, and therefore the debugger can be used directly from the IDE.

If you require using a debugger directly on a CLI binary, you have to build
a binary that includes the debug symbols. You can build such a binary by using
`TANZU_CLI_ENABLE_DEBUG=1` along with your build command.

## Centralized Discovery of Plugins

The Tanzu CLI uses a system of plugins to provide functionality to interact
Expand Down
11 changes: 11 additions & 0 deletions docs/plugindev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ well.

Edit the file as appropriate.

### Debugging

By default a plugin is built without debug symbols to reduce its binary size;
this has the side-effect of preventing the use of a debugger towards the built
binary. However, when using an IDE, a different binary is used, one built by
the IDE, and therefore the debugger can be used directly from the IDE.

If you require using a debugger directly on a plugin binary, you have to build
a binary that includes the debug symbols. You can build such a binary by using
`PLUGIN_ENABLE_DEBUG=1` along with your build command.

### Publishing a plugin

To publish one or more built plugins to a target repository, one would need to
Expand Down
10 changes: 9 additions & 1 deletion plugin-tooling.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ endif
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Date=$(PLUGIN_BUILD_DATE)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.SHA=$(PLUGIN_BUILD_SHA)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Version=$(PLUGIN_BUILD_VERSION)'
PLUGIN_GO_FLAGS ?=

# Remove debug symbols to reduce binary size
# To build with debug symbols: PLUGIN_ENABLE_DEBUG=1
ifeq ($(strip $(PLUGIN_ENABLE_DEBUG)),)
PLUGIN_LD_FLAGS += -w -s
endif

# The below disables function inlining to reduce binary size
PLUGIN_GO_FLAGS += -gcflags=all=-l

# Add supported OS-ARCHITECTURE combinations here
PLUGIN_BUILD_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64 darwin-arm64 linux-arm64
Expand Down
13 changes: 11 additions & 2 deletions test/sample-plugin/plugin-tooling.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ endif
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Date=$(PLUGIN_BUILD_DATE)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.SHA=$(PLUGIN_BUILD_SHA)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Version=$(PLUGIN_BUILD_VERSION)'
PLUGIN_GO_FLAGS ?=

# Remove debug symbols to reduce binary size
# To build with debug symbols: PLUGIN_ENABLE_DEBUG=1
ifeq ($(strip $(PLUGIN_ENABLE_DEBUG)),)
PLUGIN_LD_FLAGS += -w -s
endif

# The below disables function inlining to reduce binary size
PLUGIN_GO_FLAGS += -gcflags=all=-l

# Add supported OS-ARCHITECTURE combinations here
PLUGIN_BUILD_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64 darwin-arm64 linux-arm64
Expand Down Expand Up @@ -88,8 +96,9 @@ plugin-install-local:
.PHONY: plugin-build
plugin-build: $(PLUGIN_BUILD_TARGETS) generate-plugin-bundle ## Build all plugin binaries for all supported os-arch

.PHONY: plugin-build-local
plugin-build-local: plugin-build-$(GOHOSTOS)-$(GOHOSTARCH) ## Build all plugin binaries for local platform

plugin-build-%:
$(eval ARCH = $(word 2,$(subst -, ,$*)))
$(eval OS = $(word 1,$(subst -, ,$*)))
Expand Down

0 comments on commit 133f611

Please sign in to comment.