Skip to content

Commit a954f18

Browse files
committed
Makefile: force build limactl if the build variables in the limactl binary is different from the current GO build variables.
The variables being checked from `go version -m _output/bin/limactl`: - `GOVERSION` - `CGO*` - `GO*` - `-ldflags` are compared to output of `CGO_ENABLED=1 go env <variables>` and `$(LDFLAGS)`. If it differs, `limactl` will be force build. This is required to using `make artifact*`. Signed-off-by: Norio Nomura <[email protected]>
1 parent c6631b5 commit a954f18

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Makefile

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

46-
GO_BUILD := $(GO) build -ldflags="-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION)" -tags "$(GO_BUILDTAGS)"
46+
LDFLAGS := -ldflags="-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION)"
47+
GO_BUILD := $(GO) build $(LDFLAGS) -tags "$(GO_BUILDTAGS)"
4748

4849
.NOTPARALLEL:
4950

@@ -122,6 +123,34 @@ FILES_IN_PKG = $(call glob_excluding_dir, ./pkg/**/!(*_test.go))
122123
# returns a list of files which are dependencies for the command $(1).
123124
dependencis_for_cmd = go.mod $(call glob_excluding_dir, ./cmd/$(1)/**/!(*_test.go)) $(FILES_IN_PKG)
124125

126+
# returns GOVERSION, CGO*, GO*, and -ldflags build variables from the output of `go version -m $(1)`.
127+
# When CGO_* variables are not set, they are not included in the output.
128+
# Because the CGO_* variables are not set means that those values are default values,
129+
# it can be assumed that those values are same if the GOVERSION is same.
130+
extract_build_vars = $(shell \
131+
($(GO) version -m $(1) 2>&- || echo $(1):) | \
132+
awk 'FNR==1{print "GOVERSION="$$2}$$2~/^(CGO|GO|-ldflags)[^=]*=[^ ]+/{sub("^.*"$$2,$$2); print $$0}' \
133+
)
134+
135+
# a list of build variables that the limactl binary was built with.
136+
limactl_build_vars = $(call extract_build_vars,_output/bin/limactl$(exe))
137+
138+
# a list of keys of build variables that are used in limactl_build_vars.
139+
keys_in_limactl_build_vars = $(shell for i in $(limactl_build_vars); do echo $${i%%=*}; done)
140+
141+
# a list of build variables that current Go build uses.
142+
current_go_build_vars = $(shell \
143+
CGO_ENABLED=1 $(GO) env $(keys_in_limactl_build_vars) | \
144+
awk '/ /{print "\""$$0"\""; next}{print}' | \
145+
for k in $(keys_in_limactl_build_vars); do read -r v && echo "$$k=$${v}"; done | \
146+
sed 's$$-ldflags=$$$(LDFLAGS)$$' \
147+
)
148+
149+
# force build limactl if the build variables in the limactl binary is different from the current GO build variables.
150+
ifneq ($(filter-out $(current_go_build_vars),$(limactl_build_vars)),)
151+
.PHONY: _output/bin/limactl$(exe)
152+
endif
153+
125154
# dependencies for limactl
126155
DEPENDENCIES_FOR_LIMACTL = $(call dependencis_for_cmd,limactl)
127156
ifeq ($(GOOS),darwin)

0 commit comments

Comments
 (0)