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 9a2696204SPaolo BonziniHOST_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 19c6919250SAlex BennéeRUNC ?= $(if $(shell command -v docker), docker, podman) 20c6919250SAlex BennéeDOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(RUNC) 21e27cae26SAlex Bennée 22324027c2SFam ZhengCUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$) 23bc739b8eSPaolo BonziniDOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME) 24324027c2SFam Zheng 25b28ef6b9SPhilippe Mathieu-Daudé.DELETE_ON_ERROR: $(DOCKER_SRC_COPY) 26324027c2SFam Zheng$(DOCKER_SRC_COPY): 27324027c2SFam Zheng @mkdir $@ 2873fb4f1dSFam Zheng $(if $(SRC_ARCHIVE), \ 2973fb4f1dSFam Zheng $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \ 3073fb4f1dSFam Zheng "CP", "$@/qemu.tar"), \ 31bc739b8eSPaolo Bonzini $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \ 3273fb4f1dSFam Zheng "GEN", "$@/qemu.tar")) 33324027c2SFam Zheng $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \ 340bdb12c7SPeter Maydell "COPY","RUNNER") 35324027c2SFam Zheng 36324027c2SFam Zhengdocker-qemu-src: $(DOCKER_SRC_COPY) 37324027c2SFam Zheng 3877162849SPaolo Bonzini# General rule for building docker images. 39324027c2SFam Zhengdocker-image-%: $(DOCKER_FILES_DIR)/%.docker 40324027c2SFam Zheng $(call quiet-command, \ 4155154c57SAlex Bennée DOCKER_BUILDKIT=1 $(RUNC) build \ 42e6f1306bSAlex Bennée $(if $V,,--quiet) \ 43e6f1306bSAlex Bennée $(if $(NOCACHE),--no-cache, \ 440b1a6490SAlex Bennée $(if $(DOCKER_REGISTRY),--cache-from $(DOCKER_REGISTRY)/qemu/$*)) \ 450b1a6490SAlex Bennée --build-arg BUILDKIT_INLINE_CACHE=1 \ 460b1a6490SAlex Bennée $(if $(NOUSER),, \ 470b1a6490SAlex Bennée --build-arg USER=$(USER) \ 480b1a6490SAlex Bennée --build-arg UID=$(UID)) \ 496445c2caSAlex Bennée -t qemu/$* - < $< $(if $V,,> /dev/null),\ 506445c2caSAlex Bennée "BUILD", $*) 51dc338fdbSAlex Bennée 52dc338fdbSAlex Bennée# Special rule for debootstraped binfmt linux-user images 53dc338fdbSAlex Bennéedocker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker 54dc338fdbSAlex Bennée $(if $(EXECUTABLE),,\ 55dc338fdbSAlex Bennée $(error EXECUTABLE not set, debootstrap of debian-$* would fail)) 56dc338fdbSAlex Bennée $(if $(DEB_ARCH),,\ 57dc338fdbSAlex Bennée $(error DEB_ARCH not set, debootstrap of debian-$* would fail)) 58dc338fdbSAlex Bennée $(if $(DEB_TYPE),,\ 59dc338fdbSAlex Bennée $(error DEB_TYPE not set, debootstrap of debian-$* would fail)) 6027963460SAlex Bennée $(if $(wildcard $(EXECUTABLE)), \ 61dc338fdbSAlex Bennée $(call quiet-command, \ 62dc338fdbSAlex Bennée DEB_ARCH=$(DEB_ARCH) \ 63dc338fdbSAlex Bennée DEB_TYPE=$(DEB_TYPE) \ 644d26c7feSAlex Bennée $(if $(DEB_URL),DEB_URL=$(DEB_URL),) \ 65182ec060SAlex Bennée $(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< \ 66dc338fdbSAlex Bennée $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \ 67dc338fdbSAlex Bennée $(if $(NOUSER),,--add-current-user) \ 68dc338fdbSAlex Bennée $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES)) \ 69dc338fdbSAlex Bennée $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \ 7019c9a18fSAlex Bennée "BUILD","binfmt debian-$* (debootstrapped)"), \ 7119c9a18fSAlex Bennée $(call quiet-command, \ 72767b6bd2SAlex Bennée $(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || \ 73d19f5fc0SAlex Bennée { echo "You will need to build $(EXECUTABLE)"; exit 1;},\ 7419c9a18fSAlex Bennée "CHECK", "debian-$* exists")) 75dc338fdbSAlex Bennée 76d996f0aeSAlex Bennée# Special case cross-compiling x86_64 on non-x86_64 systems. 7706746324SAlex Bennéeifeq ($(HOST_ARCH),x86_64) 78ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64-cross 79ec22b726SAlex Bennéeelse 80ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64 81ec22b726SAlex Bennéeendif 82409c1c9cSAlex Bennée 83e5389e44SAlex Bennée# For non-x86 hosts not all cross-compilers have been packaged 8406746324SAlex Bennéeifneq ($(HOST_ARCH),x86_64) 8554ab3c3fSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mipsel-cross debian-mips64el-cross 86e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-ppc64el-cross 87e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-s390x-cross 88d79f5c8bSDaniel P. BerrangéDOCKER_PARTIAL_IMAGES += fedora 89e5389e44SAlex Bennéeendif 90e5389e44SAlex Bennée 915dbefb87SAlex Bennée# alpine has no adduser 925dbefb87SAlex Bennéedocker-image-alpine: NOUSER=1 935dbefb87SAlex Bennée 942eb4461aSRichard Hendersondebian-toolchain-run = \ 952eb4461aSRichard Henderson $(if $(NOCACHE), \ 962eb4461aSRichard Henderson $(call quiet-command, \ 972eb4461aSRichard Henderson $(DOCKER_SCRIPT) build -t qemu/$1 -f $< \ 982eb4461aSRichard Henderson $(if $V,,--quiet) --no-cache \ 992eb4461aSRichard Henderson --registry $(DOCKER_REGISTRY) --extra-files \ 1002eb4461aSRichard Henderson $(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh, \ 1012eb4461aSRichard Henderson "BUILD", $1), \ 1022eb4461aSRichard Henderson $(call quiet-command, \ 1032eb4461aSRichard Henderson $(DOCKER_SCRIPT) fetch $(if $V,,--quiet) \ 1042eb4461aSRichard Henderson qemu/$1 $(DOCKER_REGISTRY), \ 1052eb4461aSRichard Henderson "FETCH", $1) \ 1062eb4461aSRichard Henderson $(call quiet-command, \ 1072eb4461aSRichard Henderson $(DOCKER_SCRIPT) update $(if $V,,--quiet) \ 1082eb4461aSRichard Henderson qemu/$1 \ 1092eb4461aSRichard Henderson $(if $(NOUSER),,--add-current-user) \ 1102eb4461aSRichard Henderson "PREPARE", $1)) 1112eb4461aSRichard Hendersondebian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1)) 1122eb4461aSRichard Henderson 113b58a4e68SRichard Hendersondocker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \ 114b58a4e68SRichard Henderson $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh 115b58a4e68SRichard Henderson $(call debian-toolchain, $@) 116b58a4e68SRichard Henderson 11777b08f73SAlex Bennée# These images may be good enough for building tests but not for test builds 118b58a4e68SRichard HendersonDOCKER_PARTIAL_IMAGES += debian-microblaze-cross 119ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-xtensa-cross 1206a2b0fd1SRichard HendersonDOCKER_PARTIAL_IMAGES += fedora-cris-cross 12177b08f73SAlex Bennée 12279bd3ae9SPaolo Bonzini# images that are only used to build other images 12379bd3ae9SPaolo BonziniDOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain 12479bd3ae9SPaolo Bonzini 12579bd3ae9SPaolo Bonzini__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))) 12679bd3ae9SPaolo BonziniDOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES)) 12779bd3ae9SPaolo Bonzini 12879bd3ae9SPaolo Bonzini__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f)) 12979bd3ae9SPaolo BonziniDOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS)) 13079bd3ae9SPaolo Bonzini 131324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination 132bbb5ba86SPaolo Bonzini$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \ 133d79f5c8bSDaniel P. Berrangé $(foreach t,$(DOCKER_TESTS), \ 134324027c2SFam Zheng $(eval .PHONY: docker-$t@$i) \ 135ab698a4dSAlex Bennée $(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \ 136324027c2SFam Zheng ) \ 137324027c2SFam Zheng $(foreach t,$(DOCKER_TESTS), \ 138ff1a3902SAlex Bennée $(eval docker-all-tests: docker-$t@$i) \ 139ff1a3902SAlex Bennée $(eval docker-$t: docker-$t@$i) \ 140324027c2SFam Zheng ) \ 141324027c2SFam Zheng) 142324027c2SFam Zheng 143324027c2SFam Zhengdocker: 144c6919250SAlex Bennée @echo 'Build QEMU and run tests inside $(RUNC) containers' 145324027c2SFam Zheng @echo 146324027c2SFam Zheng @echo 'Available targets:' 147324027c2SFam Zheng @echo 148324027c2SFam Zheng @echo ' docker: Print this help.' 149ff1a3902SAlex Bennée @echo ' docker-all-tests: Run all image/test combinations.' 15087db9018SWainer dos Santos Moschetta @echo ' docker-TEST: Run "TEST" on all image combinations.' 151324027c2SFam Zheng @echo ' docker-clean: Kill and remove residual docker testing containers.' 152324027c2SFam Zheng @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".' 153324027c2SFam Zheng @echo ' Note: "TEST" is one of the listed test name,' 154324027c2SFam Zheng @echo ' or a script name under $$QEMU_SRC/tests/docker/;' 15587db9018SWainer dos Santos Moschetta @echo ' "IMAGE" is one of the listed container name.' 156324027c2SFam Zheng @echo ' docker-image: Build all images.' 157324027c2SFam Zheng @echo ' docker-image-IMAGE: Build image "IMAGE".' 15887db9018SWainer dos Santos Moschetta @echo ' docker-run: For manually running a "TEST" with "IMAGE".' 159324027c2SFam Zheng @echo 160324027c2SFam Zheng @echo 'Available container images:' 161324027c2SFam Zheng @echo ' $(DOCKER_IMAGES)' 162324027c2SFam Zheng @echo 163324027c2SFam Zheng @echo 'Available tests:' 164324027c2SFam Zheng @echo ' $(DOCKER_TESTS)' 165324027c2SFam Zheng @echo 166324027c2SFam Zheng @echo 'Special variables:' 167324027c2SFam Zheng @echo ' TARGET_LIST=a,b,c Override target list in builds.' 16835e0f959SFam Zheng @echo ' EXTRA_CONFIGURE_OPTS="..."' 16935e0f959SFam Zheng @echo ' Extra configure options.' 170e558220dSAlex Bennée @echo ' TEST_COMMAND="..." Override the default `make check` target.' 171be20302aSAlex Bennée @echo ' IMAGES="a b c ..": Restrict available images to subset.' 172be20302aSAlex Bennée @echo ' TESTS="x y z .." Restrict available tests to subset.' 173324027c2SFam Zheng @echo ' J=[0..9]* Overrides the -jN parameter for make commands' 174324027c2SFam Zheng @echo ' (default is 1)' 175324027c2SFam Zheng @echo ' DEBUG=1 Stop and drop to shell in the created container' 176324027c2SFam Zheng @echo ' before running the command.' 1778a2390a4SDaniel P. Berrange @echo ' NETWORK=1 Enable virtual network interface with default backend.' 1786423795eSPhilippe Mathieu-Daudé @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.' 179dc23bbc3SPhilippe Mathieu-Daudé @echo ' NOUSER=1 Define to disable adding current user to containers passwd.' 180324027c2SFam Zheng @echo ' NOCACHE=1 Ignore cache when build images.' 181b7c851b2SAlex Bennée @echo ' EXECUTABLE=<path> Include executable in image.' 1824c84f662SPhilippe Mathieu-Daudé @echo ' EXTRA_FILES="<path> [... <path>]"' 1834c84f662SPhilippe Mathieu-Daudé @echo ' Include extra files in image.' 184e6f1306bSAlex Bennée @echo ' REGISTRY=url Cache builds from registry (default:$(DOCKER_REGISTRY))' 185324027c2SFam Zheng 1866147c249SAlex Bennéedocker-help: docker 1876147c249SAlex Bennée 18879bd3ae9SPaolo Bonzini# Use a global constant ccache directory to speed up repetitive builds 18979bd3ae9SPaolo BonziniDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache 19079bd3ae9SPaolo Bonzini 191e86c9a64SAlex Bennée# This rule if for directly running against an arbitrary docker target. 192e86c9a64SAlex Bennée# It is called by the expanded docker targets (e.g. make 193d39eaa22SPaolo Bonzini# docker-test-foo@bar) which will also ensure the image is up to date. 194e86c9a64SAlex Bennée# 195e86c9a64SAlex Bennée# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64 196e86c9a64SAlex Bennée# 197e86c9a64SAlex Bennéedocker-run: docker-qemu-src 198324027c2SFam Zheng @mkdir -p "$(DOCKER_CCACHE_DIR)" 199e86c9a64SAlex Bennée @if test -z "$(IMAGE)" || test -z "$(TEST)"; \ 200e86c9a64SAlex Bennée then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \ 201324027c2SFam Zheng fi 202e86c9a64SAlex Bennée $(if $(EXECUTABLE), \ 203324027c2SFam Zheng $(call quiet-command, \ 204e27cae26SAlex Bennée $(DOCKER_SCRIPT) update \ 2058d628d07SAlex Bennée $(IMAGE) --executable $(EXECUTABLE), \ 206e86c9a64SAlex Bennée " COPYING $(EXECUTABLE) to $(IMAGE)")) 207e86c9a64SAlex Bennée $(call quiet-command, \ 208789bc541SAlex Bennée $(RUNC) run \ 209789bc541SAlex Bennée --rm \ 210*aa8246d8SAkihiko Odaki $(if $(NOUSER),, \ 211*aa8246d8SAkihiko Odaki $(if $(filter docker,$(RUNC)), \ 212*aa8246d8SAkihiko Odaki -u $(UID), \ 213*aa8246d8SAkihiko Odaki --userns keep-id \ 214*aa8246d8SAkihiko Odaki ) \ 215*aa8246d8SAkihiko Odaki ) \ 21644707491SFam Zheng --security-opt seccomp=unconfined \ 2174b2c6bc5SFam Zheng $(if $(DEBUG),-ti,) \ 2188a2390a4SDaniel P. Berrange $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \ 2195609c512SAlex Bennée -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \ 220e86c9a64SAlex Bennée -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \ 221e558220dSAlex Bennée -e TEST_COMMAND="$(TEST_COMMAND)" \ 222e86c9a64SAlex Bennée -e V=$V -e J=$J -e DEBUG=$(DEBUG) \ 223e86c9a64SAlex Bennée -e SHOW_ENV=$(SHOW_ENV) \ 22436ac78e6SFam Zheng $(if $(NOUSER),, \ 225324027c2SFam Zheng -e CCACHE_DIR=/var/tmp/ccache \ 226324027c2SFam Zheng -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \ 22736ac78e6SFam Zheng ) \ 22836ac78e6SFam Zheng -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \ 229e86c9a64SAlex Bennée $(IMAGE) \ 230c8158513SFam Zheng /var/tmp/qemu/run \ 231e86c9a64SAlex Bennée $(TEST), " RUN $(TEST) in ${IMAGE}") 23208c3f6d5SPeter Xu $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \ 23308c3f6d5SPeter Xu " CLEANUP $(DOCKER_SRC_COPY)") 234e86c9a64SAlex Bennée 23579bd3ae9SPaolo Bonzinidocker-image: ${DOCKER_IMAGES:%=docker-image-%} 23679bd3ae9SPaolo Bonzini 237324027c2SFam Zhengdocker-clean: 238e27cae26SAlex Bennée $(call quiet-command, $(DOCKER_SCRIPT) clean) 239