From 78ee13c54fd9cc8d0c900030ccf093ccc7073603 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Fri, 12 Aug 2022 21:43:46 -0700 Subject: [PATCH] Pass DBG=1 to `make` to build for debug --- Makefile | 4 ++++ build/build.sh | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 1d7aa172d..38255115f 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,9 @@ VERSION ?= $(shell git describe --tags --always --dirty) # This version-strategy uses a manual value to set the version string #VERSION ?= 1.2.3 +# Set this to 1 to build a debugger-friendly binary. +DBG ?= + # These are passed to docker when building and testing. HTTP_PROXY ?= HTTPS_PROXY ?= @@ -129,6 +132,7 @@ $(OUTBIN): .go/$(OUTBIN).stamp ARCH=$(ARCH) \ OS=$(OS) \ VERSION=$(VERSION) \ + BUILD_DEBUG=$(DBG) \ ./build/build.sh \ " if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \ diff --git a/build/build.sh b/build/build.sh index 664a17b2b..0aec5c8fc 100755 --- a/build/build.sh +++ b/build/build.sh @@ -34,9 +34,23 @@ fi export CGO_ENABLED=0 export GOARCH="${ARCH}" export GOOS="${OS}" -export GOFLAGS="-mod=vendor" +if [[ "${BUILD_DEBUG:-}" == 1 ]]; then + # Debugging - disable optimizations and inlining + gogcflags="all=-N -l" + goasmflags="" + goldflags="" +else + # Not debugging - trim paths, disable symbols and DWARF. + goasmflags="all=-trimpath=$(pwd)" + gogcflags="all=-trimpath=$(pwd)" + goldflags="-s -w" +fi + +always_ldflags="-X $(go list -m)/pkg/version.VERSION=${VERSION}" go install \ -installsuffix "static" \ - -ldflags "-X $(go list -m)/pkg/version.VERSION=${VERSION}" \ + -gcflags="${gogcflags}" \ + -asmflags="${goasmflags}" \ + -ldflags="${always_ldflags} ${goldflags}" \ ./...