1 # Makefile for Docker tests
3 .PHONY: docker docker-help docker-test docker-clean docker-image docker-qemu-src
9 HOST_ARCH = $(shell uname -m)
10 USER = $(if $(NOUSER),,$(shell id -un))
11 UID = $(if $(NOUSER),,$(shell id -u))
13 DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
14 ifeq ($(HOST_ARCH),x86_64)
15 DOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu
17 DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY))
19 RUNC ?= $(if $(shell command -v docker), docker, podman)
20 DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(RUNC)
22 CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
23 DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
25 .DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
28 $(if $(SRC_ARCHIVE), \
29 $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
30 "CP", "$@/qemu.tar"), \
31 $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
32 "GEN", "$@/qemu.tar"))
33 $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
36 docker-qemu-src: $(DOCKER_SRC_COPY)
38 # General rule for building docker images.
39 docker-image-%: $(DOCKER_FILES_DIR)/%.docker
40 $(call quiet-command, \
41 DOCKER_BUILDKIT=1 $(RUNC) build \
43 $(if $(NOCACHE),--no-cache, \
44 $(if $(DOCKER_REGISTRY),--cache-from $(DOCKER_REGISTRY)/qemu/$*)) \
45 --build-arg BUILDKIT_INLINE_CACHE=1 \
47 --build-arg USER=$(USER) \
48 --build-arg UID=$(UID)) \
49 -t qemu/$* - < $< $(if $V,,> /dev/null),\
52 # Special rule for debootstraped binfmt linux-user images
53 docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
55 $(error EXECUTABLE not set, debootstrap of debian-$* would fail))
57 $(error DEB_ARCH not set, debootstrap of debian-$* would fail))
59 $(error DEB_TYPE not set, debootstrap of debian-$* would fail))
60 $(if $(wildcard $(EXECUTABLE)), \
61 $(call quiet-command, \
62 DEB_ARCH=$(DEB_ARCH) \
63 DEB_TYPE=$(DEB_TYPE) \
64 $(if $(DEB_URL),DEB_URL=$(DEB_URL),) \
65 $(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< \
66 $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \
67 $(if $(NOUSER),,--add-current-user) \
68 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES)) \
69 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
70 "BUILD","binfmt debian-$* (debootstrapped)"), \
71 $(call quiet-command, \
72 $(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || \
73 { echo "You will need to build $(EXECUTABLE)"; exit 1;},\
74 "CHECK", "debian-$* exists"))
76 # Special case cross-compiling x86_64 on non-x86_64 systems.
77 ifeq ($(HOST_ARCH),x86_64)
78 DOCKER_PARTIAL_IMAGES += debian-amd64-cross
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-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 # alpine has no adduser
92 docker-image-alpine: NOUSER=1
94 debian-toolchain-run = \
96 $(call quiet-command, \
97 $(DOCKER_SCRIPT) build -t qemu/$1 -f $< \
98 $(if $V,,--quiet) --no-cache \
99 --registry $(DOCKER_REGISTRY) --extra-files \
100 $(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh, \
102 $(call quiet-command, \
103 $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \
104 qemu/$1 $(DOCKER_REGISTRY), \
106 $(call quiet-command, \
107 $(DOCKER_SCRIPT) update $(if $V,,--quiet) \
109 $(if $(NOUSER),,--add-current-user) \
111 debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
113 docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
114 $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
115 $(call debian-toolchain, $@)
117 # These images may be good enough for building tests but not for test builds
118 DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
119 DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
121 # images that are only used to build other images
122 DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
124 __IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
125 DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
127 __TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
128 DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
130 # Expand all the pre-requistes for each docker image and test combination
131 $(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
132 $(foreach t,$(DOCKER_TESTS), \
133 $(eval .PHONY: docker-$t@$i) \
134 $(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \
136 $(foreach t,$(DOCKER_TESTS), \
137 $(eval docker-all-tests: docker-$t@$i) \
138 $(eval docker-$t: docker-$t@$i) \
143 @echo 'Build QEMU and run tests inside $(RUNC) containers'
145 @echo 'Available targets:'
147 @echo ' docker: Print this help.'
148 @echo ' docker-all-tests: Run all image/test combinations.'
149 @echo ' docker-TEST: Run "TEST" on all image combinations.'
150 @echo ' docker-clean: Kill and remove residual docker testing containers.'
151 @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".'
152 @echo ' Note: "TEST" is one of the listed test name,'
153 @echo ' or a script name under $$QEMU_SRC/tests/docker/;'
154 @echo ' "IMAGE" is one of the listed container name.'
155 @echo ' docker-image: Build all images.'
156 @echo ' docker-image-IMAGE: Build image "IMAGE".'
157 @echo ' docker-run: For manually running a "TEST" with "IMAGE".'
159 @echo 'Available container images:'
160 @echo ' $(DOCKER_IMAGES)'
162 @echo 'Available tests:'
163 @echo ' $(DOCKER_TESTS)'
165 @echo 'Special variables:'
166 @echo ' TARGET_LIST=a,b,c Override target list in builds.'
167 @echo ' EXTRA_CONFIGURE_OPTS="..."'
168 @echo ' Extra configure options.'
169 @echo ' TEST_COMMAND="..." Override the default `make check` target.'
170 @echo ' IMAGES="a b c ..": Restrict available images to subset.'
171 @echo ' TESTS="x y z .." Restrict available tests to subset.'
172 @echo ' J=[0..9]* Overrides the -jN parameter for make commands'
173 @echo ' (default is 1)'
174 @echo ' DEBUG=1 Stop and drop to shell in the created container'
175 @echo ' before running the command.'
176 @echo ' NETWORK=1 Enable virtual network interface with default backend.'
177 @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.'
178 @echo ' NOUSER=1 Define to disable adding current user to containers passwd.'
179 @echo ' NOCACHE=1 Ignore cache when build images.'
180 @echo ' EXECUTABLE=<path> Include executable in image.'
181 @echo ' EXTRA_FILES="<path> [... <path>]"'
182 @echo ' Include extra files in image.'
183 @echo ' REGISTRY=url Cache builds from registry (default:$(DOCKER_REGISTRY))'
187 # Use a global constant ccache directory to speed up repetitive builds
188 DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
190 # This rule if for directly running against an arbitrary docker target.
191 # It is called by the expanded docker targets (e.g. make
192 # docker-test-foo@bar) which will also ensure the image is up to date.
194 # For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
196 docker-run: docker-qemu-src
197 @mkdir -p "$(DOCKER_CCACHE_DIR)"
198 @if test -z "$(IMAGE)" || test -z "$(TEST)"; \
199 then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
201 $(if $(EXECUTABLE), \
202 $(call quiet-command, \
203 $(DOCKER_SCRIPT) update \
204 $(IMAGE) --executable $(EXECUTABLE), \
205 " COPYING $(EXECUTABLE) to $(IMAGE)"))
206 $(call quiet-command, \
210 $(if $(filter docker,$(RUNC)), \
215 --security-opt seccomp=unconfined \
216 $(if $(DEBUG),-ti,) \
217 $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
218 -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \
219 -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
220 -e TEST_COMMAND="$(TEST_COMMAND)" \
221 -e V=$V -e J=$J -e DEBUG=$(DEBUG) \
222 -e SHOW_ENV=$(SHOW_ENV) \
224 -e CCACHE_DIR=/var/tmp/ccache \
225 -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
227 -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
230 $(TEST), " RUN $(TEST) in ${IMAGE}")
231 $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
232 " CLEANUP $(DOCKER_SRC_COPY)")
234 docker-image: ${DOCKER_IMAGES:%=docker-image-%}
237 $(call quiet-command, $(DOCKER_SCRIPT) clean)