Skip to content

Commit 95422d3

Browse files
committed
Makefile: add -tags to force build check, and applied it to guest agent.
Signed-off-by: Norio Nomura <[email protected]>
1 parent a954f18 commit 95422d3

File tree

1 file changed

+60
-32
lines changed

1 file changed

+60
-32
lines changed

Makefile

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ 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-
LDFLAGS := -ldflags="-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION)"
47-
GO_BUILD := $(GO) build $(LDFLAGS) -tags "$(GO_BUILDTAGS)"
46+
GO_BUILD_LDFLAGS := -ldflags="-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION)"
47+
# `go -version -m` returns -tags with comma-separated list, because space-separated list is deprecated in go1.13.
48+
# converting to comma-separated list is useful for comparing with the output of `go version -m`.
49+
GO_BUILD_FLAG_TAGS := $(addprefix -tags=,$(shell echo "$(GO_BUILDTAGS)"|tr " " "\n"|paste -sd "," -))
50+
GO_BUILD := $(GO) build $(GO_BUILD_LDFLAGS) $(GO_BUILD_FLAG_TAGS)
4851

4952
.NOTPARALLEL:
5053

@@ -116,51 +119,68 @@ binaries: clean \
116119
.PHONY: limactl lima helpers
117120
limactl: _output/bin/limactl$(exe) codesign lima
118121

122+
### Listing Dependencies
123+
119124
# returns a list of files expanded from $(1) excluding directories.
120125
glob_excluding_dir = $(shell bash -c -O extglob -O globstar -O nullglob 'for f in $(1); do test -d $$f || echo $$f; done')
121126
FILES_IN_PKG = $(call glob_excluding_dir, ./pkg/**/!(*_test.go))
122127

123128
# returns a list of files which are dependencies for the command $(1).
124129
dependencis_for_cmd = go.mod $(call glob_excluding_dir, ./cmd/$(1)/**/!(*_test.go)) $(FILES_IN_PKG)
125130

126-
# returns GOVERSION, CGO*, GO*, and -ldflags build variables from the output of `go version -m $(1)`.
131+
### Force Building Targets
132+
133+
# returns GOVERSION, CGO*, GO*, -ldflags, and -tags build variables from the output of `go version -m $(1)`.
127134
# When CGO_* variables are not set, they are not included in the output.
128135
# Because the CGO_* variables are not set means that those values are default values,
129136
# it can be assumed that those values are same if the GOVERSION is same.
137+
# $(1): target binary
130138
extract_build_vars = $(shell \
131139
($(GO) version -m $(1) 2>&- || echo $(1):) | \
132-
awk 'FNR==1{print "GOVERSION="$$2}$$2~/^(CGO|GO|-ldflags)[^=]*=[^ ]+/{sub("^.*"$$2,$$2); print $$0}' \
140+
awk 'FNR==1{print "GOVERSION="$$2}$$2~/^(CGO|GO|-ldflags|-tags).*=.+$$/{sub("^.*"$$2,$$2); print $$0}' \
133141
)
134142

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)
143+
# a list of keys from the GO build variables to be used for calling `go env`.
144+
# keys starting with '-' are excluded because `go env` does not support those keys.
145+
# $(1): extracted build variables from the binary
146+
keys_in_build_vars = $(filter-out -%,$(shell for i in $(1); do echo $${i%%=*}; done))
140147

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) | \
148+
# a list of GO build variables to build the target binary.
149+
# $(1): target binary. expecting ENVS_$(2) is set to use the environment variables for the target binary.
150+
# $(2): key of the GO build variable to be used for calling `go env`.
151+
go_build_vars = $(shell \
152+
$(ENVS_$(1)) $(GO) env $(2) | \
144153
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-
)
154+
for k in $(2); do read -r v && echo "$$k=$${v}"; done \
155+
) $(GO_BUILD_LDFLAGS) $(GO_BUILD_FLAG_TAGS)
148156

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
157+
# returns the difference between $(1) and $(2).
158+
diff = $(filter-out $(2),$(1))$(filter-out $(1),$(2))
159+
160+
# returns diff between the GO build variables in the binary $(1) and the building variables.
161+
# $(1): target binary
162+
# $(2): extracted GO build variables from the binary
163+
compare_build_vars = $(call diff,$(call go_build_vars,$(1),$(call keys_in_build_vars,$(2))),$(2))
164+
165+
# returns "force" if the GO build variables in the binary $(1) is different from the building variables.
166+
# $(1): target binary. expecting ENVS_$(1) is set to use the environment variables for the target binary.
167+
force_build = $(if $(call compare_build_vars,$(1),$(call extract_build_vars,$(1))),force,)
168+
169+
force: # placeholder for force build
153170

154171
# dependencies for limactl
155172
DEPENDENCIES_FOR_LIMACTL = $(call dependencis_for_cmd,limactl)
156173
ifeq ($(GOOS),darwin)
157174
DEPENDENCIES_FOR_LIMACTL += vz.entitlements
158175
endif
159176

160-
_output/bin/limactl$(exe): $(DEPENDENCIES_FOR_LIMACTL)
177+
# environment variables for limactl. this variable is used for checking force build.
178+
ENVS__output/bin/limactl$(exe) = CGO_ENABLED=1 $(addprefix GOOS=,$(GOOS)) $(addprefix GOARCH=,$(GOARCH))
179+
180+
_output/bin/limactl$(exe): $(DEPENDENCIES_FOR_LIMACTL) $(call force_build,_output/bin/limactl$(exe))
161181
# The hostagent must be compiled with CGO_ENABLED=1 so that net.LookupIP() in the DNS server
162182
# calls the native resolver library and not the simplistic version in the Go library.
163-
CGO_ENABLED=1 $(GO_BUILD) -o $@ ./cmd/limactl
183+
$(ENVS_$@) $(GO_BUILD) -o $@ ./cmd/limactl
164184

