Skip to content

Commit 2ea780e

Browse files
authored
Merge pull request #2687 from norio-nomura/Improve-compressed-guestagent-rebuilds
Makefile: improve compressed guestagent rebuilds
2 parents cae9bf2 + 5db5c54 commit 2ea780e

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

Makefile

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ compare_build_vars = $(call diff,$(call go_build_vars,$(1),$(call keys_in_build_
201201
# $(1): target binary. expecting ENVS_$(1) is set to use the environment variables for the target binary.
202202
force_build = $(if $(call compare_build_vars,$(1),$(call extract_build_vars,$(1))),force,)
203203

204+
# returns the file name without .gz extension. It also gunzips the file with .gz extension if exists.
205+
# $(1): target file
206+
gunzip_if_exists = $(shell f=$(1); f=$${f%.gz}; test -f "$${f}.gz" && (set -x; gunzip -f "$${f}.gz") ; echo "$${f}")
207+
208+
# call force_build with passing output of gunzip_if_exists as an argument.
209+
# $(1): target file
210+
force_build_with_gunzip = $(call force_build,$(call gunzip_if_exists,$(1)))
211+
204212
force: # placeholder for force build
205213

206214
################################################################################
@@ -246,47 +254,60 @@ MKDIR_TARGETS += _output/bin
246254
# _output/share/lima/lima-guestagent
247255
LINUX_GUESTAGENT_PATH_COMMON = _output/share/lima/lima-guestagent.Linux-
248256

257+
# How to add architecure specific guestagent:
258+
# 1. Add the architecture to GUESTAGENT_ARCHS
259+
# 2. Add ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)<arch> to set GOOS, GOARCH, and other necessary environment variables
249260
GUESTAGENT_ARCHS = aarch64 armv7l riscv64 x86_64
261+
262+
ALL_GUESTAGENTS_NOT_COMPRESSED = $(addprefix $(LINUX_GUESTAGENT_PATH_COMMON),$(GUESTAGENT_ARCHS))
263+
ifeq ($(CONFIG_GUESTAGENT_COMPRESS),y)
264+
$(info Guestagents are unzipped each time to check the build configuration; they may be gunzipped afterward.)
265+
gz=.gz
266+
endif
267+
268+
ALL_GUESTAGENTS = $(addsuffix $(gz),$(ALL_GUESTAGENTS_NOT_COMPRESSED))
269+
270+
# guestagent path for the given architectures. it may has .gz extension if CONFIG_GUESTAGENT_COMPRESS is enabled.
271+
# $(1): list of architectures
272+
guestaget_path = $(foreach arch,$(1),$(LINUX_GUESTAGENT_PATH_COMMON)$(arch)$(gz))
273+
250274
NATIVE_GUESTAGENT_ARCH = $(shell uname -m | sed -e s/arm64/aarch64/)
251-
NATIVE_GUESTAGENT = $(LINUX_GUESTAGENT_PATH_COMMON)$(NATIVE_GUESTAGENT_ARCH)
275+
NATIVE_GUESTAGENT = $(call guestaget_path,$(NATIVE_GUESTAGENT_ARCH))
252276
ADDITIONAL_GUESTAGENT_ARCHS = $(filter-out $(NATIVE_GUESTAGENT_ARCH),$(GUESTAGENT_ARCHS))
253-
ADDITIONAL_GUESTAGENTS = $(addprefix $(LINUX_GUESTAGENT_PATH_COMMON),$(ADDITIONAL_GUESTAGENT_ARCHS))
277+
ADDITIONAL_GUESTAGENTS = $(call guestaget_path,$(ADDITIONAL_GUESTAGENT_ARCHS))
254278

255-
# How to add architecure specific guestagent:
256-
# 1. Add the architecture to GUESTAGENT_ARCHS
257-
# 2. Add ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)<arch> to set GOOS, GOARCH, and other necessary environment variables
258279
ifeq ($(CONFIG_GUESTAGENT_OS_LINUX),y)
259280

281+
# config_guestagent_arch returns expanded value of CONFIG_GUESTAGENT_ARCH_<arch>
282+
# $(1): architecture
260283
# CONFIG_GUESTAGENT_ARCH_<arch> naming convention: uppercase, remove '_'
261-
config_guestagent_arch_name = CONFIG_GUESTAGENT_ARCH_$(shell echo $(1)|tr -d _|tr a-z A-Z)
284+
config_guestagent_arch = $(filter y,$(CONFIG_GUESTAGENT_ARCH_$(shell echo $(1)|tr -d _|tr a-z A-Z)))
262285

263-
# guestagent_path returns the path to the guestagent binary for the given architecture,
286+
# guestagent_path_enabled_by_config returns the path to the guestagent binary for the given architecture,
264287
# or an empty string if the CONFIG_GUESTAGENT_ARCH_<arch> is not set.
265-
guestagent_path = $(if $(findstring y,$($(call config_guestagent_arch_name,$(1)))),$(LINUX_GUESTAGENT_PATH_COMMON)$(1))
288+
guestagent_path_enabled_by_config = $(if $(call config_guestagent_arch,$(1)),$(call guestaget_path,$(1)))
266289

267290
# apply CONFIG_GUESTAGENT_ARCH_*
268-
GUESTAGENTS = $(foreach arch,$(GUESTAGENT_ARCHS),$(call guestagent_path,$(arch)))
291+
GUESTAGENTS = $(foreach arch,$(GUESTAGENT_ARCHS),$(call guestagent_path_enabled_by_config,$(arch)))
269292
endif
270293

271-
272294
.PHONY: guestagents native-guestagent additional-guestagents
273295
guestagents: $(GUESTAGENTS)
274296
native-guestagent: $(NATIVE_GUESTAGENT)
275297
additional-guestagents: $(ADDITIONAL_GUESTAGENTS)
276298
%-guestagent:
277-
@[ "$(findstring $(*),$(GUESTAGENT_ARCHS))" == "$(*)" ] && make $(LINUX_GUESTAGENT_PATH_COMMON)$*
299+
@[ "$(findstring $(*),$(GUESTAGENT_ARCHS))" == "$(*)" ] && make $(call guestaget_path,$*)
278300

279301
# environment variables for linx-guestagent. these variable are used for checking force build.
280-
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)aarch64 = GOOS=linux GOARCH=arm64 CGO_ENABLED=0
281-
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)armv7l = GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0
282-
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)riscv64 = GOOS=linux GOARCH=riscv64 CGO_ENABLED=0
283-
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)x86_64 = GOOS=linux GOARCH=amd64 CGO_ENABLED=0
284-
$(LINUX_GUESTAGENT_PATH_COMMON)%: $(call dependencis_for_cmd,lima-guestagent) $$(call force_build,$$@) | _output/share/lima
302+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)aarch64 = CGO_ENABLED=0 GOOS=linux GOARCH=arm64
303+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)armv7l = CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7
304+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)riscv64 = CGO_ENABLED=0 GOOS=linux GOARCH=riscv64
305+
ENVS_$(LINUX_GUESTAGENT_PATH_COMMON)x86_64 = CGO_ENABLED=0 GOOS=linux GOARCH=amd64
306+
$(ALL_GUESTAGENTS_NOT_COMPRESSED): $(call dependencis_for_cmd,lima-guestagent) $$(call force_build_with_gunzip,$$@) | _output/share/lima
285307
$(ENVS_$@) $(GO_BUILD) -o $@ ./cmd/lima-guestagent
286308
chmod 644 $@
287-
ifeq ($(CONFIG_GUESTAGENT_COMPRESS),y)
288-
gzip -f $@
289-
endif
309+
$(LINUX_GUESTAGENT_PATH_COMMON)%.gz: $(LINUX_GUESTAGENT_PATH_COMMON)% $$(call force_build_with_gunzip,$$@)
310+
@set -x; gzip $<
290311

291312
MKDIR_TARGETS += _output/share/lima
292313

@@ -491,7 +512,7 @@ ARTIFACT_PATH_COMMON = _artifacts/lima-$(VERSION_TRIMMED)-$(ARTIFACT_OS)-$(ARTIF
491512
artifact: $(addprefix $(ARTIFACT_PATH_COMMON),$(ARTIFACT_FILE_EXTENSIONS))
492513

493514
ARTIFACT_DES = _output/bin/limactl$(exe) $(LIMA_DEPS) $(HELPERS_DEPS) \
494-
$(NATIVE_GUESTAGENT) $(ADDITIONAL_GUESTAGENTS) \
515+
$(ALL_GUESTAGENTS) \
495516
$(TEMPLATES) $(TEMPLATE_EXPERIMENTALS) _output/share/lima/examples \
496517
$(DOCUMENTATION) _output/share/doc/lima/templates _output/share/doc/lima/examples \
497518
_output/share/man/man1/limactl.1

0 commit comments

Comments
 (0)