xref: /openbmc/qemu/tests/docker/Makefile.include (revision b70ec50b)
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
1705af039dSMarc-André LureauENGINE := 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
729d5e546aSPhilippe Mathieu-Daudé# Enforce dependencies for composite images
7379bd3ae9SPaolo Bonzini# we don't run tests on intermediate images (used as base by another image)
7479bd3ae9SPaolo BonziniDOCKER_PARTIAL_IMAGES := debian10 debian11
7506746324SAlex Bennéeifeq ($(HOST_ARCH),x86_64)
76698a71edSAlex Bennéedocker-image-debian-amd64: docker-image-debian10
77ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64-cross
78ec22b726SAlex Bennéeelse
79ec22b726SAlex Bennéedocker-image-debian-amd64-cross: docker-image-debian10
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)
85e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mips-cross 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
91e3386c27SAlex Bennéedocker-image-debian-alpha-cross: docker-image-debian10
92c268700bSAlex Bennéedocker-image-debian-hppa-cross: docker-image-debian10
9320f8b1a2SAlex Bennéedocker-image-debian-m68k-cross: docker-image-debian10
94698a71edSAlex Bennéedocker-image-debian-mips-cross: docker-image-debian10
954575a701SAlex Bennéedocker-image-debian-mips64-cross: docker-image-debian10
96923984c5SAlex Bennéedocker-image-debian-sh4-cross: docker-image-debian10
9778d1d696SAlex Bennéedocker-image-debian-sparc64-cross: docker-image-debian10
9818b6be43SAlex Bennée
9917888749SAlex Bennée# The native build should never use the registry
10017888749SAlex Bennéedocker-image-debian-native: DOCKER_REGISTRY=
10117888749SAlex Bennée
102d47e3751SAlex Bennée# base images should not add a local user
103d47e3751SAlex Bennéedocker-image-debian10: NOUSER=1
104d47e3751SAlex Bennéedocker-image-debian11: NOUSER=1
10517888749SAlex Bennée
1065dbefb87SAlex Bennée# alpine has no adduser
1075dbefb87SAlex Bennéedocker-image-alpine: NOUSER=1
1085dbefb87SAlex Bennée
1092eb4461aSRichard Hendersondebian-toolchain-run = \
1102eb4461aSRichard Henderson	$(if $(NOCACHE), 						\
1112eb4461aSRichard Henderson		$(call quiet-command,					\
1122eb4461aSRichard Henderson			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
1132eb4461aSRichard Henderson			$(if $V,,--quiet) --no-cache 			\
1142eb4461aSRichard Henderson			--registry $(DOCKER_REGISTRY) --extra-files	\
1152eb4461aSRichard Henderson			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
1162eb4461aSRichard Henderson			"BUILD", $1),				        \
1172eb4461aSRichard Henderson		$(call quiet-command,					\
1182eb4461aSRichard Henderson			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
1192eb4461aSRichard Henderson				qemu/$1 $(DOCKER_REGISTRY),		\
1202eb4461aSRichard Henderson			"FETCH", $1)					\
1212eb4461aSRichard Henderson		$(call quiet-command,					\
1222eb4461aSRichard Henderson			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
1232eb4461aSRichard Henderson				qemu/$1 				\
1242eb4461aSRichard Henderson				$(if $(NOUSER),,--add-current-user) 	\
1252eb4461aSRichard Henderson			"PREPARE", $1))
1262eb4461aSRichard Hendersondebian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
1272eb4461aSRichard Henderson
128426045aeSPaolo Bonzinidocker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker \
129426045aeSPaolo Bonzini	$(DOCKER_FILES_DIR)/debian-hexagon-cross.d/build-toolchain.sh
130426045aeSPaolo Bonzini	$(call debian-toolchain, $@)
131426045aeSPaolo Bonzini
132b58a4e68SRichard Hendersondocker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
133b58a4e68SRichard Henderson    $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
134b58a4e68SRichard Henderson	$(call debian-toolchain, $@)
135b58a4e68SRichard Henderson
1362eb4461aSRichard Hendersondocker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
1372eb4461aSRichard Henderson    $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
1382eb4461aSRichard Henderson	$(call debian-toolchain, $@)
1392eb4461aSRichard Henderson
14036dc5fedSPhilippe Mathieu-Daudé# Specialist build images, sometimes very limited tools
141cf63177eSThomas Huthdocker-image-debian-tricore-cross: docker-image-debian10
14227ebeda0SAlex Bennéedocker-image-debian-all-test-cross: docker-image-debian10
143*b70ec50bSRichard Hendersondocker-image-debian-loongarch-cross: docker-image-debian11
144b58a4e68SRichard Hendersondocker-image-debian-microblaze-cross: docker-image-debian10
1452eb4461aSRichard Hendersondocker-image-debian-nios2-cross: docker-image-debian10
146cf9b6d21SMatheus Ferstdocker-image-debian-powerpc-test-cross: docker-image-debian11
1477d5817a4SAlex Bennéedocker-image-debian-riscv64-test-cross: docker-image-debian11
14836dc5fedSPhilippe Mathieu-Daudé
14977b08f73SAlex Bennée# These images may be good enough for building tests but not for test builds
150ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-alpha-cross
151cf9b6d21SMatheus FerstDOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
152ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-hppa-cross
153*b70ec50bSRichard HendersonDOCKER_PARTIAL_IMAGES += debian-loongarch-cross
154ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
155b58a4e68SRichard HendersonDOCKER_PARTIAL_IMAGES += debian-microblaze-cross
1562eb4461aSRichard HendersonDOCKER_PARTIAL_IMAGES += debian-nios2-cross
1577d5817a4SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
158ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
159ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-tricore-cross
160ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-xtensa-cross
1616a2b0fd1SRichard HendersonDOCKER_PARTIAL_IMAGES += fedora-cris-cross
16277b08f73SAlex Bennée
16379bd3ae9SPaolo Bonzini# images that are only used to build other images
16479bd3ae9SPaolo BonziniDOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
16579bd3ae9SPaolo Bonzini
16679bd3ae9SPaolo Bonzini__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
16779bd3ae9SPaolo BonziniDOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
16879bd3ae9SPaolo Bonzini
16979bd3ae9SPaolo Bonzini__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
17079bd3ae9SPaolo BonziniDOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
17179bd3ae9SPaolo Bonzini
172324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination
173bbb5ba86SPaolo Bonzini$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
174d79f5c8bSDaniel P. Berrangé	$(foreach t,$(DOCKER_TESTS), \
175324027c2SFam Zheng		$(eval .PHONY: docker-$t@$i) \
176ab698a4dSAlex Bennée		$(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \
177324027c2SFam Zheng	) \
178324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS), \
179ff1a3902SAlex Bennée		$(eval docker-all-tests: docker-$t@$i) \
180ff1a3902SAlex Bennée		$(eval docker-$t: docker-$t@$i) \
181324027c2SFam Zheng	) \
182324027c2SFam Zheng)
183324027c2SFam Zheng
184324027c2SFam Zhengdocker:
18505af039dSMarc-André Lureau	@echo 'Build QEMU and run tests inside Docker or Podman containers'
186324027c2SFam Zheng	@echo
187324027c2SFam Zheng	@echo 'Available targets:'
188324027c2SFam Zheng	@echo
189324027c2SFam Zheng	@echo '    docker:              Print this help.'
190ff1a3902SAlex Bennée	@echo '    docker-all-tests:    Run all image/test combinations.'
19187db9018SWainer dos Santos Moschetta	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
192324027c2SFam Zheng	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
193324027c2SFam Zheng	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
194324027c2SFam Zheng	@echo '                         Note: "TEST" is one of the listed test name,'
195324027c2SFam Zheng	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
19687db9018SWainer dos Santos Moschetta	@echo '                         "IMAGE" is one of the listed container name.'
197324027c2SFam Zheng	@echo '    docker-image:        Build all images.'
198324027c2SFam Zheng	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
19987db9018SWainer dos Santos Moschetta	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
200324027c2SFam Zheng	@echo
201324027c2SFam Zheng	@echo 'Available container images:'
202324027c2SFam Zheng	@echo '    $(DOCKER_IMAGES)'
203324027c2SFam Zheng	@echo
204324027c2SFam Zheng	@echo 'Available tests:'
205324027c2SFam Zheng	@echo '    $(DOCKER_TESTS)'
206324027c2SFam Zheng	@echo
207324027c2SFam Zheng	@echo 'Special variables:'
208324027c2SFam Zheng	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
20935e0f959SFam Zheng	@echo '    EXTRA_CONFIGURE_OPTS="..."'
21035e0f959SFam Zheng	@echo '                         Extra configure options.'
211be20302aSAlex Bennée	@echo '    IMAGES="a b c ..":   Restrict available images to subset.'
212be20302aSAlex Bennée	@echo '    TESTS="x y z .."     Restrict available tests to subset.'
213324027c2SFam Zheng	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
214324027c2SFam Zheng	@echo '                         (default is 1)'
215324027c2SFam Zheng	@echo '    DEBUG=1              Stop and drop to shell in the created container'
216324027c2SFam Zheng	@echo '                         before running the command.'
2178a2390a4SDaniel P. Berrange	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
2186423795eSPhilippe Mathieu-Daudé	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
219dc23bbc3SPhilippe Mathieu-Daudé	@echo '    NOUSER=1             Define to disable adding current user to containers passwd.'
220324027c2SFam Zheng	@echo '    NOCACHE=1            Ignore cache when build images.'
221b7c851b2SAlex Bennée	@echo '    EXECUTABLE=<path>    Include executable in image.'
2224c84f662SPhilippe Mathieu-Daudé	@echo '    EXTRA_FILES="<path> [... <path>]"'
2234c84f662SPhilippe Mathieu-Daudé	@echo '                         Include extra files in image.'
22405af039dSMarc-André Lureau	@echo '    ENGINE=auto/docker/podman'
22505af039dSMarc-André Lureau	@echo '                         Specify which container engine to run.'
226e6f1306bSAlex Bennée	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
227324027c2SFam Zheng
2286147c249SAlex Bennéedocker-help: docker
2296147c249SAlex Bennée
23079bd3ae9SPaolo Bonzini# Use a global constant ccache directory to speed up repetitive builds
23179bd3ae9SPaolo BonziniDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
23279bd3ae9SPaolo Bonzini
233e86c9a64SAlex Bennée# This rule if for directly running against an arbitrary docker target.
234e86c9a64SAlex Bennée# It is called by the expanded docker targets (e.g. make
235d39eaa22SPaolo Bonzini# docker-test-foo@bar) which will also ensure the image is up to date.
236e86c9a64SAlex Bennée#
237e86c9a64SAlex Bennée# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
238e86c9a64SAlex Bennée#
239e86c9a64SAlex Bennéedocker-run: docker-qemu-src
240324027c2SFam Zheng	@mkdir -p "$(DOCKER_CCACHE_DIR)"
241e86c9a64SAlex Bennée	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
242e86c9a64SAlex Bennée		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
243324027c2SFam Zheng	fi
244e86c9a64SAlex Bennée	$(if $(EXECUTABLE),						\
245324027c2SFam Zheng		$(call quiet-command,					\
246e27cae26SAlex Bennée			$(DOCKER_SCRIPT) update 			\
2478d628d07SAlex Bennée			$(IMAGE) --executable $(EXECUTABLE),		\
248e86c9a64SAlex Bennée			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
249e86c9a64SAlex Bennée	$(call quiet-command,						\
250e27cae26SAlex Bennée		$(DOCKER_SCRIPT) run 					\
2512461d80eSMarc-André Lureau			$(if $(NOUSER),,--run-as-current-user) 		\
25244707491SFam Zheng			--security-opt seccomp=unconfined		\
2534b2c6bc5SFam Zheng			$(if $(DEBUG),-ti,)				\
2548a2390a4SDaniel P. Berrange			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
2555609c512SAlex Bennée			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
256e86c9a64SAlex Bennée			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
257e86c9a64SAlex Bennée			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
258e86c9a64SAlex Bennée			-e SHOW_ENV=$(SHOW_ENV) 			\
25936ac78e6SFam Zheng			$(if $(NOUSER),,				\
260324027c2SFam Zheng				-e CCACHE_DIR=/var/tmp/ccache 		\
261324027c2SFam Zheng				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
26236ac78e6SFam Zheng			)						\
26336ac78e6SFam Zheng			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
264e86c9a64SAlex Bennée			$(IMAGE) 					\
265c8158513SFam Zheng			/var/tmp/qemu/run 				\
266e86c9a64SAlex Bennée			$(TEST), "  RUN $(TEST) in ${IMAGE}")
26708c3f6d5SPeter Xu	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
26808c3f6d5SPeter Xu		"  CLEANUP $(DOCKER_SRC_COPY)")
269e86c9a64SAlex Bennée
27079bd3ae9SPaolo Bonzinidocker-image: ${DOCKER_IMAGES:%=docker-image-%}
27179bd3ae9SPaolo Bonzini
272324027c2SFam Zhengdocker-clean:
273e27cae26SAlex Bennée	$(call quiet-command, $(DOCKER_SCRIPT) clean)
274