@@ -43,16 +43,38 @@ PACKAGE := github.com/lima-vm/lima/v2
43
43
VERSION := $(shell git describe --match 'v[0-9]* ' --dirty='.m' --always --tags)
44
44
VERSION_TRIMMED := $(VERSION:v%=% )
45
45
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 ?=
46
51
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
+
47
66
GO_BUILD_LDFLAGS_S := true
48
67
ifeq ($(KEEP_SYMBOLS ) ,1)
49
68
GO_BUILD_LDFLAGS_S = false
50
69
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 ) "
52
74
# `go -version -m` returns -tags with comma-separated list, because space-separated list is deprecated in go1.13.
53
75
# converting to comma-separated list is useful for comparing with the output of `go version -m`.
54
76
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 ) )
56
78
57
79
# ###############################################################################
58
80
# Features
@@ -78,7 +100,9 @@ help-variables:
78
100
@echo ' # Variables that can be overridden.'
79
101
@echo
80
102
@echo ' - PREFIX (directory) : Installation prefix (default: /usr/local)'
103
+ @echo ' - KEEP_DWARF (1 or 0) : Whether to keep DWARF information (default: 0)'
81
104
@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)'
82
106
83
107
.PHONY : help-targets
84
108
help-targets :
@@ -177,7 +201,7 @@ dependencies_for_cmd = go.mod $(call find_files_excluding_dir_and_test, ./cmd/$(
177
201
# $(1): target binary
178
202
extract_build_vars = $(shell \
179
203
($(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}' \
181
205
)
182
206
183
207
# a list of keys from the GO build variables to be used for calling `go env`.
@@ -192,7 +216,7 @@ go_build_vars = $(shell \
192
216
$(ENVS_$(1 ) ) $(GO ) env $(2 ) | \
193
217
awk '/ /{print "\""$$0"\""; next}{print}' | \
194
218
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 )
196
220
197
221
# returns the difference between $(1) and $(2).
198
222
diff = $(filter-out $(2 ) ,$(1 ) )$(filter-out $(1 ) ,$(2 ) )
0 commit comments