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 DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
12 ifeq ($(HOST_ARCH),x86_64)
13 DOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu
15 DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY))
18 DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
20 CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
21 DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
23 .DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
26 $(if $(SRC_ARCHIVE), \
27 $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
28 "CP", "$@/qemu.tar"), \
29 $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
30 "GEN", "$@/qemu.tar"))
31 $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
34 docker-qemu-src: $(DOCKER_SRC_COPY)
36 # General rule for building docker images.
37 docker-image-%: $(DOCKER_FILES_DIR)/%.docker
38 $(call quiet-command,\
39 $(DOCKER_SCRIPT) build -t qemu/$* -f $< \
41 $(if $(NOCACHE),--no-cache, \
42 $(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
43 $(if $(NOUSER),,--add-current-user) \
44 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
45 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
48 # Special rule for debootstraped binfmt linux-user images
49 docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
51 $(error EXECUTABLE not set, debootstrap of debian-$* would fail))
53 $(error DEB_ARCH not set, debootstrap of debian-$* would fail))
55 $(error DEB_TYPE not set, debootstrap of debian-$* would fail))
56 $(if $(wildcard $(EXECUTABLE)), \
57 $(call quiet-command, \
58 DEB_ARCH=$(DEB_ARCH) \
59 DEB_TYPE=$(DEB_TYPE) \
60 $(if $(DEB_URL),DEB_URL=$(DEB_URL),) \
61 $(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< \
62 $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \
63 $(if $(NOUSER),,--add-current-user) \
64 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES)) \
65 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
66 "BUILD","binfmt debian-$* (debootstrapped)"), \
67 $(call quiet-command, \
68 $(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || \
69 { echo "You will need to build $(EXECUTABLE)"; exit 1;},\
70 "CHECK", "debian-$* exists"))
72 # Enforce dependencies for composite images
73 # we don't run tests on intermediate images (used as base by another image)
74 DOCKER_PARTIAL_IMAGES := debian10 debian11
75 ifeq ($(HOST_ARCH),x86_64)
76 docker-image-debian-amd64: docker-image-debian10
77 DOCKER_PARTIAL_IMAGES += debian-amd64-cross
79 docker-image-debian-amd64-cross: docker-image-debian10
80 DOCKER_PARTIAL_IMAGES += debian-amd64
83 # For non-x86 hosts not all cross-compilers have been packaged
84 ifneq ($(HOST_ARCH),x86_64)
85 DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
86 DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
87 DOCKER_PARTIAL_IMAGES += debian-s390x-cross
88 DOCKER_PARTIAL_IMAGES += fedora
91 docker-image-debian-mips-cross: docker-image-debian10
92 docker-image-debian-sparc64-cross: docker-image-debian10
94 # The native build should never use the registry
95 docker-image-debian-native: DOCKER_REGISTRY=
97 # base images should not add a local user
98 docker-image-debian10: NOUSER=1
99 docker-image-debian11: NOUSER=1
101 # alpine has no adduser
102 docker-image-alpine: NOUSER=1
104 debian-toolchain-run = \
106 $(call quiet-command, \
107 $(DOCKER_SCRIPT) build -t qemu/$1 -f $< \
108 $(if $V,,--quiet) --no-cache \
109 --registry $(DOCKER_REGISTRY) --extra-files \
110 $(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh, \
112 $(call quiet-command, \
113 $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \
114 qemu/$1 $(DOCKER_REGISTRY), \
116 $(call quiet-command, \
117 $(DOCKER_SCRIPT) update $(if $V,,--quiet) \
119 $(if $(NOUSER),,--add-current-user) \
121 debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
123 docker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker \
124 $(DOCKER_FILES_DIR)/debian-hexagon-cross.d/build-toolchain.sh
125 $(call debian-toolchain, $@)
127 docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
128 $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
129 $(call debian-toolchain, $@)
131 docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
132 $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
133 $(call debian-toolchain, $@)
135 # Specialist build images, sometimes very limited tools
136 docker-image-debian-tricore-cross: docker-image-debian10
137 docker-image-debian-all-test-cross: docker-image-debian10
138 docker-image-debian-loongarch-cross: docker-image-debian11
139 docker-image-debian-microblaze-cross: docker-image-debian10
140 docker-image-debian-nios2-cross: docker-image-debian10
141 docker-image-debian-powerpc-test-cross: docker-image-debian11
142 docker-image-debian-riscv64-test-cross: docker-image-debian11
144 # These images may be good enough for building tests but not for test builds
145 DOCKER_PARTIAL_IMAGES += debian-alpha-cross
146 DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
147 DOCKER_PARTIAL_IMAGES += debian-hppa-cross
148 DOCKER_PARTIAL_IMAGES += debian-loongarch-cross
149 DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
150 DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
151 DOCKER_PARTIAL_IMAGES += debian-nios2-cross
152 DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
153 DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
154 DOCKER_PARTIAL_IMAGES += debian-tricore-cross
155 DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
156 DOCKER_PARTIAL_IMAGES += fedora-cris-cross
158 # images that are only used to build other images
159 DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
161 __IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
162 DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
164 __TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
165 DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
167 # Expand all the pre-requistes for each docker image and test combination
168 $(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
169 $(foreach t,$(DOCKER_TESTS), \
170 $(eval .PHONY: docker-$t@$i) \
171 $(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \
173 $(foreach t,$(DOCKER_TESTS), \
174 $(eval docker-all-tests: docker-$t@$i) \
175 $(eval docker-$t: docker-$t@$i) \
180 @echo 'Build QEMU and run tests inside Docker or Podman containers'
182 @echo 'Available targets:'
184 @echo ' docker: Print this help.'
185 @echo ' docker-all-tests: Run all image/test combinations.'
186 @echo ' docker-TEST: Run "TEST" on all image combinations.'
187 @echo ' docker-clean: Kill and remove residual docker testing containers.'
188 @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".'
189 @echo ' Note: "TEST" is one of the listed test name,'
190 @echo ' or a script name under $$QEMU_SRC/tests/docker/;'
191 @echo ' "IMAGE" is one of the listed container name.'
192 @echo ' docker-image: Build all images.'
193 @echo ' docker-image-IMAGE: Build image "IMAGE".'
194 @echo ' docker-run: For manually running a "TEST" with "IMAGE".'
196 @echo 'Available container images:'
197 @echo ' $(DOCKER_IMAGES)'
199 @echo 'Available tests:'
200 @echo ' $(DOCKER_TESTS)'
202 @echo 'Special variables:'
203 @echo ' TARGET_LIST=a,b,c Override target list in builds.'
204 @echo ' EXTRA_CONFIGURE_OPTS="..."'
205 @echo ' Extra configure options.'
206 @echo ' IMAGES="a b c ..": Restrict available images to subset.'
207 @echo ' TESTS="x y z .." Restrict available tests to subset.'
208 @echo ' J=[0..9]* Overrides the -jN parameter for make commands'
209 @echo ' (default is 1)'
210 @echo ' DEBUG=1 Stop and drop to shell in the created container'
211 @echo ' before running the command.'
212 @echo ' NETWORK=1 Enable virtual network interface with default backend.'
213 @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.'
214 @echo ' NOUSER=1 Define to disable adding current user to containers passwd.'
215 @echo ' NOCACHE=1 Ignore cache when build images.'
216 @echo ' EXECUTABLE=<path> Include executable in image.'
217 @echo ' EXTRA_FILES="<path> [... <path>]"'
218 @echo ' Include extra files in image.'
219 @echo ' ENGINE=auto/docker/podman'
220 @echo ' Specify which container engine to run.'
221 @echo ' REGISTRY=url Cache builds from registry (default:$(DOCKER_REGISTRY))'
225 # Use a global constant ccache directory to speed up repetitive builds
226 DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
228 # This rule if for directly running against an arbitrary docker target.
229 # It is called by the expanded docker targets (e.g. make
230 # docker-test-foo@bar) which will also ensure the image is up to date.
232 # For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
234 docker-run: docker-qemu-src
235 @mkdir -p "$(DOCKER_CCACHE_DIR)"
236 @if test -z "$(IMAGE)" || test -z "$(TEST)"; \
237 then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
239 $(if $(EXECUTABLE), \
240 $(call quiet-command, \
241 $(DOCKER_SCRIPT) update \
242 $(IMAGE) --executable $(EXECUTABLE), \
243 " COPYING $(EXECUTABLE) to $(IMAGE)"))
244 $(call quiet-command, \
245 $(DOCKER_SCRIPT) run \
246 $(if $(NOUSER),,--run-as-current-user) \
247 --security-opt seccomp=unconfined \
248 $(if $(DEBUG),-ti,) \
249 $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
250 -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \
251 -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
252 -e V=$V -e J=$J -e DEBUG=$(DEBUG) \
253 -e SHOW_ENV=$(SHOW_ENV) \
255 -e CCACHE_DIR=/var/tmp/ccache \
256 -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
258 -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
261 $(TEST), " RUN $(TEST) in ${IMAGE}")
262 $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
263 " CLEANUP $(DOCKER_SRC_COPY)")
265 docker-image: ${DOCKER_IMAGES:%=docker-image-%}
268 $(call quiet-command, $(DOCKER_SCRIPT) clean)