From 2bd1ead32e0c0574fcf19188d00622311f7618d8 Mon Sep 17 00:00:00 2001 From: Murphy Chen Date: Wed, 11 Sep 2024 19:40:48 +0800 Subject: [PATCH] [cmd] Add new make targets with ldflags to build lite version binaries (#34698) **Description:** We can use ldflags `-w` and `-s` to downscale the binary size. The official docs explanation of the two options: ``` -s Omit the symbol table and debug information. -w Omit the DWARF symbol table. ``` After building with the above options, the binary size scales down 28%: ``` -rwxr-xr-x 1 fraps staff 249M Aug 15 14:23 otelcontribcol_darwin_arm64 -rwxr-xr-x 1 fraps staff 347M Aug 8 18:20 otelcontribcol_darwin_arm64-old ``` I think it's worth adding them. The only degradation is we can't use GDB to debug however it rare use case. **Link to tracking Issue:** **Testing:** **Documentation:** --------- Co-authored-by: Curtis Robert --- Makefile | 16 ++++++++++++++++ Makefile.Common | 2 ++ 2 files changed, 18 insertions(+) diff --git a/Makefile b/Makefile index 8c2bfa9ae94c..09d8da8962d8 100644 --- a/Makefile +++ b/Makefile @@ -338,6 +338,12 @@ otelcontribcol: cd ./cmd/otelcontribcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/otelcontribcol_$(GOOS)_$(GOARCH)$(EXTENSION) \ -tags $(GO_BUILD_TAGS) . +# Build the Collector executable without the symbol table, debug information, and the DWARF symbol table. +.PHONY: otelcontribcollite +otelcontribcollite: + cd ./cmd/otelcontribcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/otelcontribcol_$(GOOS)_$(GOARCH)$(EXTENSION) \ + -tags $(GO_BUILD_TAGS) -ldflags $(GO_BUILD_LDFLAGS) . + .PHONY: genoteltestbedcol genoteltestbedcol: $(BUILDER) $(BUILDER) --skip-compilation --config cmd/oteltestbedcol/builder-config.yaml --output-path cmd/oteltestbedcol @@ -349,12 +355,22 @@ oteltestbedcol: cd ./cmd/oteltestbedcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/oteltestbedcol_$(GOOS)_$(GOARCH)$(EXTENSION) \ -tags $(GO_BUILD_TAGS) . +.PHONY: oteltestbedcollite +oteltestbedcollite: + cd ./cmd/oteltestbedcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/oteltestbedcol_$(GOOS)_$(GOARCH)$(EXTENSION) \ + -tags $(GO_BUILD_TAGS) -ldflags $(GO_BUILD_LDFLAGS) . + # Build the telemetrygen executable. .PHONY: telemetrygen telemetrygen: cd ./cmd/telemetrygen && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/telemetrygen_$(GOOS)_$(GOARCH)$(EXTENSION) \ -tags $(GO_BUILD_TAGS) . +.PHONY: telemetrygenlite +telemetrygenlite: + cd ./cmd/telemetrygen && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/telemetrygen_$(GOOS)_$(GOARCH)$(EXTENSION) \ + -tags $(GO_BUILD_TAGS) -ldflags $(GO_BUILD_LDFLAGS) . + # helper function to update the core packages in builder-config.yaml # input parameters are # $(1) = path/to/versions.yaml (where it greps the relevant packages) diff --git a/Makefile.Common b/Makefile.Common index 53584bf57e14..e2eee6f55e72 100644 --- a/Makefile.Common +++ b/Makefile.Common @@ -25,6 +25,8 @@ SRC_PARENT_DIR := $(shell dirname $(SRC_ROOT)) # build tags required by any component should be defined as an independent variables and later added to GO_BUILD_TAGS below GO_BUILD_TAGS="" +# These ldflags allow the build tool to omit the symbol table, debug information, and the DWARF symbol table to downscale binary size. +GO_BUILD_LDFLAGS="-s -w" GOTEST_TIMEOUT?= 600s GOTEST_OPT?= -race -timeout $(GOTEST_TIMEOUT) -parallel 4 --tags=$(GO_BUILD_TAGS) GOTEST_INTEGRATION_OPT?= -race -timeout 360s -parallel 4