Skip to content

Commit 7137d62

Browse files
committed
Makefile: rewrite artifact* targets to move archive creation to the called make subprocess.
Signed-off-by: Norio Nomura <[email protected]>
1 parent ee37ba8 commit 7137d62

File tree

1 file changed

+90
-26
lines changed

1 file changed

+90
-26
lines changed

Makefile

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ PLANTUML ?= plantuml # may also be "java -jar plantuml.jar" if installed elsewhe
1212
KCONFIG_CONF ?= $(shell command -v kconfig-conf || command -v kbuild-conf || echo oldconfig)
1313
KCONFIG_MCONF ?= $(shell command -v kconfig-mconf || command -v kbuild-mconf || echo menuconfig)
1414

15+
GOARCH ?= $(shell $(GO) env GOARCH)
16+
GOHOSTARCH := $(shell $(GO) env GOHOSTARCH)
17+
GOHOSTOS := $(shell $(GO) env GOHOSTOS)
1518
GOOS ?= $(shell $(GO) env GOOS)
1619
ifeq ($(GOOS),windows)
1720
bat = .bat
@@ -90,6 +93,27 @@ help-targets:
9093
@echo '# e.g. to install limactl, helpers, native guestagent, and templates:'
9194
@echo '# make native install'
9295

96+
.PHONY: help-artifact
97+
help-artifact:
98+
@echo '# Targets for building artifacts to _artifacts/'
99+
@echo
100+
@echo 'Targets to building multiple archs artifacts for GOOS:'
101+
@echo '- artifacts : Build artifacts for current OS and supported archs'
102+
@echo '- artifacts-<GOOS> : Build artifacts for supported archs and <GOOS>: darwin, linux, or windows'
103+
@echo
104+
@echo 'Targets to building GOOS and ARCH (GOARCH, or uname -m) specific artifacts:'
105+
@echo '- artifact : Build artifacts for current GOOS and GOARCH'
106+
@echo '- artifact-<GOOS> : Build artifacts for current GOARCH and <GOOS>: darwin, linux, or windows'
107+
@echo '- artifact-<ARCH> : Build artifacts for current GOOS with <ARCH>: amd64, arm64, x86_64, or aarch64'
108+
@echo '- artifact-<GOOS>-<ARCH> : Build artifacts for <GOOS> and <ARCH>'
109+
@echo
110+
@echo '# GOOS and GOARCH can be specified with make parameters or environment variables.'
111+
@echo '# e.g. to build artifact for linux and arm64:'
112+
@echo '# make GOOS=linux GOARCH=arm64 artifact'
113+
@echo
114+
@echo 'Targets for miscellaneous artifacts:'
115+
@echo '- artifacts-misc : Build artifacts for go.mod, go.sum, and vendor'
116+
93117
exe: _output/bin/limactl$(exe)
94118

95119
.PHONY: minimal native
@@ -176,7 +200,7 @@ DEPENDENCIES_FOR_LIMACTL += vz.entitlements
176200
endif
177201

178202
# environment variables for limactl. this variable is used for checking force build.
179-
ENVS__output/bin/limactl$(exe) = CGO_ENABLED=1 $(addprefix GOOS=,$(GOOS)) $(addprefix GOARCH=,$(GOARCH))
203+
ENVS__output/bin/limactl$(exe) = CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) CC=$(CC)
180204

181205
_output/bin/limactl$(exe): $(DEPENDENCIES_FOR_LIMACTL) $$(call force_build,$$@)
182206
# The hostagent must be compiled with CGO_ENABLED=1 so that net.LookupIP() in the DNS server
@@ -376,44 +400,84 @@ install-tools:
376400
generate:
377401
go generate ./...
378402

379-
.PHONY: artifacts-darwin
380-
artifacts-darwin: artifact-darwin-x86_64 artifact-darwin-arm64
381-
artifact-darwin-arm64: ENVS=GOOS=darwin GOARCH=arm64
382-
artifact-darwin-x86_64: ENVS=GOOS=darwin GOARCH=amd64
383-
384-
.PHONY: artifacts-linux
385-
artifacts-linux: artifact-linux-x86_64 artifact-linux-aarch64
386-
artifact-linux-aarch64: ENVS=GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc
387-
artifact-linux-x86_64: ENVS=GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc
388-
389-
.PHONY: artifacts-windows
390-
artifacts-windows: artifact-windows-x86_64
391-
artifact-windows-x86_64: ENVS=GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc
392-
artifact-windows-%: _artifacts/lima-$(VERSION_TRIMMED)-Windows-$$*.tar.gz _artifacts/lima-$(VERSION_TRIMMED)-Windows-$$*.zip
393-
@true # do nothing
403+
################################################################################
404+
# _artifacts/lima-$(VERSION_TRIMMED)-$(ARTIFACT_OS)-$(ARTIFACT_UNAME_M)
405+
.PHONY: artifact
394406

395407
# returns the capitalized string of $(1).
396408
capitalize = $(shell bash -c 'word="$(1)"; echo $${word^}')
397-
artifact-%: _artifacts/lima-$(VERSION_TRIMMED)-$$(call capitalize,$$*).tar.gz
398-
@true # do nothing
399409

