1 # Makefile for Docker tests
3 .PHONY: docker docker-help docker-test docker-clean docker-image docker-qemu-src
9 HOST_ARCH = $(if $(ARCH),$(ARCH),$(shell uname -m))
11 # These variables can be set by the user to limit the set of docker
12 # images and tests to a more restricted subset
16 DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
17 # we don't run tests on intermediate images (used as base by another image)
18 DOCKER_PARTIAL_IMAGES := debian10 debian11
19 # we don't directly build virtual images (they are used to build other images)
20 DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
21 __IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
22 DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
23 DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
24 # Use a global constant ccache directory to speed up repetitive builds
25 DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
26 ifeq ($(HOST_ARCH),x86_64)
27 DOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu
29 DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY))
31 __TESTS := $(notdir $(shell \
32 find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
33 DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
37 DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
39 CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
40 DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
42 .DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
45 $(if $(SRC_ARCHIVE), \
46 $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
47 "CP", "$@/qemu.tar"), \
48 $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
49 "GEN", "$@/qemu.tar"))
50 $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
53 docker-qemu-src: $(DOCKER_SRC_COPY)
55 docker-image: ${DOCKER_TARGETS}
57 # General rule for building docker images. If we are a sub-make
58 # invoked with SKIP_DOCKER_BUILD we still check the image is up to date
60 ifdef SKIP_DOCKER_BUILD
61 docker-image-%: $(DOCKER_FILES_DIR)/%.docker
62 $(call quiet-command, \
63 $(DOCKER_SCRIPT) check --quiet qemu/$* $<, \
66 docker-image-%: $(DOCKER_FILES_DIR)/%.docker
67 $(call quiet-command,\
68 $(DOCKER_SCRIPT) build -t qemu/$* -f $< \
70 $(if $(NOCACHE),--no-cache, \
71 $(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
72 $(if $(NOUSER),,--add-current-user) \
73 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
74 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
77 # Special rule for debootstraped binfmt linux-user images
78 docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
80 $(error EXECUTABLE not set, debootstrap of debian-$* would fail))
82 $(error DEB_ARCH not set, debootstrap of debian-$* would fail))
84 $(error DEB_TYPE not set, debootstrap of debian-$* would fail))
85 $(if $(wildcard $(EXECUTABLE)), \
86 $(call quiet-command, \
87 DEB_ARCH=$(DEB_ARCH) \
88 DEB_TYPE=$(DEB_TYPE) \
89 $(if $(DEB_URL),DEB_URL=$(DEB_URL),) \
90 $(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< \
91 $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \
92 $(if $(NOUSER),,--add-current-user) \
93 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES)) \
94 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
95 "BUILD","binfmt debian-$* (debootstrapped)"), \
96 $(call quiet-command, \
97 $(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || \
98 { echo "You will need to build $(EXECUTABLE)"; exit 1;},\
99 "CHECK", "debian-$* exists"))
103 # Enforce dependencies for composite images
104 ifeq ($(HOST_ARCH),x86_64)
105 docker-image-debian-amd64: docker-image-debian10
106 DOCKER_PARTIAL_IMAGES += debian-amd64-cross
108 docker-image-debian-amd64-cross: docker-image-debian10
109 DOCKER_PARTIAL_IMAGES += debian-amd64
112 # For non-x86 hosts not all cross-compilers have been packaged
113 ifneq ($(HOST_ARCH),x86_64)
114 DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
115 DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
116 DOCKER_PARTIAL_IMAGES += debian-s390x-cross
117 DOCKER_PARTIAL_IMAGES += fedora
120 docker-image-debian-alpha-cross: docker-image-debian10
121 docker-image-debian-armel-cross: docker-image-debian10
122 docker-image-debian-armhf-cross: docker-image-debian10
123 docker-image-debian-hppa-cross: docker-image-debian10
124 docker-image-debian-m68k-cross: docker-image-debian10
125 docker-image-debian-mips-cross: docker-image-debian10
126 docker-image-debian-mips64-cross: docker-image-debian10
127 docker-image-debian-mips64el-cross: docker-image-debian10
128 docker-image-debian-mipsel-cross: docker-image-debian10
129 docker-image-debian-ppc64el-cross: docker-image-debian10
130 docker-image-debian-sh4-cross: docker-image-debian10
131 docker-image-debian-sparc64-cross: docker-image-debian10
133 # The native build should never use the registry
134 docker-image-debian-native: DOCKER_REGISTRY=
136 # base images should not add a local user
137 docker-image-debian10: NOUSER=1
138 docker-image-debian11: NOUSER=1
140 # alpine has no adduser
141 docker-image-alpine: NOUSER=1
144 # The build rule for hexagon-cross is special in so far for most of
145 # the time we don't want to build it. While dockers caching does avoid
146 # this most of the time sometimes we want to force the issue.
148 docker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker
150 $(call quiet-command, \
151 $(DOCKER_SCRIPT) build -t qemu/debian-hexagon-cross -f $< \
152 $(if $V,,--quiet) --no-cache \
153 --registry $(DOCKER_REGISTRY) --extra-files \
154 $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker.d/build-toolchain.sh, \
155 "BUILD", "debian-hexagon-cross"), \
156 $(call quiet-command, \
157 $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \
158 qemu/debian-hexagon-cross $(DOCKER_REGISTRY), \
159 "FETCH", "debian-hexagon-cross") \
160 $(call quiet-command, \
161 $(DOCKER_SCRIPT) update $(if $V,,--quiet) \
162 qemu/debian-hexagon-cross --add-current-user, \
163 "PREPARE", "debian-hexagon-cross"))
165 debian-toolchain-run = \
167 $(call quiet-command, \
168 $(DOCKER_SCRIPT) build -t qemu/$1 -f $< \
169 $(if $V,,--quiet) --no-cache \
170 --registry $(DOCKER_REGISTRY) --extra-files \
171 $(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh, \
173 $(call quiet-command, \
174 $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \
175 qemu/$1 $(DOCKER_REGISTRY), \
177 $(call quiet-command, \
178 $(DOCKER_SCRIPT) update $(if $V,,--quiet) \
180 $(if $(NOUSER),,--add-current-user) \
182 debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
184 docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
185 $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
186 $(call debian-toolchain, $@)
188 docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
189 $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
190 $(call debian-toolchain, $@)
192 # Specialist build images, sometimes very limited tools
193 docker-image-debian-tricore-cross: docker-image-debian10
194 docker-image-debian-all-test-cross: docker-image-debian10
195 docker-image-debian-microblaze-cross: docker-image-debian10
196 docker-image-debian-nios2-cross: docker-image-debian10
197 docker-image-debian-powerpc-test-cross: docker-image-debian11
198 docker-image-debian-riscv64-test-cross: docker-image-debian11
200 # These images may be good enough for building tests but not for test builds
201 DOCKER_PARTIAL_IMAGES += debian-alpha-cross
202 DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
203 DOCKER_PARTIAL_IMAGES += debian-hppa-cross
204 DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
205 DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
206 DOCKER_PARTIAL_IMAGES += debian-nios2-cross
207 DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
208 DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
209 DOCKER_PARTIAL_IMAGES += debian-tricore-cross
210 DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
211 DOCKER_PARTIAL_IMAGES += fedora-cris-cross
213 # Expand all the pre-requistes for each docker image and test combination
214 $(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES) $(DOCKER_VIRTUAL_IMAGES),$(DOCKER_IMAGES)), \
215 $(foreach t,$(DOCKER_TESTS), \
216 $(eval .PHONY: docker-$t@$i) \
217 $(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
219 $(foreach t,$(DOCKER_TESTS), \
220 $(eval docker-all-tests: docker-$t@$i) \
221 $(eval docker-$t: docker-$t@$i) \
226 @echo 'Build QEMU and run tests inside Docker or Podman containers'
228 @echo 'Available targets:'
230 @echo ' docker: Print this help.'
231 @echo ' docker-all-tests: Run all image/test combinations.'
232 @echo ' docker-TEST: Run "TEST" on all image combinations.'
233 @echo ' docker-clean: Kill and remove residual docker testing containers.'
234 @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".'
235 @echo ' Note: "TEST" is one of the listed test name,'
236 @echo ' or a script name under $$QEMU_SRC/tests/docker/;'
237 @echo ' "IMAGE" is one of the listed container name.'
238 @echo ' docker-image: Build all images.'
239 @echo ' docker-image-IMAGE: Build image "IMAGE".'
240 @echo ' docker-run: For manually running a "TEST" with "IMAGE".'
242 @echo 'Available container images:'
243 @echo ' $(DOCKER_IMAGES)'
245 @echo 'Available tests:'
246 @echo ' $(DOCKER_TESTS)'
248 @echo 'Special variables:'
249 @echo ' TARGET_LIST=a,b,c Override target list in builds.'
250 @echo ' EXTRA_CONFIGURE_OPTS="..."'
251 @echo ' Extra configure options.'
252 @echo ' IMAGES="a b c ..": Restrict available images to subset.'
253 @echo ' TESTS="x y z .." Restrict available tests to subset.'
254 @echo ' J=[0..9]* Overrides the -jN parameter for make commands'
255 @echo ' (default is 1)'
256 @echo ' DEBUG=1 Stop and drop to shell in the created container'
257 @echo ' before running the command.'
258 @echo ' NETWORK=1 Enable virtual network interface with default backend.'
259 @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.'
260 @echo ' NOUSER=1 Define to disable adding current user to containers passwd.'
261 @echo ' NOCACHE=1 Ignore cache when build images.'
262 @echo ' EXECUTABLE=<path> Include executable in image.'
263 @echo ' EXTRA_FILES="<path> [... <path>]"'
264 @echo ' Include extra files in image.'
265 @echo ' ENGINE=auto/docker/podman'
266 @echo ' Specify which container engine to run.'
267 @echo ' REGISTRY=url Cache builds from registry (default:$(DOCKER_REGISTRY))'
271 # This rule if for directly running against an arbitrary docker target.
272 # It is called by the expanded docker targets (e.g. make
273 # docker-test-foo@bar) which will do additional verification.
275 # For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
277 docker-run: docker-qemu-src
278 @mkdir -p "$(DOCKER_CCACHE_DIR)"
279 @if test -z "$(IMAGE)" || test -z "$(TEST)"; \
280 then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
282 $(if $(EXECUTABLE), \
283 $(call quiet-command, \
284 $(DOCKER_SCRIPT) update \
285 $(IMAGE) --executable $(EXECUTABLE), \
286 " COPYING $(EXECUTABLE) to $(IMAGE)"))
287 $(call quiet-command, \
288 $(DOCKER_SCRIPT) run \
289 $(if $(NOUSER),,--run-as-current-user) \
290 --security-opt seccomp=unconfined \
291 $(if $(DEBUG),-ti,) \
292 $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
293 -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \
294 -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
295 -e V=$V -e J=$J -e DEBUG=$(DEBUG) \
296 -e SHOW_ENV=$(SHOW_ENV) \
298 -e CCACHE_DIR=/var/tmp/ccache \
299 -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
301 -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
304 $(TEST), " RUN $(TEST) in ${IMAGE}")
305 $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
306 " CLEANUP $(DOCKER_SRC_COPY)")
310 # Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
311 docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
312 docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
314 @$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu/$(IMAGE)
317 $(call quiet-command, $(DOCKER_SCRIPT) clean)