Skip to content

Commit 8f406cb

Browse files
authored
fix: go build with trimmed path prefix (#249)
go build with trimmed path prefix
1 parent 370b066 commit 8f406cb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ go.work.sum
2020

2121
coverage*.txt
2222
gotest*.log
23+
dist

Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Use bash for all shell commands (required for pipefail and other bash features)
55
SHELL := /bin/bash
66

7-
.PHONY: all test test-unit test-integration test-e2e format lint build build/pkg install package clean \
7+
.PHONY: all test test-unit test-integration test-e2e format lint build build-all build/pkg install package clean \
88
build-demo build-demo-grpc build-demo-http format/go format/yaml lint/go lint/yaml \
99
lint/action lint/makefile lint/license-header lint/license-header/fix lint/dockerfile actionlint yamlfmt gotestfmt ratchet ratchet/pin \
1010
ratchet/update ratchet/check golangci-lint embedmd checkmake hadolint help docs check-embed \
@@ -15,6 +15,7 @@ SHELL := /bin/bash
1515

1616
# Constant variables
1717
BINARY_NAME := otel
18+
PLATFORMS := darwin/amd64 linux/amd64 windows/amd64 darwin/arm64 linux/arm64
1819
TOOL_DIR := tool/cmd
1920
INST_PKG_GZIP = otel-pkg.gz
2021
INST_PKG_TMP = pkg_temp
@@ -26,6 +27,8 @@ GOOS ?= $(shell go env GOOS)
2627
VERSION := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
2728
COMMIT_HASH := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
2829
BUILD_TIME := $(shell date -u '+%Y-%m-%d')
30+
LDFLAGS := -X main.Version=$(VERSION) -X main.CommitHash=$(COMMIT_HASH) -X main.BuildTime=$(BUILD_TIME)
31+
GO_BUILD_CMD := go build -trimpath -a -ldflags "$(LDFLAGS)"
2932
EXT :=
3033
ifeq ($(GOOS),windows)
3134
EXT = .exe
@@ -60,9 +63,24 @@ build: build/pkg package ## Build the instrumentation tool
6063
@echo "Building instrumentation tool..."
6164
@cp $(API_SYNC_SOURCE) $(API_SYNC_TARGET)
6265
@go mod tidy
63-
@go build -a -ldflags "-X main.Version=$(VERSION) -X main.CommitHash=$(COMMIT_HASH) -X main.BuildTime=$(BUILD_TIME)" -o $(BINARY_NAME)$(EXT) ./$(TOOL_DIR)
66+
@$(GO_BUILD_CMD) -o $(BINARY_NAME)$(EXT) ./$(TOOL_DIR)
6467
@./$(BINARY_NAME)$(EXT) version
6568

69+
build-all: build/pkg package ## Build the instrumentation tool for all platforms
70+
@echo "Building instrumentation tool for all platforms..."
71+
@cp $(API_SYNC_SOURCE) $(API_SYNC_TARGET)
72+
@go mod tidy
73+
@mkdir -p dist
74+
@for platform in $(PLATFORMS); do \
75+
GOOS=$${platform%/*}; \
76+
GOARCH=$${platform#*/}; \
77+
echo "Building for $$GOOS/$$GOARCH..."; \
78+
EXT=""; \
79+
if [ "$$GOOS" = "windows" ]; then EXT=".exe"; fi; \
80+
env GOOS=$$GOOS GOARCH=$$GOARCH $(GO_BUILD_CMD) -o dist/$(BINARY_NAME)-$$GOOS-$$GOARCH$$EXT ./$(TOOL_DIR); \
81+
done
82+
@echo "All builds completed. Artifacts in dist/"
83+
6684
install: package ## Install otel to $$GOPATH/bin (auto-packages instrumentation)
6785
@echo "Installing otel..."
6886
@cp $(API_SYNC_SOURCE) $(API_SYNC_TARGET)
@@ -339,6 +357,7 @@ test-e2e/coverage: build gotestfmt
339357

340358
clean: ## Clean build artifacts
341359
@echo "Cleaning build artifacts..."
360+
rm -rf dist
342361
rm -f $(BINARY_NAME)$(EXT)
343362
rm -f demo/basic/basic
344363
rm -f demo/grpc/server/server

0 commit comments

Comments
 (0)