Skip to content

Commit 16ec1dd

Browse files
authored
Merge pull request #4044 from norio-nomura/makefile-add-debug-flag
`Makefile`: Add the `DEBUG` flag to build binaries with debug information for use by `dlv exec`.
2 parents da1f0cc + 4b7f04c commit 16ec1dd

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

Makefile

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,38 @@ PACKAGE := github.com/lima-vm/lima/v2
4343
VERSION := $(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)
4444
VERSION_TRIMMED := $(VERSION:v%=%)
4545

46+
# `DEBUG` flag to build binaries with debug information for use by `dlv exec`.
47+
# This implies KEEP_DWARF=1 and KEEP_SYMBOLS=1.
48+
DEBUG ?=
49+
GO_BUILD_GCFLAGS ?=
50+
KEEP_DWARF ?=
4651
KEEP_SYMBOLS ?=
52+
ifeq ($(DEBUG),1)
53+
# Disable optimizations and inlining to make debugging easier.
54+
GO_BUILD_GCFLAGS = -gcflags="all=-N -l"
55+
# Keep the symbol table
56+
KEEP_DWARF = 1
57+
# Enable DWARF generation
58+
KEEP_SYMBOLS = 1
59+
endif
60+
61+
GO_BUILD_LDFLAGS_W := true
62+
ifeq ($(KEEP_DWARF),1)
63+
GO_BUILD_LDFLAGS_W = false
64+
endif
65+
4766
GO_BUILD_LDFLAGS_S := true
4867
ifeq ($(KEEP_SYMBOLS),1)
4968
GO_BUILD_LDFLAGS_S = false
5069
endif
51-
GO_BUILD_LDFLAGS := -ldflags="-s=$(GO_BUILD_LDFLAGS_S) -w -X $(PACKAGE)/pkg/version.Version=$(VERSION)"
70+
# `-s`: Strip the symbol table according to the KEEP_SYMBOLS config
71+
# `-w`: Disable DWARF generation according to the KEEP_DWARF config
72+
# `-X`: Embed version information.
73+
GO_BUILD_LDFLAGS := -ldflags="-s=$(GO_BUILD_LDFLAGS_S) -w=$(GO_BUILD_LDFLAGS_W) -X $(PACKAGE)/pkg/version.Version=$(VERSION)"
5274
# `go -version -m` returns -tags with comma-separated list, because space-separated list is deprecated in go1.13.
5375
# converting to comma-separated list is useful for comparing with the output of `go version -m`.
5476
GO_BUILD_FLAG_TAGS := $(addprefix -tags=,$(shell echo "$(GO_BUILDTAGS)"|tr " " "\n"|paste -sd "," -))
55-
GO_BUILD := $(GO) build $(GO_BUILD_LDFLAGS) $(GO_BUILD_FLAG_TAGS)
77+
GO_BUILD := $(strip $(GO) build $(GO_BUILD_GCFLAGS) $(GO_BUILD_LDFLAGS) $(GO_BUILD_FLAG_TAGS))
5678

5779
################################################################################
5880
# Features
@@ -78,7 +100,9 @@ help-variables:
78100
@echo '# Variables that can be overridden.'
79101
@echo
80102
@echo '- PREFIX (directory) : Installation prefix (default: /usr/local)'
103+
@echo '- KEEP_DWARF (1 or 0) : Whether to keep DWARF information (default: 0)'
81104
@echo '- KEEP_SYMBOLS (1 or 0) : Whether to keep symbols (default: 0)'
105+
@echo '- DEBUG (1 or 0) : Whether to build with debug information (default: 0)'
82106

83107
.PHONY: help-targets
84108
help-targets:
@@ -177,7 +201,7 @@ dependencies_for_cmd = go.mod $(call find_files_excluding_dir_and_test, ./cmd/$(
177201
# $(1): target binary
178202
extract_build_vars = $(shell \
179203
($(GO) version -m $(1) 2>&- || echo $(1):) | \
180-
awk 'FNR==1{print "GOVERSION="$$2}$$2~/^(CGO|GO|-ldflags|-tags).*=.+$$/{sub("^.*"$$2,$$2); print $$0}' \
204+
awk 'FNR==1{print "GOVERSION="$$2}$$2~/^(CGO|GO|-gcflags|-ldflags|-tags).*=.+$$/{sub("^.*"$$2,$$2); print $$0}' \
181205
)
182206

183207
# a list of keys from the GO build variables to be used for calling `go env`.
@@ -192,7 +216,7 @@ go_build_vars = $(shell \
192216
$(ENVS_$(1)) $(GO) env $(2) | \
193217
awk '/ /{print "\""$$0"\""; next}{print}' | \
194218
for k in $(2); do read -r v && echo "$$k=$${v}"; done \
195-
) $(GO_BUILD_LDFLAGS) $(GO_BUILD_FLAG_TAGS)
219+
) $(GO_BUILD_GCFLAGS) $(GO_BUILD_LDFLAGS) $(GO_BUILD_FLAG_TAGS)
196220

197221
# returns the difference between $(1) and $(2).
198222
diff = $(filter-out $(2),$(1))$(filter-out $(1),$(2))

0 commit comments

Comments
 (0)