Skip to content

Commit 5ab6cbf

Browse files
authored
Merge pull request #2638 from norio-nomura/refactor-makefile
Refactor Makefile
2 parents da15262 + d29935f commit 5ab6cbf

File tree

1 file changed

+152
-108
lines changed

1 file changed

+152
-108
lines changed

Makefile

Lines changed: 152 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,42 @@ all: binaries manpages
5454
help:
5555
@echo ' binaries - Build all binaries'
5656
@echo ' manpages - Build manual pages'
57+
@echo
58+
@echo " Use 'make help-targets' to see additional targets."
59+
60+
.PHONY: help-targets
61+
help-targets:
62+
@echo '# Targets can be categorized by their location.'
63+
@echo
64+
@echo 'Targets for files in _output/bin/:'
65+
@echo '- limactl : Build limactl, and lima'
66+
@echo '- lima : Copy lima, and lima.bat'
67+
@echo '- helpers : Copy nerdctl.lima, apptainer.lima, docker.lima, podman.lima, and kubectl.lima'
68+
@echo
69+
@echo 'Targets for files in _output/share/lima/:'
70+
@echo '- guestagents : Build guestagents for archs enabled by CONFIG_GUESTAGENT_ARCHS_*'
71+
@echo '- native-guestagent : Build guestagent for native arch'
72+
@echo '- additional-guestagents : Build guestagents for archs other than native arch'
73+
@echo '- <arch>-guestagent : Build guestagent for <arch>: $(sort $(GUESTAGENT_ARCHS))'
74+
@echo
75+
@echo 'Targets for files in _output/share/lima/templates/:'
76+
@echo '- templates : Copy templates'
77+
@echo '- template_experimentals : Copy experimental templates to experimental/'
78+
@echo '- default_template : Copy default.yaml template'
79+
@echo '- create-examples-link : Create a symlink at ../examples pointing to templates'
80+
@echo
81+
@echo 'Targets for files in _output/share/doc/lima:'
82+
@echo '- documentation : Copy documentation to _output/share/doc/lima'
83+
@echo '- create-links-in-doc-dir : Create some symlinks pointing ../../lima/templates'
84+
@echo
85+
@echo '# e.g. to install limactl, helpers, native guestagent, and templates:'
86+
@echo '# make native install'
5787

5888
exe: _output/bin/limactl$(exe)
5989

60-
.PHONY: minimal
61-
minimal: clean \
62-
_output/bin/limactl$(exe) \
63-
codesign \
64-
_output/share/lima/lima-guestagent.Linux-$(shell uname -m | sed -e s/arm64/aarch64/)
65-
mkdir -p _output/share/lima/templates
66-
cp -aL examples/default.yaml _output/share/lima/templates/
90+
.PHONY: minimal native
91+
minimal: clean limactl native-guestagent default_template
92+
native: clean limactl helpers native-guestagent templates
6793

6894
config: Kconfig
6995
$(KCONFIG_CONF) $<
@@ -79,122 +105,137 @@ menuconfig: Kconfig
79105

80106
-include .config
81107

82-
HELPERS = \
83-
_output/bin/nerdctl.lima \
84-
_output/bin/apptainer.lima \
85-
_output/bin/docker.lima \
86-
_output/bin/podman.lima \
87-
_output/bin/kubectl.lima
108+
.PHONY: binaries
109+
binaries: clean \
110+
limactl helpers guestagents \
111+
templates template_experimentals create-examples-link \
112+
documentation create-links-in-doc-dir
88113

89-
ifeq ($(CONFIG_GUESTAGENT_COMPRESS),y)
90-
gz = .gz
91-
endif
114+
# _output/bin
115+
.PHONY: limactl _output/bin/limactl$(exe) lima helpers
116+
limactl: _output/bin/limactl$(exe) codesign lima
117+
118+
_output/bin/limactl$(exe):
119+
# The hostagent must be compiled with CGO_ENABLED=1 so that net.LookupIP() in the DNS server
120+
# calls the native resolver library and not the simplistic version in the Go library.
121+
CGO_ENABLED=1 $(GO_BUILD) -o $@ ./cmd/limactl
122+
123+
LIMA_CMDS = lima lima$(bat)
124+
lima: $(addprefix _output/bin/,$(LIMA_CMDS))
125+
126+
HELPER_CMDS = nerdctl.lima apptainer.lima docker.lima podman.lima kubectl.lima
127+
helpers: $(addprefix _output/bin/,$(HELPER_CMDS))
128+
129+
_output/bin/%: ./cmd/% | _output/bin
130+
cp -a $< $@
92131