165185
LIMA_CMDS = lima lima$(bat)
166186
lima: $(addprefix _output/bin/,$(LIMA_CMDS))
@@ -174,9 +194,11 @@ _output/bin/%: ./cmd/% | _output/bin
174194
MKDIR_TARGETS += _output/bin
175195

176196
# _output/share/lima/lima-guestagent
197+
LINUX_GUESTAGENT_PATH_COMMON = _output/share/lima/lima-guestagent.Linux-
198+
177199
# How to add architecure specific guestagent:
178200
# 1. Add the architecture to GUESTAGENT_ARCHS
179-
# 2. Add GUESTAGENT_ARCH_ENVS_<arch> to set GOARCH and other necessary environment variables
201+
# 2. Add ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)<arch> to set GOOS, GOARCH, and other necessary environment variables
180202
ifeq ($(CONFIG_GUESTAGENT_OS_LINUX),y)
181203
GUESTAGENT_ARCHS = aarch64 armv7l riscv64 x86_64
182204
NATIVE_GUESTAGENT_ARCH = $(shell uname -m | sed -e s/arm64/aarch64/)
@@ -187,26 +209,32 @@ config_guestagent_arch_name = CONFIG_GUESTAGENT_ARCH_$(shell echo $(1)|tr -d _|t
187209

188210
# guestagent_path returns the path to the guestagent binary for the given architecture,
189211
# or an empty string if the CONFIG_GUESTAGENT_ARCH_<arch> is not set.
190-
guestagent_path = $(if $(findstring y,$($(call config_guestagent_arch_name,$(1)))),_output/share/lima/lima-guestagent.Linux-$(1))
212+
guestagent_path = $(if $(findstring y,$($(call config_guestagent_arch_name,$(1)))),$(LINUX_GUESTAGENT_PATH_COMMON)$(1))
191213

192214
# apply CONFIG_GUESTAGENT_ARCH_*
193215
GUESTAGENTS = $(foreach arch,$(GUESTAGENT_ARCHS),$(call guestagent_path,$(arch)))
194-
GUESTAGENT_ARCH_ENVS_aarch64 = GOARCH=arm64
195-
GUESTAGENT_ARCH_ENVS_armv7l = GOARCH=arm GOARM=7
196-
GUESTAGENT_ARCH_ENVS_riscv64 = GOARCH=riscv64
197-
GUESTAGENT_ARCH_ENVS_x86_64 = GOARCH=amd64
198-
NATIVE_GUESTAGENT=_output/share/lima/lima-guestagent.Linux-$(NATIVE_GUESTAGENT_ARCH)
199-
ADDITIONAL_GUESTAGENTS=$(addprefix _output/share/lima/lima-guestagent.Linux-,$(ADDITIONAL_GUESTAGENT_ARCHS))
216+
NATIVE_GUESTAGENT=$(LINUX_GUESTAGENT_PATH_COMMON)$(NATIVE_GUESTAGENT_ARCH)
217+
ADDITIONAL_GUESTAGENTS=$(addprefix $(LINUX_GUESTAGENT_PATH_COMMON),$(ADDITIONAL_GUESTAGENT_ARCHS))
200218
endif
201219

202220
.PHONY: guestagents native-guestagent additional-guestagents
203221
guestagents: $(GUESTAGENTS)
204222
native-guestagent: $(NATIVE_GUESTAGENT)
205223
additional-guestagents: $(ADDITIONAL_GUESTAGENTS)
206224
%-guestagent:
207-
@[ "$(findstring $(*),$(GUESTAGENT_ARCHS))" == "$(*)" ] && make _output/share/lima/lima-guestagent.Linux-$*
208-
_output/share/lima/lima-guestagent.Linux-%: $(call dependencis_for_cmd,lima-guestagent) | _output/share/lima
209-
GOOS=linux $(GUESTAGENT_ARCH_ENVS_$*) CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
225+
@[ "$(findstring $(*),$(GUESTAGENT_ARCHS))" == "$(*)" ] && make $(LINUX_GUESTAGENT_PATH_COMMON)$*
226+
227+
# environment variables for linx-guestagent. these variable are used for checking force build.
228+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)aarch64 = GOOS=linux GOARCH=arm64 CGO_ENABLED=0
229+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)armv7l = GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0
230+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)riscv64 = GOOS=linux GOARCH=riscv64 CGO_ENABLED=0
231+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)x86_64 = GOOS=linux GOARCH=amd64 CGO_ENABLED=0
232+
$(LINUX_GUESTAGENT_PATH_COMMON)aarch64: $(call force_build,$(LINUX_GUESTAGENT_PATH_COMMON)aarch64)
233+
$(LINUX_GUESTAGENT_PATH_COMMON)armv7l: $(call force_build,$(LINUX_GUESTAGENT_PATH_COMMON)armv7l)
234+
$(LINUX_GUESTAGENT_PATH_COMMON)riscv64: $(call force_build,$(LINUX_GUESTAGENT_PATH_COMMON)riscv64)
235+
$(LINUX_GUESTAGENT_PATH_COMMON)x86_64: $(call force_build,$(LINUX_GUESTAGENT_PATH_COMMON)x86_64)
236+
$(LINUX_GUESTAGENT_PATH_COMMON)%: $(call dependencis_for_cmd,lima-guestagent) | _output/share/lima
237+
$(ENVS_$@) $(GO_BUILD) -o $@ ./cmd/lima-guestagent
210238
chmod 644 $@
211239
ifeq ($(CONFIG_GUESTAGENT_COMPRESS),y)
212240
gzip $@

0 commit comments

Comments
 (0)