400-
# avoid removing the artifacts on completion of the targets.
401-
.PRECIOUS: _artifacts/lima-%.tar.gz _artifacts/lima-%.zip
402-
_artifacts/lima-%.tar.gz: | _artifacts
403-
$(ENVS) make clean binaries
404-
$(TAR) -C _output/ -czvf $@ ./
410+
# returns the architecture name converted from GOARCH to GNU coreutils uname -m.
411+
to_uname_m = $(foreach arch,$(1),$(shell echo $(arch) | sed 's/amd64/x86_64/' | sed 's/arm64/aarch64/'))
405412

406-
_artifacts/lima-%.zip: | _artifacts
407-
$(ENVS) make clean binaries
413+
ARTIFACT_FILE_EXTENSIONS := .tar.gz
414+
415+
ifeq ($(GOOS),darwin)
416+
# returns the architecture name converted from GOARCH to macOS's uname -m.
417+
to_uname_m = $(foreach arch,$(1),$(shell echo $(arch) | sed 's/amd64/x86_64/'))
418+
else ifeq ($(GOOS),linux)
419+
# CC is required for cross-compiling on Linux.
420+
CC = $(call to_uname_m,$(GOARCH))-linux-gnu-gcc
421+
else ifeq ($(GOOS),windows)
422+
# artifact in zip format also provided for Windows.
423+
ARTIFACT_FILE_EXTENSIONS += .zip
424+
endif
425+
426+
# artifacts: artifacts-$(GOOS)
427+
ARTIFACT_OS = $(call capitalize,$(GOOS))
428+
ARTIFACT_UNAME_M = $(call to_uname_m,$(GOARCH))
429+
ARTIFACT_PATH_COMMON = _artifacts/lima-$(VERSION_TRIMMED)-$(ARTIFACT_OS)-$(ARTIFACT_UNAME_M)
430+
431+
artifact: $(addprefix $(ARTIFACT_PATH_COMMON),$(ARTIFACT_FILE_EXTENSIONS))
432+
433+
# file targets
434+
$(ARTIFACT_PATH_COMMON).tar.gz: binaries | _artifacts
435+
$(TAR) -C _output/ --no-xattrs -czvf $@ ./
436+
437+
$(ARTIFACT_PATH_COMMON).zip: binaries | _artifacts
408438
cd _output && $(ZIP) -r ../$@ *
409439

410-
MKDIR_TARGETS += _artifacts
440+
# generate manpages using native limactl.
441+
manpages-using-native-limactl: GOOS = $(GOHOSTOS)
442+
manpages-using-native-limactl: GOARCH = $(GOHOSTARCH)
443+
manpages-using-native-limactl: manpages
444+
445+
# returns "manpages-using-native-limactl" if $(1) is not equal to $(GOHOSTOS).
446+
# $(1): GOOS
447+
generate_manpages_if_needed = $(if $(filter $(if $(1),$(1),$(GOOS)),$(GOHOSTOS)),,manpages-using-native-limactl)
448+
449+
# build native arch first, then build other archs.
450+
artifact_goarchs = arm64 amd64
451+
goarchs_native_and_others = $(GOHOSTARCH) $(filter-out $(GOHOSTARCH),$(artifact_goarchs))
452+
453+
# artifacts is artifact bundles for each OS.
454+
# if target GOOS is native, build native arch first, generate manpages, then build other archs.
455+
# if target GOOS is not native, build native limactl, generate manpages, then build the target GOOS with archs.
456+
.PHONY: artifacts artifacts-darwin artifacts-linux artifacts-windows
457+
artifacts: $$(addprefix artifact-$$(GOOS)-,$$(goarchs_native_and_others))
458+
artifacts-darwin: $$(call generate_manpages_if_needed,darwin) $$(addprefix artifact-darwin-,$$(goarchs_native_and_others))
459+
artifacts-linux: $$(call generate_manpages_if_needed,linux) $$(addprefix artifact-linux-,$$(goarchs_native_and_others))
460+
artifacts-windows: $$(call generate_manpages_if_needed,windows) $$(addprefix artifact-windows-,$$(goarchs_native_and_others))
461+
462+
# set variables for artifact variant targets.
463+
artifact-darwin% artifact-darwin: GOOS = darwin
464+
artifact-linux% artifact-linux: GOOS = linux
465+
artifact-windows% artifact-windows: GOOS = windows
466+
artifact-%-amd64 artifact-%-x86_64 artifact-amd64 artifact-x86_64: GOARCH = amd64
467+
artifact-%-arm64 artifact-%-aarch64 artifact-arm64 artifact-aarch64: GOARCH = arm64
468+
469+
# build cross arch binaries.
470+
artifact-%: $$(call generate_manpages_if_needed)
471+
make artifact GOOS=$(GOOS) GOARCH=$(GOARCH)
411472

412473
.PHONY: artifacts-misc
413474
artifacts-misc: | _artifacts
414475
go mod vendor
415476
$(TAR) -czf _artifacts/lima-$(VERSION_TRIMMED)-go-mod-vendor.tar.gz go.mod go.sum vendor
416477

478+
MKDIR_TARGETS += _artifacts
479+
480+
################################################################################
417481
.PHONY: codesign
418482
codesign: _output/bin/limactl
419483
ifeq ($(GOOS),darwin)

0 commit comments

Comments
 (0)