132+
MKDIR_TARGETS += _output/bin
133+
134+
# _output/share/lima/lima-guestagent
135+
# How to add architecure specific guestagent:
136+
# 1. Add the architecture to GUESTAGENT_ARCHS
137+
# 2. Add GUESTAGENT_ARCH_ENVS_<arch> to set GOARCH and other necessary environment variables
93138
ifeq ($(CONFIG_GUESTAGENT_OS_LINUX),y)
94-
ifeq ($(CONFIG_GUESTAGENT_ARCH_X8664),y)
95-
GUESTAGENT += \
96-
_output/share/lima/lima-guestagent.Linux-x86_64$(gz)
97-
endif
98-
ifeq ($(CONFIG_GUESTAGENT_ARCH_AARCH64),y)
99-
GUESTAGENT += \
100-
_output/share/lima/lima-guestagent.Linux-aarch64$(gz)
101-
endif
102-
ifeq ($(CONFIG_GUESTAGENT_ARCH_ARMV7L),y)
103-
GUESTAGENT += \
104-
_output/share/lima/lima-guestagent.Linux-armv7l$(gz)
105-
endif
106-
ifeq ($(CONFIG_GUESTAGENT_ARCH_RISCV64),y)
107-
GUESTAGENT += \
108-
_output/share/lima/lima-guestagent.Linux-riscv64$(gz)
109-
endif
139+
GUESTAGENT_ARCHS = aarch64 armv7l riscv64 x86_64
140+
NATIVE_GUESTAGENT_ARCH = $(shell uname -m | sed -e s/arm64/aarch64/)
141+
ADDITIONAL_GUESTAGENT_ARCHS = $(filter-out $(NATIVE_GUESTAGENT_ARCH),$(GUESTAGENT_ARCHS))
142+
143+
# CONFIG_GUESTAGENT_ARCH_<arch> naming convention: uppercase, remove '_'
144+
config_guestagent_arch_name = CONFIG_GUESTAGENT_ARCH_$(shell echo $(1)|tr -d _|tr a-z A-Z)
145+
146+
# guestagent_path returns the path to the guestagent binary for the given architecture,
147+
# or an empty string if the CONFIG_GUESTAGENT_ARCH_<arch> is not set.
148+
guestagent_path = $(if $(findstring y,$($(call config_guestagent_arch_name,$(1)))),_output/share/lima/lima-guestagent.Linux-$(1))
149+
150+
# apply CONFIG_GUESTAGENT_ARCH_*
151+
GUESTAGENTS = $(foreach arch,$(GUESTAGENT_ARCHS),$(call guestagent_path,$(arch)))
152+
GUESTAGENT_ARCH_ENVS_aarch64 = GOARCH=arm64
153+
GUESTAGENT_ARCH_ENVS_armv7l = GOARCH=arm GOARM=7
154+
GUESTAGENT_ARCH_ENVS_riscv64 = GOARCH=riscv64
155+
GUESTAGENT_ARCH_ENVS_x86_64 = GOARCH=amd64
156+
NATIVE_GUESTAGENT=_output/share/lima/lima-guestagent.Linux-$(NATIVE_GUESTAGENT_ARCH)
157+
ADDITIONAL_GUESTAGENTS=$(addprefix _output/share/lima/lima-guestagent.Linux-,$(ADDITIONAL_GUESTAGENT_ARCHS))
110158
endif
111159

