Skip to content

Commit

Permalink
Make it easy to build with debugging support
Browse files Browse the repository at this point in the history
This is a bit scrappy, (especially related to the mismatched make
targets when building the debug binaries), but the idea is to make
debugging ec more accessible.
  • Loading branch information
simonbaird committed Feb 13, 2025
1 parent 7ecce20 commit 2e3506c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ help: ## Display this help
generate: ## Code-generate files
go generate ./...

# Set DEBUG_BUILD=1 to build a binary with gdb/dlv debugging support
BUILD_GC_FLAGS=$(if $(DEBUG_BUILD),-gcflags="-N -l",)
BUILD_TRIMPATH=$(if $(DEBUG_BUILD),,-trimpath)
BUILD_LD_FLAGS=$(if $(DEBUG_BUILD),,-s -w)
BUILD_BIN_SUFFIX=$(if $(DEBUG_BUILD),_debug,)

.PHONY: $(ALL_SUPPORTED_OS_ARCH)
$(ALL_SUPPORTED_OS_ARCH): generate ## Build binaries for specific platform/architecture, e.g. make dist/ec_linux_amd64
@GOOS=$(word 2,$(subst _, ,$(notdir $@))); \
GOARCH=$(word 3,$(subst _, ,$(notdir $@))); \
GOOS=$${GOOS} GOARCH=$${GOARCH} CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X github.com/enterprise-contract/ec-cli/internal/version.Version=$(VERSION)" -o dist/ec_$${GOOS}_$${GOARCH}; \
sha256sum -b dist/ec_$${GOOS}_$${GOARCH} > dist/ec_$${GOOS}_$${GOARCH}.sha256
GOOS=$${GOOS} GOARCH=$${GOARCH} CGO_ENABLED=0 go build $(BUILD_TRIMPATH) $(BUILD_GC_FLAGS) -ldflags="$(BUILD_LD_FLAGS) -X github.com/enterprise-contract/ec-cli/internal/version.Version=$(VERSION)" -o dist/ec_$${GOOS}_$${GOARCH}$(BUILD_BIN_SUFFIX); \
sha256sum -b dist/ec_$${GOOS}_$${GOARCH}$(BUILD_BIN_SUFFIX) > dist/ec_$${GOOS}_$${GOARCH}$(BUILD_BIN_SUFFIX).sha256

.PHONY: dist
dist: $(ALL_SUPPORTED_OS_ARCH) ## Build binaries for all supported operating systems and architectures
Expand All @@ -73,12 +79,15 @@ BUILD_LOCAL_PLATFORM:=$(shell go env GOOS)/$(shell go env GOARCH)
BUILD_LOCAL_ARCH:=$(shell go env GOOS)_$(shell go env GOARCH)
.PHONY: build
build: dist/ec_$(BUILD_LOCAL_ARCH) ## Build the ec binary for the current platform
@ln -sf ec_$(BUILD_LOCAL_ARCH) dist/ec
@ln -sf ec_$(BUILD_LOCAL_ARCH)$(BUILD_BIN_SUFFIX) dist/ec$(BUILD_BIN_SUFFIX)

BUILD_IMG_ARCH:=$(shell podman version -f {{.Server.OsArch}} | awk -F/ '{print $$1}')_$(shell podman version -f {{.Server.OsArch}} | awk -F/ '{print $$2}')
.PHONY: build-for-test
build-for-test: dist/ec_$(BUILD_IMG_ARCH)

debug-run:
dlv exec dist/ec_$(BUILD_LOCAL_ARCH)_debug # ...params here as required

.PHONY: clean
clean: ## Delete build output
@rm -f dist/*
Expand Down

0 comments on commit 2e3506c

Please sign in to comment.