1324027c2SFam Zheng# Makefile for Docker tests 2324027c2SFam Zheng 36147c249SAlex Bennée.PHONY: docker docker-help docker-test docker-clean docker-image docker-qemu-src 4324027c2SFam Zheng 5660f7930SPaolo BonziniNULL := 6660f7930SPaolo BonziniSPACE := $(NULL) # 7660f7930SPaolo BonziniCOMMA := , 8660f7930SPaolo Bonzini 906746324SAlex BennéeHOST_ARCH = $(if $(ARCH),$(ARCH),$(shell uname -m)) 100b1a6490SAlex BennéeUSER = $(if $(NOUSER),,$(shell id -un)) 110b1a6490SAlex BennéeUID = $(if $(NOUSER),,$(shell id -u)) 1206746324SAlex Bennée 13324027c2SFam ZhengDOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles 1434019198SAlex Bennéeifeq ($(HOST_ARCH),x86_64) 1534019198SAlex BennéeDOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu 1634019198SAlex Bennéeendif 1734019198SAlex BennéeDOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY)) 18324027c2SFam Zheng 190b1a6490SAlex BennéeRUNC ?= docker 20c4575b59SPaolo BonziniENGINE ?= auto 2105af039dSMarc-André LureauDOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE) 22e27cae26SAlex Bennée 23324027c2SFam ZhengCUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$) 24bc739b8eSPaolo BonziniDOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME) 25324027c2SFam Zheng 26b28ef6b9SPhilippe Mathieu-Daudé.DELETE_ON_ERROR: $(DOCKER_SRC_COPY) 27324027c2SFam Zheng$(DOCKER_SRC_COPY): 28324027c2SFam Zheng @mkdir $@ 2973fb4f1dSFam Zheng $(if $(SRC_ARCHIVE), \ 3073fb4f1dSFam Zheng $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \ 3173fb4f1dSFam Zheng "CP", "$@/qemu.tar"), \ 32bc739b8eSPaolo Bonzini $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \ 3373fb4f1dSFam Zheng "GEN", "$@/qemu.tar")) 34324027c2SFam Zheng $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \ 350bdb12c7SPeter Maydell "COPY","RUNNER") 36324027c2SFam Zheng 37324027c2SFam Zhengdocker-qemu-src: $(DOCKER_SRC_COPY) 38324027c2SFam Zheng 3977162849SPaolo Bonzini# General rule for building docker images. 40324027c2SFam Zhengdocker-image-%: $(DOCKER_FILES_DIR)/%.docker 41324027c2SFam Zheng $(call quiet-command, \ 42*55154c57SAlex Bennée DOCKER_BUILDKIT=1 $(RUNC) build \ 43e6f1306bSAlex Bennée $(if $V,,--quiet) \ 44e6f1306bSAlex Bennée $(if $(NOCACHE),--no-cache, \ 450b1a6490SAlex Bennée $(if $(DOCKER_REGISTRY),--cache-from $(DOCKER_REGISTRY)/qemu/$*)) \ 460b1a6490SAlex Bennée --build-arg BUILDKIT_INLINE_CACHE=1 \ 470b1a6490SAlex Bennée $(if $(NOUSER),, \ 480b1a6490SAlex Bennée --build-arg USER=$(USER) \ 490b1a6490SAlex Bennée --build-arg UID=$(UID)) \ 500b1a6490SAlex Bennée -t qemu/$* - < $<, \ 510b1a6490SAlex Bennée "BUILD", $1) 52dc338fdbSAlex Bennée 53dc338fdbSAlex Bennée# Special rule for debootstraped binfmt linux-user images 54dc338fdbSAlex Bennéedocker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker 55dc338fdbSAlex Bennée $(if $(EXECUTABLE),,\ 56dc338fdbSAlex Bennée $(error EXECUTABLE not set, debootstrap of debian-$* would fail)) 57dc338fdbSAlex Bennée $(if $(DEB_ARCH),,\ 58dc338fdbSAlex Bennée $(error DEB_ARCH not set, debootstrap of debian-$* would fail)) 59dc338fdbSAlex Bennée $(if $(DEB_TYPE),,\ 60dc338fdbSAlex Bennée $(error DEB_TYPE not set, debootstrap of debian-$* would fail)) 6127963460SAlex Bennée $(if $(wildcard $(EXECUTABLE)), \ 62dc338fdbSAlex Bennée $(call quiet-command, \ 63dc338fdbSAlex Bennée DEB_ARCH=$(DEB_ARCH) \ 64dc338fdbSAlex Bennée DEB_TYPE=$(DEB_TYPE) \ 654d26c7feSAlex Bennée $(if $(DEB_URL),DEB_URL=$(DEB_URL),) \ 66182ec060SAlex Bennée $(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< \ 67dc338fdbSAlex Bennée $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \ 68dc338fdbSAlex Bennée $(if $(NOUSER),,--add-current-user) \ 69dc338fdbSAlex Bennée $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES)) \ 70dc338fdbSAlex Bennée $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \ 7119c9a18fSAlex Bennée "BUILD","binfmt debian-$* (debootstrapped)"), \ 7219c9a18fSAlex Bennée $(call quiet-command, \ 73767b6bd2SAlex Bennée $(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || \ 74d19f5fc0SAlex Bennée { echo "You will need to build $(EXECUTABLE)"; exit 1;},\ 7519c9a18fSAlex Bennée "CHECK", "debian-$* exists")) 76dc338fdbSAlex Bennée 77d996f0aeSAlex Bennée# Special case cross-compiling x86_64 on non-x86_64 systems. 7806746324SAlex Bennéeifeq ($(HOST_ARCH),x86_64) 79ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64-cross 80ec22b726SAlex Bennéeelse 81ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64 82ec22b726SAlex Bennéeendif 83409c1c9cSAlex Bennée 84e5389e44SAlex Bennée# For non-x86 hosts not all cross-compilers have been packaged 8506746324SAlex Bennéeifneq ($(HOST_ARCH),x86_64) 8654ab3c3fSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mipsel-cross debian-mips64el-cross 87e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-ppc64el-cross 88e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-s390x-cross 89d79f5c8bSDaniel P. BerrangéDOCKER_PARTIAL_IMAGES += fedora 90e5389e44SAlex Bennéeendif 91e5389e44SAlex Bennée 9217888749SAlex Bennée# The native build should never use the registry 9317888749SAlex Bennéedocker-image-debian-native: DOCKER_REGISTRY= 9417888749SAlex Bennée 955dbefb87SAlex Bennée# alpine has no adduser 965dbefb87SAlex Bennéedocker-image-alpine: NOUSER=1 975dbefb87SAlex Bennée 982eb4461aSRichard Hendersondebian-toolchain-run = \ 992eb4461aSRichard Henderson $(if $(NOCACHE), \ 1002eb4461aSRichard Henderson $(call quiet-command, \ 1012eb4461aSRichard Henderson $(DOCKER_SCRIPT) build -t qemu/$1 -f $< \ 1022eb4461aSRichard Henderson $(if $V,,--quiet) --no-cache \ 1032eb4461aSRichard Henderson --registry $(DOCKER_REGISTRY) --extra-files \ 1042eb4461aSRichard Henderson $(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh, \ 1052eb4461aSRichard Henderson "BUILD", $1), \ 1062eb4461aSRichard Henderson $(call quiet-command, \ 1072eb4461aSRichard Henderson $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \ 1082eb4461aSRichard Henderson qemu/$1 $(DOCKER_REGISTRY), \ 1092eb4461aSRichard Henderson "FETCH", $1) \ 1102eb4461aSRichard Henderson $(call quiet-command, \ 1112eb4461aSRichard Henderson $(DOCKER_SCRIPT) update $(if $V,,--quiet) \ 1122eb4461aSRichard Henderson qemu/$1 \ 1132eb4461aSRichard Henderson $(if $(NOUSER),,--add-current-user) \ 1142eb4461aSRichard Henderson "PREPARE", $1)) 1152eb4461aSRichard Hendersondebian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1)) 1162eb4461aSRichard Henderson 117b58a4e68SRichard Hendersondocker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \ 118b58a4e68SRichard Henderson $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh 119b58a4e68SRichard Henderson $(call debian-toolchain, $@) 120b58a4e68SRichard Henderson 1212eb4461aSRichard Hendersondocker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \ 1222eb4461aSRichard Henderson $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh 1232eb4461aSRichard Henderson $(call debian-toolchain, $@) 1242eb4461aSRichard Henderson 12577b08f73SAlex Bennée# These images may be good enough for building tests but not for test builds 126ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-alpha-cross 127cf9b6d21SMatheus FerstDOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross 128ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-hppa-cross 129b70ec50bSRichard HendersonDOCKER_PARTIAL_IMAGES += debian-loongarch-cross 130ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross 131b58a4e68SRichard HendersonDOCKER_PARTIAL_IMAGES += debian-microblaze-cross 13254ab3c3fSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mips-cross 1332eb4461aSRichard HendersonDOCKER_PARTIAL_IMAGES += debian-nios2-cross 1347d5817a4SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross 135ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross 136ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-xtensa-cross 1376a2b0fd1SRichard HendersonDOCKER_PARTIAL_IMAGES += fedora-cris-cross 13877b08f73SAlex Bennée 13979bd3ae9SPaolo Bonzini# images that are only used to build other images 14079bd3ae9SPaolo BonziniDOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain 14179bd3ae9SPaolo Bonzini 14279bd3ae9SPaolo Bonzini__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))) 14379bd3ae9SPaolo BonziniDOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES)) 14479bd3ae9SPaolo Bonzini 14579bd3ae9SPaolo Bonzini__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f)) 14679bd3ae9SPaolo BonziniDOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS)) 14779bd3ae9SPaolo Bonzini 148324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination 149bbb5ba86SPaolo Bonzini$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \ 150d79f5c8bSDaniel P. Berrangé $(foreach t,$(DOCKER_TESTS), \ 151324027c2SFam Zheng $(eval .PHONY: docker-$t@$i) \ 152ab698a4dSAlex Bennée $(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \ 153324027c2SFam Zheng ) \ 154324027c2SFam Zheng $(foreach t,$(DOCKER_TESTS), \ 155ff1a3902SAlex Bennée $(eval docker-all-tests: docker-$t@$i) \ 156ff1a3902SAlex Bennée $(eval docker-$t: docker-$t@$i) \ 157324027c2SFam Zheng ) \ 158324027c2SFam Zheng) 159324027c2SFam Zheng 160324027c2SFam Zhengdocker: 16105af039dSMarc-André Lureau @echo 'Build QEMU and run tests inside Docker or Podman containers' 162324027c2SFam Zheng @echo 163324027c2SFam Zheng @echo 'Available targets:' 164324027c2SFam Zheng @echo 165324027c2SFam Zheng @echo ' docker: Print this help.' 166ff1a3902SAlex Bennée @echo ' docker-all-tests: Run all image/test combinations.' 16787db9018SWainer dos Santos Moschetta @echo ' docker-TEST: Run "TEST" on all image combinations.' 168324027c2SFam Zheng @echo ' docker-clean: Kill and remove residual docker testing containers.' 169324027c2SFam Zheng @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".' 170324027c2SFam Zheng @echo ' Note: "TEST" is one of the listed test name,' 171324027c2SFam Zheng @echo ' or a script name under $$QEMU_SRC/tests/docker/;' 17287db9018SWainer dos Santos Moschetta @echo ' "IMAGE" is one of the listed container name.' 173324027c2SFam Zheng @echo ' docker-image: Build all images.' 174324027c2SFam Zheng @echo ' docker-image-IMAGE: Build image "IMAGE".' 17587db9018SWainer dos Santos Moschetta @echo ' docker-run: For manually running a "TEST" with "IMAGE".' 176324027c2SFam Zheng @echo 177324027c2SFam Zheng @echo 'Available container images:' 178324027c2SFam Zheng @echo ' $(DOCKER_IMAGES)' 179324027c2SFam Zheng @echo 180324027c2SFam Zheng @echo 'Available tests:' 181324027c2SFam Zheng @echo ' $(DOCKER_TESTS)' 182324027c2SFam Zheng @echo 183324027c2SFam Zheng @echo 'Special variables:' 184324027c2SFam Zheng @echo ' TARGET_LIST=a,b,c Override target list in builds.' 18535e0f959SFam Zheng @echo ' EXTRA_CONFIGURE_OPTS="..."' 18635e0f959SFam Zheng @echo ' Extra configure options.' 187e558220dSAlex Bennée @echo ' TEST_COMMAND="..." Override the default `make check` target.' 188be20302aSAlex Bennée @echo ' IMAGES="a b c ..": Restrict available images to subset.' 189be20302aSAlex Bennée @echo ' TESTS="x y z .." Restrict available tests to subset.' 190324027c2SFam Zheng @echo ' J=[0..9]* Overrides the -jN parameter for make commands' 191324027c2SFam Zheng @echo ' (default is 1)' 192324027c2SFam Zheng @echo ' DEBUG=1 Stop and drop to shell in the created container' 193324027c2SFam Zheng @echo ' before running the command.' 1948a2390a4SDaniel P. Berrange @echo ' NETWORK=1 Enable virtual network interface with default backend.' 1956423795eSPhilippe Mathieu-Daudé @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.' 196dc23bbc3SPhilippe Mathieu-Daudé @echo ' NOUSER=1 Define to disable adding current user to containers passwd.' 197324027c2SFam Zheng @echo ' NOCACHE=1 Ignore cache when build images.' 198b7c851b2SAlex Bennée @echo ' EXECUTABLE=<path> Include executable in image.' 1994c84f662SPhilippe Mathieu-Daudé @echo ' EXTRA_FILES="<path> [... <path>]"' 2004c84f662SPhilippe Mathieu-Daudé @echo ' Include extra files in image.' 20105af039dSMarc-André Lureau @echo ' ENGINE=auto/docker/podman' 20205af039dSMarc-André Lureau @echo ' Specify which container engine to run.' 203e6f1306bSAlex Bennée @echo ' REGISTRY=url Cache builds from registry (default:$(DOCKER_REGISTRY))' 204324027c2SFam Zheng 2056147c249SAlex Bennéedocker-help: docker 2066147c249SAlex Bennée 20779bd3ae9SPaolo Bonzini# Use a global constant ccache directory to speed up repetitive builds 20879bd3ae9SPaolo BonziniDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache 20979bd3ae9SPaolo Bonzini 210e86c9a64SAlex Bennée# This rule if for directly running against an arbitrary docker target. 211e86c9a64SAlex Bennée# It is called by the expanded docker targets (e.g. make 212d39eaa22SPaolo Bonzini# docker-test-foo@bar) which will also ensure the image is up to date. 213e86c9a64SAlex Bennée# 214e86c9a64SAlex Bennée# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64 215e86c9a64SAlex Bennée# 216e86c9a64SAlex Bennéedocker-run: docker-qemu-src 217324027c2SFam Zheng @mkdir -p "$(DOCKER_CCACHE_DIR)" 218e86c9a64SAlex Bennée @if test -z "$(IMAGE)" || test -z "$(TEST)"; \ 219e86c9a64SAlex Bennée then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \ 220324027c2SFam Zheng fi 221e86c9a64SAlex Bennée $(if $(EXECUTABLE), \ 222324027c2SFam Zheng $(call quiet-command, \ 223e27cae26SAlex Bennée $(DOCKER_SCRIPT) update \ 2248d628d07SAlex Bennée $(IMAGE) --executable $(EXECUTABLE), \ 225e86c9a64SAlex Bennée " COPYING $(EXECUTABLE) to $(IMAGE)")) 226e86c9a64SAlex Bennée $(call quiet-command, \ 227789bc541SAlex Bennée $(RUNC) run \ 228789bc541SAlex Bennée --rm \ 229789bc541SAlex Bennée $(if $(NOUSER),,-u $(UID)) \ 23044707491SFam Zheng --security-opt seccomp=unconfined \ 2314b2c6bc5SFam Zheng $(if $(DEBUG),-ti,) \ 2328a2390a4SDaniel P. Berrange $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \ 2335609c512SAlex Bennée -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \ 234e86c9a64SAlex Bennée -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \ 235e558220dSAlex Bennée -e TEST_COMMAND="$(TEST_COMMAND)" \ 236e86c9a64SAlex Bennée -e V=$V -e J=$J -e DEBUG=$(DEBUG) \ 237e86c9a64SAlex Bennée -e SHOW_ENV=$(SHOW_ENV) \ 23836ac78e6SFam Zheng $(if $(NOUSER),, \ 239324027c2SFam Zheng -e CCACHE_DIR=/var/tmp/ccache \ 240324027c2SFam Zheng -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \ 24136ac78e6SFam Zheng ) \ 24236ac78e6SFam Zheng -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \ 243e86c9a64SAlex Bennée $(IMAGE) \ 244c8158513SFam Zheng /var/tmp/qemu/run \ 245e86c9a64SAlex Bennée $(TEST), " RUN $(TEST) in ${IMAGE}") 24608c3f6d5SPeter Xu $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \ 24708c3f6d5SPeter Xu " CLEANUP $(DOCKER_SRC_COPY)") 248e86c9a64SAlex Bennée 24979bd3ae9SPaolo Bonzinidocker-image: ${DOCKER_IMAGES:%=docker-image-%} 25079bd3ae9SPaolo Bonzini 251324027c2SFam Zhengdocker-clean: 252e27cae26SAlex Bennée $(call quiet-command, $(DOCKER_SCRIPT) clean) 253