112-
.PHONY: binaries
113-
binaries: clean \
114-
_output/bin/lima \
115-
_output/bin/lima$(bat) \
116-
_output/bin/limactl$(exe) \
117-
codesign \
118-
$(HELPERS) \
119-
$(GUESTAGENT)
120-
cp -aL examples _output/share/lima/templates
121-
ifneq ($(GOOS),windows)
122-
ln -sf templates _output/share/lima/examples
123-
else
124-
cp -aL examples _output/share/lima/examples
125-
endif
126-
mkdir -p _output/share/doc/lima
127-
cp -aL *.md LICENSE _output/share/doc/lima
128-
echo "Moved to https://github.com/lima-vm/.github/blob/main/SECURITY.md" >_output/share/doc/lima/SECURITY.md
129-
ifneq ($(GOOS),windows)
130-
ln -sf ../../lima/templates _output/share/doc/lima/templates
131-
ln -sf templates _output/share/doc/lima/examples
132-
else
133-
cp -aL examples _output/share/doc/lima/examples
134-
cp -aL examples _output/share/doc/lima/templates
160+
.PHONY: guestagents native-guestagent additional-guestagents
161+
guestagents: $(GUESTAGENTS)
162+
native-guestagent: $(NATIVE_GUESTAGENT)
163+
additional-guestagents: $(ADDITIONAL_GUESTAGENTS)
164+
%-guestagent:
165+
@[ "$(findstring $(*),$(GUESTAGENT_ARCHS))" == "$(*)" ] && make _output/share/lima/lima-guestagent.Linux-$*
166+
_output/share/lima/lima-guestagent.Linux-%: | _output/share/lima
167+
GOOS=linux $(GUESTAGENT_ARCH_ENVS_$*) CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
168+
chmod 644 $@
169+
ifeq ($(CONFIG_GUESTAGENT_COMPRESS),y)
170+
gzip $@
135171
endif
136-
echo $(VERSION) > _output/share/doc/lima/VERSION
137172

138-
.PHONY: _output/bin/lima
139-
_output/bin/lima:
140-
mkdir -p _output/bin
141-
cp -a ./cmd/lima $@
173+
MKDIR_TARGETS += _output/share/lima
142174

