xref: /openbmc/qemu/tests/docker/Makefile.include (revision c4575b59)
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))
1006746324SAlex Bennée
11324027c2SFam ZhengDOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
1234019198SAlex Bennéeifeq ($(HOST_ARCH),x86_64)
1334019198SAlex BennéeDOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu
1434019198SAlex Bennéeendif
1534019198SAlex BennéeDOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY))
16324027c2SFam Zheng
17*c4575b59SPaolo BonziniENGINE ?= auto
1805af039dSMarc-André LureauDOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
19e27cae26SAlex Bennée
20324027c2SFam ZhengCUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
21bc739b8eSPaolo BonziniDOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
22324027c2SFam Zheng
23b28ef6b9SPhilippe Mathieu-Daudé.DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
24324027c2SFam Zheng$(DOCKER_SRC_COPY):
25324027c2SFam Zheng	@mkdir $@
2673fb4f1dSFam Zheng	$(if $(SRC_ARCHIVE), \
2773fb4f1dSFam Zheng		$(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
2873fb4f1dSFam Zheng			"CP", "$@/qemu.tar"), \
29bc739b8eSPaolo Bonzini		$(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
3073fb4f1dSFam Zheng			"GEN", "$@/qemu.tar"))
31324027c2SFam Zheng	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
320bdb12c7SPeter Maydell		"COPY","RUNNER")
33324027c2SFam Zheng
34324027c2SFam Zhengdocker-qemu-src: $(DOCKER_SRC_COPY)
35324027c2SFam Zheng
3677162849SPaolo Bonzini# General rule for building docker images.
37324027c2SFam Zhengdocker-image-%: $(DOCKER_FILES_DIR)/%.docker
38324027c2SFam Zheng	$(call quiet-command,\
39767b6bd2SAlex Bennée		$(DOCKER_SCRIPT) build -t qemu/$* -f $< \
40e6f1306bSAlex Bennée		$(if $V,,--quiet) \
41e6f1306bSAlex Bennée		$(if $(NOCACHE),--no-cache, \
42e6f1306bSAlex Bennée			$(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
43414a8ce5SAlex Bennée		$(if $(NOUSER),,--add-current-user) \
444c84f662SPhilippe Mathieu-Daudé		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
45b7c851b2SAlex Bennée		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
460bdb12c7SPeter Maydell		"BUILD","$*")
47dc338fdbSAlex Bennée
48dc338fdbSAlex Bennée# Special rule for debootstraped binfmt linux-user images
49dc338fdbSAlex Bennéedocker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
50dc338fdbSAlex Bennée	$(if $(EXECUTABLE),,\
51dc338fdbSAlex Bennée		$(error EXECUTABLE not set, debootstrap of debian-$* would fail))
52dc338fdbSAlex Bennée	$(if $(DEB_ARCH),,\
53dc338fdbSAlex Bennée		$(error DEB_ARCH not set, debootstrap of debian-$* would fail))
54dc338fdbSAlex Bennée	$(if $(DEB_TYPE),,\
55dc338fdbSAlex Bennée		$(error DEB_TYPE not set, debootstrap of debian-$* would fail))
5627963460SAlex Bennée	$(if $(wildcard $(EXECUTABLE)),						\
57dc338fdbSAlex Bennée		$(call quiet-command,						\
58dc338fdbSAlex Bennée			DEB_ARCH=$(DEB_ARCH)					\
59dc338fdbSAlex Bennée			DEB_TYPE=$(DEB_TYPE) 					\
604d26c7feSAlex Bennée			$(if $(DEB_URL),DEB_URL=$(DEB_URL),)			\
61182ec060SAlex Bennée			$(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< 		\
62dc338fdbSAlex Bennée			$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) 		\
63dc338fdbSAlex Bennée			$(if $(NOUSER),,--add-current-user) 			\
64dc338fdbSAlex Bennée			$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))	\
65dc338fdbSAlex Bennée			$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
6619c9a18fSAlex Bennée			"BUILD","binfmt debian-$* (debootstrapped)"),		\
6719c9a18fSAlex Bennée		$(call quiet-command,						\
68767b6bd2SAlex Bennée			$(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || 	\
69d19f5fc0SAlex Bennée			{ echo "You will need to build $(EXECUTABLE)"; exit 1;},\
7019c9a18fSAlex Bennée			"CHECK", "debian-$* exists"))
71dc338fdbSAlex Bennée
72d996f0aeSAlex Bennée# Special case cross-compiling x86_64 on non-x86_64 systems.
7306746324SAlex Bennéeifeq ($(HOST_ARCH),x86_64)
74ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64-cross
75ec22b726SAlex Bennéeelse
76ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64
77ec22b726SAlex Bennéeendif
78409c1c9cSAlex Bennée
79e5389e44SAlex Bennée# For non-x86 hosts not all cross-compilers have been packaged
8006746324SAlex Bennéeifneq ($(HOST_ARCH),x86_64)
8154ab3c3fSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mipsel-cross debian-mips64el-cross
82e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
83e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-s390x-cross
84d79f5c8bSDaniel P. BerrangéDOCKER_PARTIAL_IMAGES += fedora
85e5389e44SAlex Bennéeendif
86e5389e44SAlex Bennée
8717888749SAlex Bennée# The native build should never use the registry
8817888749SAlex Bennéedocker-image-debian-native: DOCKER_REGISTRY=
8917888749SAlex Bennée
905dbefb87SAlex Bennée# alpine has no adduser
915dbefb87SAlex Bennéedocker-image-alpine: NOUSER=1
925dbefb87SAlex Bennée
932eb4461aSRichard Hendersondebian-toolchain-run = \
942eb4461aSRichard Henderson	$(if $(NOCACHE), 						\
952eb4461aSRichard Henderson		$(call quiet-command,					\
962eb4461aSRichard Henderson			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
972eb4461aSRichard Henderson			$(if $V,,--quiet) --no-cache 			\
982eb4461aSRichard Henderson			--registry $(DOCKER_REGISTRY) --extra-files	\
992eb4461aSRichard Henderson			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
1002eb4461aSRichard Henderson			"BUILD", $1),				        \
1012eb4461aSRichard Henderson		$(call quiet-command,					\
1022eb4461aSRichard Henderson			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
1032eb4461aSRichard Henderson				qemu/$1 $(DOCKER_REGISTRY),		\
1042eb4461aSRichard Henderson			"FETCH", $1)					\
1052eb4461aSRichard Henderson		$(call quiet-command,					\
1062eb4461aSRichard Henderson			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
1072eb4461aSRichard Henderson				qemu/$1 				\
1082eb4461aSRichard Henderson				$(if $(NOUSER),,--add-current-user) 	\
1092eb4461aSRichard Henderson			"PREPARE", $1))
1102eb4461aSRichard Hendersondebian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
1112eb4461aSRichard Henderson
112426045aeSPaolo Bonzinidocker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker \
113426045aeSPaolo Bonzini	$(DOCKER_FILES_DIR)/debian-hexagon-cross.d/build-toolchain.sh
114426045aeSPaolo Bonzini	$(call debian-toolchain, $@)
115426045aeSPaolo Bonzini
116b58a4e68SRichard Hendersondocker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
117b58a4e68SRichard Henderson    $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
118b58a4e68SRichard Henderson	$(call debian-toolchain, $@)
119b58a4e68SRichard Henderson
1202eb4461aSRichard Hendersondocker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
1212eb4461aSRichard Henderson    $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
1222eb4461aSRichard Henderson	$(call debian-toolchain, $@)
1232eb4461aSRichard Henderson
12477b08f73SAlex Bennée# These images may be good enough for building tests but not for test builds
125ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-alpha-cross
126cf9b6d21SMatheus FerstDOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
127ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-hppa-cross
128b70ec50bSRichard HendersonDOCKER_PARTIAL_IMAGES += debian-loongarch-cross
129ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
130b58a4e68SRichard HendersonDOCKER_PARTIAL_IMAGES += debian-microblaze-cross
13154ab3c3fSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mips-cross
1322eb4461aSRichard HendersonDOCKER_PARTIAL_IMAGES += debian-nios2-cross
1337d5817a4SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
134ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
135ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-tricore-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.'
187be20302aSAlex Bennée	@echo '    IMAGES="a b c ..":   Restrict available images to subset.'
188be20302aSAlex Bennée	@echo '    TESTS="x y z .."     Restrict available tests to subset.'
189324027c2SFam Zheng	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
190324027c2SFam Zheng	@echo '                         (default is 1)'
191324027c2SFam Zheng	@echo '    DEBUG=1              Stop and drop to shell in the created container'
192324027c2SFam Zheng	@echo '                         before running the command.'
1938a2390a4SDaniel P. Berrange	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
1946423795eSPhilippe Mathieu-Daudé	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
195dc23bbc3SPhilippe Mathieu-Daudé	@echo '    NOUSER=1             Define to disable adding current user to containers passwd.'
196324027c2SFam Zheng	@echo '    NOCACHE=1            Ignore cache when build images.'
197b7c851b2SAlex Bennée	@echo '    EXECUTABLE=<path>    Include executable in image.'
1984c84f662SPhilippe Mathieu-Daudé	@echo '    EXTRA_FILES="<path> [... <path>]"'
1994c84f662SPhilippe Mathieu-Daudé	@echo '                         Include extra files in image.'
20005af039dSMarc-André Lureau	@echo '    ENGINE=auto/docker/podman'
20105af039dSMarc-André Lureau	@echo '                         Specify which container engine to run.'
202e6f1306bSAlex Bennée	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
203324027c2SFam Zheng
2046147c249SAlex Bennéedocker-help: docker
2056147c249SAlex Bennée
20679bd3ae9SPaolo Bonzini# Use a global constant ccache directory to speed up repetitive builds
20779bd3ae9SPaolo BonziniDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
20879bd3ae9SPaolo Bonzini
209e86c9a64SAlex Bennée# This rule if for directly running against an arbitrary docker target.
210e86c9a64SAlex Bennée# It is called by the expanded docker targets (e.g. make
211d39eaa22SPaolo Bonzini# docker-test-foo@bar) which will also ensure the image is up to date.
212e86c9a64SAlex Bennée#
213e86c9a64SAlex Bennée# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
214e86c9a64SAlex Bennée#
215e86c9a64SAlex Bennéedocker-run: docker-qemu-src
216324027c2SFam Zheng	@mkdir -p "$(DOCKER_CCACHE_DIR)"
217e86c9a64SAlex Bennée	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
218e86c9a64SAlex Bennée		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
219324027c2SFam Zheng	fi
220e86c9a64SAlex Bennée	$(if $(EXECUTABLE),						\
221324027c2SFam Zheng		$(call quiet-command,					\
222e27cae26SAlex Bennée			$(DOCKER_SCRIPT) update 			\
2238d628d07SAlex Bennée			$(IMAGE) --executable $(EXECUTABLE),		\
224e86c9a64SAlex Bennée			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
225e86c9a64SAlex Bennée	$(call quiet-command,						\
226e27cae26SAlex Bennée		$(DOCKER_SCRIPT) run 					\
2272461d80eSMarc-André Lureau			$(if $(NOUSER),,--run-as-current-user) 		\
22844707491SFam Zheng			--security-opt seccomp=unconfined		\
2294b2c6bc5SFam Zheng			$(if $(DEBUG),-ti,)				\
2308a2390a4SDaniel P. Berrange			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
2315609c512SAlex Bennée			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
232e86c9a64SAlex Bennée			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
233e86c9a64SAlex Bennée			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
234e86c9a64SAlex Bennée			-e SHOW_ENV=$(SHOW_ENV) 			\
23536ac78e6SFam Zheng			$(if $(NOUSER),,				\
236324027c2SFam Zheng				-e CCACHE_DIR=/var/tmp/ccache 		\
237324027c2SFam Zheng				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
23836ac78e6SFam Zheng			)						\
23936ac78e6SFam Zheng			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
240e86c9a64SAlex Bennée			$(IMAGE) 					\
241c8158513SFam Zheng			/var/tmp/qemu/run 				\
242e86c9a64SAlex Bennée			$(TEST), "  RUN $(TEST) in ${IMAGE}")
24308c3f6d5SPeter Xu	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
24408c3f6d5SPeter Xu		"  CLEANUP $(DOCKER_SRC_COPY)")
245e86c9a64SAlex Bennée
24679bd3ae9SPaolo Bonzinidocker-image: ${DOCKER_IMAGES:%=docker-image-%}
24779bd3ae9SPaolo Bonzini
248324027c2SFam Zhengdocker-clean:
249e27cae26SAlex Bennée	$(call quiet-command, $(DOCKER_SCRIPT) clean)
250