xref: /openbmc/qemu/tests/docker/Makefile.include (revision c6919250)
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
19*c6919250SAlex BennéeRUNC ?= $(if $(shell command -v docker), docker, podman)
20*c6919250SAlex 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
9117888749SAlex Bennée# The native build should never use the registry
9217888749SAlex Bennéedocker-image-debian-native: DOCKER_REGISTRY=
9317888749SAlex Bennée
945dbefb87SAlex Bennée# alpine has no adduser
955dbefb87SAlex Bennéedocker-image-alpine: NOUSER=1
965dbefb87SAlex Bennée
972eb4461aSRichard Hendersondebian-toolchain-run = \
982eb4461aSRichard Henderson	$(if $(NOCACHE), 						\
992eb4461aSRichard Henderson		$(call quiet-command,					\
1002eb4461aSRichard Henderson			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
1012eb4461aSRichard Henderson			$(if $V,,--quiet) --no-cache 			\
1022eb4461aSRichard Henderson			--registry $(DOCKER_REGISTRY) --extra-files	\
1032eb4461aSRichard Henderson			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
1042eb4461aSRichard Henderson			"BUILD", $1),				        \
1052eb4461aSRichard Henderson		$(call quiet-command,					\
1062eb4461aSRichard Henderson			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
1072eb4461aSRichard Henderson				qemu/$1 $(DOCKER_REGISTRY),		\
1082eb4461aSRichard Henderson			"FETCH", $1)					\
1092eb4461aSRichard Henderson		$(call quiet-command,					\
1102eb4461aSRichard Henderson			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
1112eb4461aSRichard Henderson				qemu/$1 				\
1122eb4461aSRichard Henderson				$(if $(NOUSER),,--add-current-user) 	\
1132eb4461aSRichard Henderson			"PREPARE", $1))
1142eb4461aSRichard Hendersondebian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
1152eb4461aSRichard Henderson
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-xtensa-cross
1366a2b0fd1SRichard HendersonDOCKER_PARTIAL_IMAGES += fedora-cris-cross
13777b08f73SAlex Bennée
13879bd3ae9SPaolo Bonzini# images that are only used to build other images
13979bd3ae9SPaolo BonziniDOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
14079bd3ae9SPaolo Bonzini
14179bd3ae9SPaolo Bonzini__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
14279bd3ae9SPaolo BonziniDOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
14379bd3ae9SPaolo Bonzini
14479bd3ae9SPaolo Bonzini__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
14579bd3ae9SPaolo BonziniDOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
14679bd3ae9SPaolo Bonzini
147324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination
148bbb5ba86SPaolo Bonzini$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
149d79f5c8bSDaniel P. Berrangé	$(foreach t,$(DOCKER_TESTS), \
150324027c2SFam Zheng		$(eval .PHONY: docker-$t@$i) \
151ab698a4dSAlex Bennée		$(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \
152324027c2SFam Zheng	) \
153324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS), \
154ff1a3902SAlex Bennée		$(eval docker-all-tests: docker-$t@$i) \
155ff1a3902SAlex Bennée		$(eval docker-$t: docker-$t@$i) \
156324027c2SFam Zheng	) \
157324027c2SFam Zheng)
158324027c2SFam Zheng
159324027c2SFam Zhengdocker:
160*c6919250SAlex Bennée	@echo 'Build QEMU and run tests inside $(RUNC) containers'
161324027c2SFam Zheng	@echo
162324027c2SFam Zheng	@echo 'Available targets:'
163324027c2SFam Zheng	@echo
164324027c2SFam Zheng	@echo '    docker:              Print this help.'
165ff1a3902SAlex Bennée	@echo '    docker-all-tests:    Run all image/test combinations.'
16687db9018SWainer dos Santos Moschetta	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
167324027c2SFam Zheng	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
168324027c2SFam Zheng	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
169324027c2SFam Zheng	@echo '                         Note: "TEST" is one of the listed test name,'
170324027c2SFam Zheng	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
17187db9018SWainer dos Santos Moschetta	@echo '                         "IMAGE" is one of the listed container name.'
172324027c2SFam Zheng	@echo '    docker-image:        Build all images.'
173324027c2SFam Zheng	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
17487db9018SWainer dos Santos Moschetta	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
175324027c2SFam Zheng	@echo
176324027c2SFam Zheng	@echo 'Available container images:'
177324027c2SFam Zheng	@echo '    $(DOCKER_IMAGES)'
178324027c2SFam Zheng	@echo
179324027c2SFam Zheng	@echo 'Available tests:'
180324027c2SFam Zheng	@echo '    $(DOCKER_TESTS)'
181324027c2SFam Zheng	@echo
182324027c2SFam Zheng	@echo 'Special variables:'
183324027c2SFam Zheng	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
18435e0f959SFam Zheng	@echo '    EXTRA_CONFIGURE_OPTS="..."'
18535e0f959SFam Zheng	@echo '                         Extra configure options.'
186e558220dSAlex Bennée	@echo '    TEST_COMMAND="..."   Override the default `make check` target.'
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.'
200e6f1306bSAlex Bennée	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
201324027c2SFam Zheng
2026147c249SAlex Bennéedocker-help: docker
2036147c249SAlex Bennée
20479bd3ae9SPaolo Bonzini# Use a global constant ccache directory to speed up repetitive builds
20579bd3ae9SPaolo BonziniDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
20679bd3ae9SPaolo Bonzini
207e86c9a64SAlex Bennée# This rule if for directly running against an arbitrary docker target.
208e86c9a64SAlex Bennée# It is called by the expanded docker targets (e.g. make
209d39eaa22SPaolo Bonzini# docker-test-foo@bar) which will also ensure the image is up to date.
210e86c9a64SAlex Bennée#
211e86c9a64SAlex Bennée# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
212e86c9a64SAlex Bennée#
213e86c9a64SAlex Bennéedocker-run: docker-qemu-src
214324027c2SFam Zheng	@mkdir -p "$(DOCKER_CCACHE_DIR)"
215e86c9a64SAlex Bennée	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
216e86c9a64SAlex Bennée		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
217324027c2SFam Zheng	fi
218e86c9a64SAlex Bennée	$(if $(EXECUTABLE),						\
219324027c2SFam Zheng		$(call quiet-command,					\
220e27cae26SAlex Bennée			$(DOCKER_SCRIPT) update 			\
2218d628d07SAlex Bennée			$(IMAGE) --executable $(EXECUTABLE),		\
222e86c9a64SAlex Bennée			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
223e86c9a64SAlex Bennée	$(call quiet-command,						\
224789bc541SAlex Bennée		$(RUNC) run 						\
225789bc541SAlex Bennée			--rm						\
226789bc541SAlex Bennée			$(if $(NOUSER),,-u $(UID)) 			\
22744707491SFam Zheng			--security-opt seccomp=unconfined		\
2284b2c6bc5SFam Zheng			$(if $(DEBUG),-ti,)				\
2298a2390a4SDaniel P. Berrange			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
2305609c512SAlex Bennée			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
231e86c9a64SAlex Bennée			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
232e558220dSAlex Bennée			-e TEST_COMMAND="$(TEST_COMMAND)" 		\
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