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 = $(shell uname -m) 10USER = $(if $(NOUSER),,$(shell id -un)) 11UID = $(if $(NOUSER),,$(shell id -u)) 12 13DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles 14ifeq ($(HOST_ARCH),x86_64) 15DOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu 16endif 17DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY)) 18 19RUNC ?= $(if $(shell command -v docker), docker, podman) 20DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(RUNC) 21 22CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$) 23DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME) 24 25.DELETE_ON_ERROR: $(DOCKER_SRC_COPY) 26$(DOCKER_SRC_COPY): 27 @mkdir $@ 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, \ 34 "COPY","RUNNER") 35 36docker-qemu-src: $(DOCKER_SRC_COPY) 37 38# General rule for building docker images. 39docker-image-%: $(DOCKER_FILES_DIR)/%.docker 40 $(call quiet-command, \ 41 DOCKER_BUILDKIT=1 $(RUNC) build \ 42 $(if $V,,--quiet) \ 43 $(if $(NOCACHE),--no-cache, \ 44 $(if $(DOCKER_REGISTRY),--cache-from $(DOCKER_REGISTRY)/qemu/$*)) \ 45 --build-arg BUILDKIT_INLINE_CACHE=1 \ 46 $(if $(NOUSER),, \ 47 --build-arg USER=$(USER) \ 48 --build-arg UID=$(UID)) \ 49 -t qemu/$* - < $< $(if $V,,> /dev/null),\ 50 "BUILD", $*) 51 52# Special rule for debootstraped binfmt linux-user images 53docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker 54 $(if $(EXECUTABLE),,\ 55 $(error EXECUTABLE not set, debootstrap of debian-$* would fail)) 56 $(if $(DEB_ARCH),,\ 57 $(error DEB_ARCH not set, debootstrap of debian-$* would fail)) 58 $(if $(DEB_TYPE),,\ 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")) 75 76# Special case cross-compiling x86_64 on non-x86_64 systems. 77ifeq ($(HOST_ARCH),x86_64) 78DOCKER_PARTIAL_IMAGES += debian-amd64-cross 79else 80DOCKER_PARTIAL_IMAGES += debian-amd64 81endif 82 83# For non-x86 hosts not all cross-compilers have been packaged 84ifneq ($(HOST_ARCH),x86_64) 85DOCKER_PARTIAL_IMAGES += debian-mipsel-cross debian-mips64el-cross 86DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross 87DOCKER_PARTIAL_IMAGES += debian-s390x-cross 88DOCKER_PARTIAL_IMAGES += fedora 89endif 90 91# The native build should never use the registry 92docker-image-debian-native: DOCKER_REGISTRY= 93 94# alpine has no adduser 95docker-image-alpine: NOUSER=1 96 97debian-toolchain-run = \ 98 $(if $(NOCACHE), \ 99 $(call quiet-command, \ 100 $(DOCKER_SCRIPT) build -t qemu/$1 -f $< \ 101 $(if $V,,--quiet) --no-cache \ 102 --registry $(DOCKER_REGISTRY) --extra-files \ 103 $(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh, \ 104 "BUILD", $1), \ 105 $(call quiet-command, \ 106 $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \ 107 qemu/$1 $(DOCKER_REGISTRY), \ 108 "FETCH", $1) \ 109 $(call quiet-command, \ 110 $(DOCKER_SCRIPT) update $(if $V,,--quiet) \ 111 qemu/$1 \ 112 $(if $(NOUSER),,--add-current-user) \ 113 "PREPARE", $1)) 114debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1)) 115 116docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \ 117 $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh 118 $(call debian-toolchain, $@) 119 120docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \ 121 $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh 122 $(call debian-toolchain, $@) 123 124# These images may be good enough for building tests but not for test builds 125DOCKER_PARTIAL_IMAGES += debian-alpha-cross 126DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross 127DOCKER_PARTIAL_IMAGES += debian-hppa-cross 128DOCKER_PARTIAL_IMAGES += debian-loongarch-cross 129DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross 130DOCKER_PARTIAL_IMAGES += debian-microblaze-cross 131DOCKER_PARTIAL_IMAGES += debian-mips-cross 132DOCKER_PARTIAL_IMAGES += debian-nios2-cross 133DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross 134DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross 135DOCKER_PARTIAL_IMAGES += debian-xtensa-cross 136DOCKER_PARTIAL_IMAGES += fedora-cris-cross 137 138# images that are only used to build other images 139DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain 140 141__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))) 142DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES)) 143 144__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f)) 145DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS)) 146 147# Expand all the pre-requistes for each docker image and test combination 148$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \ 149 $(foreach t,$(DOCKER_TESTS), \ 150 $(eval .PHONY: docker-$t@$i) \ 151 $(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \ 152 ) \ 153 $(foreach t,$(DOCKER_TESTS), \ 154 $(eval docker-all-tests: docker-$t@$i) \ 155 $(eval docker-$t: docker-$t@$i) \ 156 ) \ 157) 158 159docker: 160 @echo 'Build QEMU and run tests inside $(RUNC) containers' 161 @echo 162 @echo 'Available targets:' 163 @echo 164 @echo ' docker: Print this help.' 165 @echo ' docker-all-tests: Run all image/test combinations.' 166 @echo ' docker-TEST: Run "TEST" on all image combinations.' 167 @echo ' docker-clean: Kill and remove residual docker testing containers.' 168 @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".' 169 @echo ' Note: "TEST" is one of the listed test name,' 170 @echo ' or a script name under $$QEMU_SRC/tests/docker/;' 171 @echo ' "IMAGE" is one of the listed container name.' 172 @echo ' docker-image: Build all images.' 173 @echo ' docker-image-IMAGE: Build image "IMAGE".' 174 @echo ' docker-run: For manually running a "TEST" with "IMAGE".' 175 @echo 176 @echo 'Available container images:' 177 @echo ' $(DOCKER_IMAGES)' 178 @echo 179 @echo 'Available tests:' 180 @echo ' $(DOCKER_TESTS)' 181 @echo 182 @echo 'Special variables:' 183 @echo ' TARGET_LIST=a,b,c Override target list in builds.' 184 @echo ' EXTRA_CONFIGURE_OPTS="..."' 185 @echo ' Extra configure options.' 186 @echo ' TEST_COMMAND="..." Override the default `make check` target.' 187 @echo ' IMAGES="a b c ..": Restrict available images to subset.' 188 @echo ' TESTS="x y z .." Restrict available tests to subset.' 189 @echo ' J=[0..9]* Overrides the -jN parameter for make commands' 190 @echo ' (default is 1)' 191 @echo ' DEBUG=1 Stop and drop to shell in the created container' 192 @echo ' before running the command.' 193 @echo ' NETWORK=1 Enable virtual network interface with default backend.' 194 @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.' 195 @echo ' NOUSER=1 Define to disable adding current user to containers passwd.' 196 @echo ' NOCACHE=1 Ignore cache when build images.' 197 @echo ' EXECUTABLE=<path> Include executable in image.' 198 @echo ' EXTRA_FILES="<path> [... <path>]"' 199 @echo ' Include extra files in image.' 200 @echo ' REGISTRY=url Cache builds from registry (default:$(DOCKER_REGISTRY))' 201 202docker-help: docker 203 204# Use a global constant ccache directory to speed up repetitive builds 205DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache 206 207# This rule if for directly running against an arbitrary docker target. 208# It is called by the expanded docker targets (e.g. make 209# docker-test-foo@bar) which will also ensure the image is up to date. 210# 211# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64 212# 213docker-run: docker-qemu-src 214 @mkdir -p "$(DOCKER_CCACHE_DIR)" 215 @if test -z "$(IMAGE)" || test -z "$(TEST)"; \ 216 then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \ 217 fi 218 $(if $(EXECUTABLE), \ 219 $(call quiet-command, \ 220 $(DOCKER_SCRIPT) update \ 221 $(IMAGE) --executable $(EXECUTABLE), \ 222 " COPYING $(EXECUTABLE) to $(IMAGE)")) 223 $(call quiet-command, \ 224 $(RUNC) run \ 225 --rm \ 226 $(if $(NOUSER),,-u $(UID)) \ 227 --security-opt seccomp=unconfined \ 228 $(if $(DEBUG),-ti,) \ 229 $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \ 230 -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \ 231 -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \ 232 -e TEST_COMMAND="$(TEST_COMMAND)" \ 233 -e V=$V -e J=$J -e DEBUG=$(DEBUG) \ 234 -e SHOW_ENV=$(SHOW_ENV) \ 235 $(if $(NOUSER),, \ 236 -e CCACHE_DIR=/var/tmp/ccache \ 237 -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \ 238 ) \ 239 -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \ 240 $(IMAGE) \ 241 /var/tmp/qemu/run \ 242 $(TEST), " RUN $(TEST) in ${IMAGE}") 243 $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \ 244 " CLEANUP $(DOCKER_SRC_COPY)") 245 246docker-image: ${DOCKER_IMAGES:%=docker-image-%} 247 248docker-clean: 249 $(call quiet-command, $(DOCKER_SCRIPT) clean) 250