143-
.PHONY: _output/bin/lima.bat
144-
_output/bin/lima.bat:
145-
mkdir -p _output/bin
146-
cp -a ./cmd/lima.bat $@
175+
# _output/share/lima/templates
176+
TEMPLATES=$(addprefix _output/share/lima/templates/,$(filter-out experimental,$(notdir $(wildcard examples/*))))
177+
TEMPLATE_EXPERIMENTALS=$(addprefix _output/share/lima/templates/experimental/,$(notdir $(wildcard examples/experimental/*)))
147178

148-
.PHONY: _output/bin/nerdctl.lima
149-
_output/bin/nerdctl.lima:
150-
mkdir -p _output/bin
151-
cp -a ./cmd/nerdctl.lima $@
179+
.PHONY: default_template templates template_experimentals
180+
default_template: _output/share/lima/templates/default.yaml
181+
templates: $(TEMPLATES)
182+
template_experimentals: $(TEMPLATE_EXPERIMENTALS)
152183

153-
_output/bin/apptainer.lima: ./cmd/apptainer.lima
154-
@mkdir -p _output/bin
155-
cp -a $^ $@
184+
$(TEMPLATES): | _output/share/lima/templates
185+
$(TEMPLATE_EXPERIMENTALS): | _output/share/lima/templates/experimental
186+
MKDIR_TARGETS += _output/share/lima/templates _output/share/lima/templates/experimental
156187

157-
_output/bin/docker.lima: ./cmd/docker.lima
158-
@mkdir -p _output/bin
159-
cp -a $^ $@
188+
_output/share/lima/templates/%: examples/%
189+
cp -aL $< $@
160190

161-
_output/bin/podman.lima: ./cmd/podman.lima
162-
@mkdir -p _output/bin
163-
cp -a $^ $@
164191

165-
_output/bin/kubectl.lima: ./cmd/kubectl.lima
166-
@mkdir -p _output/bin
167-
cp -a $^ $@
192+
# _output/share/lima/examples
193+
.PHONY: create-examples-link
194+
create-examples-link: _output/share/lima/examples
195+
_output/share/lima/examples: default_template # depends on minimal template target
196+
ifneq ($(GOOS),windows)
197+
ln -sf templates $@
198+
else
199+
# copy from templates builded in build process
200+
cp -aL _output/share/lima/templates $@
201+
endif
168202

169-
.PHONY: _output/bin/limactl$(exe)
170-
_output/bin/limactl$(exe):
171-
# The hostagent must be compiled with CGO_ENABLED=1 so that net.LookupIP() in the DNS server
172-
# calls the native resolver library and not the simplistic version in the Go library.
173-
CGO_ENABLED=1 $(GO_BUILD) -o $@ ./cmd/limactl
203+
# _output/share/doc/lima
204+
DOCUMENTATION=$(addprefix _output/share/doc/lima/,$(wildcard *.md) LICENSE SECURITY.md VERSION)
174205

175-
.PHONY: _output/share/lima/lima-guestagent.Linux-x86_64
176-
_output/share/lima/lima-guestagent.Linux-x86_64:
177-
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
178-
chmod 644 $@
206+
.PHONY: documentation
207+
documentation: $(DOCUMENTATION)
179208

180-
.PHONY: _output/share/lima/lima-guestagent.Linux-aarch64
181-
_output/share/lima/lima-guestagent.Linux-aarch64:
182-
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
183-
chmod 644 $@
209+
_output/share/doc/lima/SECURITY.md: | _output/share/doc/lima
210+
echo "Moved to https://github.com/lima-vm/.github/blob/main/SECURITY.md" > $@
184211

185-
.PHONY: _output/share/lima/lima-guestagent.Linux-armv7l
186-
_output/share/lima/lima-guestagent.Linux-armv7l:
187-
GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
188-
chmod 644 $@
212+
_output/share/doc/lima/VERSION: | _output/share/doc/lima
213+
echo $(VERSION) > $@
189214

190-
.PHONY: _output/share/lima/lima-guestagent.Linux-riscv64
191-
_output/share/lima/lima-guestagent.Linux-riscv64:
192-
GOOS=linux GOARCH=riscv64 CGO_ENABLED=0 $(GO_BUILD) -o $@ ./cmd/lima-guestagent
193-
chmod 644 $@
215+
_output/share/doc/lima/%: % | _output/share/doc/lima
216+
cp -aL $< $@
217+
218+
MKDIR_TARGETS += _output/share/doc/lima
194219

195-
_output/share/lima/lima-guestagent.%.gz: _output/share/lima/lima-guestagent.%
196-
gzip $<
197220

221+
.PHONY: create-links-in-doc-dir
222+
create-links-in-doc-dir: _output/share/doc/lima/templates _output/share/doc/lima/examples
223+
_output/share/doc/lima/templates: default_template # depends on minimal template target
224+
ifneq ($(GOOS),windows)
225+
ln -sf ../../lima/templates $@
226+
else
227+
# copy from templates builded in build process
228+
cp -aL _output/share/lima/templates $@
229+
endif
230+
_output/share/doc/lima/examples: default_template # depends on minimal template target
231+
ifneq ($(GOOS),windows)
232+
ln -sf templates $@
233+
else
234+
# copy from templates builded in build process
235+
cp -aL _output/share/lima/templates $@
236+
endif
237+
238+
# _output/share/man/man1
198239
.PHONY: manpages
199240
manpages: _output/bin/limactl$(exe)
200241
@mkdir -p _output/share/man/man1
@@ -285,16 +326,14 @@ artifact-windows-x86_64: ENVS=GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gc
285326
artifact-windows-x86_64: _artifacts/lima-$(VERSION_TRIMMED)-Windows-x86_64.tar.gz
286327
cd _output && $(ZIP) -r ../_artifacts/lima-$(VERSION_TRIMMED)-Windows-x86_64.zip *
287328

288-
_artifacts/lima-%.tar.gz: mkdir-artifacts
329+
_artifacts/lima-%.tar.gz: | _artifacts
289330
$(ENVS) make clean binaries
290331
$(TAR) -C _output/ -czvf $@ ./
291332

292-
mkdir-artifacts:
293-
mkdir -p _artifacts
333+
MKDIR_TARGETS += _artifacts
294334

295335
.PHONY: artifacts-misc
296-
artifacts-misc:
297-
mkdir -p _artifacts
336+
artifacts-misc: | _artifacts
298337
go mod vendor
299338
$(TAR) -czf _artifacts/lima-$(VERSION_TRIMMED)-go-mod-vendor.tar.gz go.mod go.sum vendor
300339

@@ -303,3 +342,8 @@ codesign:
303342
ifeq ($(GOOS),darwin)
304343
codesign --entitlements vz.entitlements -s - ./_output/bin/limactl
305344
endif
345+
346+
# This target must be placed after any changes to the `MKDIR_TARGETS` variable.
347+
# It seems that variable expansion in Makefile targets is not done recursively.
348+
$(MKDIR_TARGETS):
349+
mkdir -p $@

0 commit comments

Comments
 (0)