xref: /qemu/tests/docker/Makefile.include (revision 727385c4)
1# Makefile for Docker tests
2
3.PHONY: docker docker-help docker-test docker-clean docker-image docker-qemu-src
4
5NULL :=
6SPACE := $(NULL) #
7COMMA := ,
8
9HOST_ARCH = $(if $(ARCH),$(ARCH),$(shell uname -m))
10
11DOCKER_SUFFIX := .docker
12DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
13# we don't run tests on intermediate images (used as base by another image)
14DOCKER_PARTIAL_IMAGES := debian10 debian11
15# we don't directly build virtual images (they are used to build other images)
16DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain empty
17DOCKER_IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
18DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
19# Use a global constant ccache directory to speed up repetitive builds
20DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
21ifeq ($(HOST_ARCH),x86_64)
22DOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu
23endif
24DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY))
25
26DOCKER_TESTS := $(notdir $(shell \
27	find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
28
29ENGINE := auto
30
31DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
32
33TESTS ?= %
34IMAGES ?= %
35
36CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
37DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
38
39.DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
40$(DOCKER_SRC_COPY):
41	@mkdir $@
42	$(if $(SRC_ARCHIVE), \
43		$(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
44			"CP", "$@/qemu.tar"), \
45		$(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
46			"GEN", "$@/qemu.tar"))
47	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
48		"COPY","RUNNER")
49
50docker-qemu-src: $(DOCKER_SRC_COPY)
51
52docker-image: ${DOCKER_TARGETS}
53
54# General rule for building docker images. If we are a sub-make
55# invoked with SKIP_DOCKER_BUILD we still check the image is up to date
56# though
57ifdef SKIP_DOCKER_BUILD
58docker-image-%: $(DOCKER_FILES_DIR)/%.docker
59	$(call quiet-command, \
60		$(DOCKER_SCRIPT) check --quiet qemu/$* $<, \
61		"CHECK", "$*")
62else
63docker-image-%: $(DOCKER_FILES_DIR)/%.docker
64	$(call quiet-command,\
65		$(DOCKER_SCRIPT) build -t qemu/$* -f $< \
66		$(if $V,,--quiet) \
67		$(if $(NOCACHE),--no-cache, \
68			$(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
69		$(if $(NOUSER),,--add-current-user) \
70		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
71		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
72		"BUILD","$*")
73
74# Special rule for debootstraped binfmt linux-user images
75docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
76	$(if $(EXECUTABLE),,\
77		$(error EXECUTABLE not set, debootstrap of debian-$* would fail))
78	$(if $(DEB_ARCH),,\
79		$(error DEB_ARCH not set, debootstrap of debian-$* would fail))
80	$(if $(DEB_TYPE),,\
81		$(error DEB_TYPE not set, debootstrap of debian-$* would fail))
82	$(if $(wildcard $(EXECUTABLE)),						\
83		$(call quiet-command,						\
84			DEB_ARCH=$(DEB_ARCH)					\
85			DEB_TYPE=$(DEB_TYPE) 					\
86			$(if $(DEB_URL),DEB_URL=$(DEB_URL),)			\
87			$(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< 		\
88			$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) 		\
89			$(if $(NOUSER),,--add-current-user) 			\
90			$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))	\
91			$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
92			"BUILD","binfmt debian-$* (debootstrapped)"),		\
93		$(call quiet-command,						\
94			$(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || 	\
95			{ echo "You will need to build $(EXECUTABLE)"; exit 1;},\
96			"CHECK", "debian-$* exists"))
97
98# These are test targets
99USER_TCG_TARGETS=$(patsubst %-linux-user,qemu-%,$(filter %-linux-user,$(TARGET_DIRS)))
100EXEC_COPY_TESTS=$(patsubst %,docker-exec-copy-test-%, $(USER_TCG_TARGETS))
101
102$(EXEC_COPY_TESTS): docker-exec-copy-test-%: $(DOCKER_FILES_DIR)/empty.docker
103	$(call quiet-command,							\
104		$(DOCKER_SCRIPT) build -t qemu/exec-copy-test-$* -f $< 		\
105			$(if $V,,--quiet) --no-cache 				\
106			--include-executable=$*					\
107			--skip-binfmt,						\
108			"TEST","copy $* to container")
109	$(call quiet-command,							\
110		$(DOCKER_SCRIPT) run qemu/exec-copy-test-$* 			\
111			/$* -version > tests/docker-exec-copy-test-$*.out,	\
112			"TEST","check $* works in container")
113
114docker-exec-copy-test: $(EXEC_COPY_TESTS)
115
116endif
117
118# Enforce dependencies for composite images
119ifeq ($(HOST_ARCH),x86_64)
120docker-image-debian-amd64: docker-image-debian10
121DOCKER_PARTIAL_IMAGES += debian-amd64-cross
122else
123docker-image-debian-amd64-cross: docker-image-debian10
124DOCKER_PARTIAL_IMAGES += debian-amd64
125endif
126
127# For non-x86 hosts not all cross-compilers have been packaged
128ifneq ($(HOST_ARCH),x86_64)
129DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
130DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
131DOCKER_PARTIAL_IMAGES += debian-s390x-cross
132DOCKER_PARTIAL_IMAGES += fedora
133endif
134
135docker-image-debian-alpha-cross: docker-image-debian10
136docker-image-debian-arm64-cross: docker-image-debian10
137docker-image-debian-armel-cross: docker-image-debian10
138docker-image-debian-armhf-cross: docker-image-debian10
139docker-image-debian-hppa-cross: docker-image-debian10
140docker-image-debian-m68k-cross: docker-image-debian10
141docker-image-debian-mips-cross: docker-image-debian10
142docker-image-debian-mips64-cross: docker-image-debian10
143docker-image-debian-mips64el-cross: docker-image-debian10
144docker-image-debian-mipsel-cross: docker-image-debian10
145docker-image-debian-ppc64el-cross: docker-image-debian10
146docker-image-debian-s390x-cross: docker-image-debian10
147docker-image-debian-sh4-cross: docker-image-debian10
148docker-image-debian-sparc64-cross: docker-image-debian10
149
150# The native build should never use the registry
151docker-image-debian-native: DOCKER_REGISTRY=
152
153
154#
155# The build rule for hexagon-cross is special in so far for most of
156# the time we don't want to build it. While dockers caching does avoid
157# this most of the time sometimes we want to force the issue.
158#
159docker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker
160	$(if $(NOCACHE), 								\
161		$(call quiet-command,							\
162			$(DOCKER_SCRIPT) build -t qemu/debian-hexagon-cross -f $< 	\
163			$(if $V,,--quiet) --no-cache 					\
164			--registry $(DOCKER_REGISTRY) --extra-files			\
165			$(DOCKER_FILES_DIR)/debian-hexagon-cross.docker.d/build-toolchain.sh, \
166			"BUILD", "debian-hexagon-cross"),				\
167		$(call quiet-command,							\
168			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)			\
169				qemu/debian-hexagon-cross $(DOCKER_REGISTRY),		\
170			"FETCH", "debian-hexagon-cross")				\
171		$(call quiet-command,							\
172			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 			\
173				qemu/debian-hexagon-cross --add-current-user,		\
174			"PREPARE", "debian-hexagon-cross"))
175
176debian-toolchain-run = \
177	$(if $(NOCACHE), 						\
178		$(call quiet-command,					\
179			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
180			$(if $V,,--quiet) --no-cache 			\
181			--registry $(DOCKER_REGISTRY) --extra-files	\
182			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
183			"BUILD", $1),				        \
184		$(call quiet-command,					\
185			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
186				qemu/$1 $(DOCKER_REGISTRY),		\
187			"FETCH", $1)					\
188		$(call quiet-command,					\
189			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
190				qemu/$1 				\
191				$(if $(NOUSER),,--add-current-user) 	\
192			"PREPARE", $1))
193debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
194
195docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
196    $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
197	$(call debian-toolchain, $@)
198
199docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
200    $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
201	$(call debian-toolchain, $@)
202
203# Specialist build images, sometimes very limited tools
204docker-image-debian-tricore-cross: docker-image-debian10
205docker-image-debian-all-test-cross: docker-image-debian10
206docker-image-debian-arm64-test-cross: docker-image-debian11
207docker-image-debian-microblaze-cross: docker-image-debian10
208docker-image-debian-nios2-cross: docker-image-debian10
209docker-image-debian-powerpc-test-cross: docker-image-debian11
210
211# These images may be good enough for building tests but not for test builds
212DOCKER_PARTIAL_IMAGES += debian-alpha-cross
213DOCKER_PARTIAL_IMAGES += debian-arm64-test-cross
214DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
215DOCKER_PARTIAL_IMAGES += debian-hppa-cross
216DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
217DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
218DOCKER_PARTIAL_IMAGES += debian-nios2-cross
219DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
220DOCKER_PARTIAL_IMAGES += debian-tricore-cross
221DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
222DOCKER_PARTIAL_IMAGES += fedora-cris-cross
223
224# Rules for building linux-user powered images
225#
226# These are slower than using native cross compiler setups but can
227# work around issues with poorly working multi-arch systems and broken
228# packages.
229
230# Expand all the pre-requistes for each docker image and test combination
231$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES) $(DOCKER_VIRTUAL_IMAGES),$(DOCKER_IMAGES)), \
232	$(foreach t,$(DOCKER_TESTS), \
233		$(eval .PHONY: docker-$t@$i) \
234		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
235	) \
236	$(foreach t,$(DOCKER_TESTS), \
237		$(eval docker-all-tests: docker-$t@$i) \
238		$(eval docker-$t: docker-$t@$i) \
239	) \
240)
241
242docker:
243	@echo 'Build QEMU and run tests inside Docker or Podman containers'
244	@echo
245	@echo 'Available targets:'
246	@echo
247	@echo '    docker:              Print this help.'
248	@echo '    docker-all-tests:    Run all image/test combinations.'
249	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
250	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
251	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
252	@echo '                         Note: "TEST" is one of the listed test name,'
253	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
254	@echo '                         "IMAGE" is one of the listed container name.'
255	@echo '    docker-image:        Build all images.'
256	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
257	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
258	@echo
259	@echo 'Available container images:'
260	@echo '    $(DOCKER_IMAGES)'
261ifneq ($(DOCKER_USER_IMAGES),)
262	@echo
263	@echo 'Available linux-user images (docker-binfmt-image-debian-%):'
264	@echo '    $(DOCKER_USER_IMAGES)'
265endif
266	@echo
267	@echo 'Available tests:'
268	@echo '    $(DOCKER_TESTS)'
269	@echo
270	@echo 'Special variables:'
271	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
272	@echo '    EXTRA_CONFIGURE_OPTS="..."'
273	@echo '                         Extra configure options.'
274	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
275	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
276	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
277	@echo '                         (default is 1)'
278	@echo '    DEBUG=1              Stop and drop to shell in the created container'
279	@echo '                         before running the command.'
280	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
281	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
282	@echo '    NOUSER=1             Define to disable adding current user to containers passwd.'
283	@echo '    NOCACHE=1            Ignore cache when build images.'
284	@echo '    EXECUTABLE=<path>    Include executable in image.'
285	@echo '    EXTRA_FILES="<path> [... <path>]"'
286	@echo '                         Include extra files in image.'
287	@echo '    ENGINE=auto/docker/podman'
288	@echo '                         Specify which container engine to run.'
289	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
290
291docker-help: docker
292
293# This rule if for directly running against an arbitrary docker target.
294# It is called by the expanded docker targets (e.g. make
295# docker-test-foo@bar) which will do additional verification.
296#
297# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
298#
299docker-run: docker-qemu-src
300	@mkdir -p "$(DOCKER_CCACHE_DIR)"
301	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
302		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
303	fi
304	$(if $(EXECUTABLE),						\
305		$(call quiet-command,					\
306			$(DOCKER_SCRIPT) update 			\
307			$(IMAGE) --executable $(EXECUTABLE),		\
308			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
309	$(call quiet-command,						\
310		$(DOCKER_SCRIPT) run 					\
311			$(if $(NOUSER),,--run-as-current-user) 		\
312			--security-opt seccomp=unconfined		\
313			$(if $(DEBUG),-ti,)				\
314			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
315			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
316			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
317			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
318			-e SHOW_ENV=$(SHOW_ENV) 			\
319			$(if $(NOUSER),,				\
320				-e CCACHE_DIR=/var/tmp/ccache 		\
321				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
322			)						\
323			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
324			$(IMAGE) 					\
325			/var/tmp/qemu/run 				\
326			$(TEST), "  RUN $(TEST) in ${IMAGE}")
327	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
328		"  CLEANUP $(DOCKER_SRC_COPY)")
329
330# Run targets:
331#
332# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
333docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
334docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
335docker-run-%:
336	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu/$(IMAGE)
337
338docker-clean:
339	$(call quiet-command, $(DOCKER_SCRIPT) clean)
340