xref: /openbmc/qemu/tests/docker/Makefile.include (revision 182ec060)
1324027c2SFam Zheng# Makefile for Docker tests
2324027c2SFam Zheng
3324027c2SFam Zheng.PHONY: docker docker-test docker-clean docker-image docker-qemu-src
4324027c2SFam Zheng
506746324SAlex BennéeHOST_ARCH = $(if $(ARCH),$(ARCH),$(shell uname -m))
606746324SAlex Bennée
7324027c2SFam ZhengDOCKER_SUFFIX := .docker
8324027c2SFam ZhengDOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
9272e551bSPhilippe Mathieu-Daudé# we don't run tests on intermediate images (used as base by another image)
10086f269cSAlex BennéeDOCKER_PARTIAL_IMAGES := debian9 debian10 debian11
11dad47814SJohn SnowDOCKER_PARTIAL_IMAGES += debian9-mxe debian-bootstrap
12ef8f3283SJohn SnowDOCKER_IMAGES := $(sort $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))
13324027c2SFam ZhengDOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
14324027c2SFam Zheng# Use a global constant ccache directory to speed up repetitive builds
15324027c2SFam ZhengDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
16e6f1306bSAlex BennéeDOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),registry.gitlab.com/qemu-project/qemu)
17324027c2SFam Zheng
18324027c2SFam ZhengDOCKER_TESTS := $(notdir $(shell \
19324027c2SFam Zheng	find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
20324027c2SFam Zheng
21324027c2SFam ZhengDOCKER_TOOLS := travis
22324027c2SFam Zheng
2305af039dSMarc-André LureauENGINE := auto
2405af039dSMarc-André Lureau
2505af039dSMarc-André LureauDOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
26e27cae26SAlex Bennée
27324027c2SFam ZhengTESTS ?= %
28324027c2SFam ZhengIMAGES ?= %
29324027c2SFam Zheng
30324027c2SFam ZhengCUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
31bc739b8eSPaolo BonziniDOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
32324027c2SFam Zheng
33b28ef6b9SPhilippe Mathieu-Daudé.DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
34324027c2SFam Zheng$(DOCKER_SRC_COPY):
35324027c2SFam Zheng	@mkdir $@
3673fb4f1dSFam Zheng	$(if $(SRC_ARCHIVE), \
3773fb4f1dSFam Zheng		$(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
3873fb4f1dSFam Zheng			"CP", "$@/qemu.tar"), \
39bc739b8eSPaolo Bonzini		$(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
4073fb4f1dSFam Zheng			"GEN", "$@/qemu.tar"))
41324027c2SFam Zheng	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
420bdb12c7SPeter Maydell		"COPY","RUNNER")
43324027c2SFam Zheng
44324027c2SFam Zhengdocker-qemu-src: $(DOCKER_SRC_COPY)
45324027c2SFam Zheng
46324027c2SFam Zhengdocker-image: ${DOCKER_TARGETS}
47324027c2SFam Zheng
48cff54773SAlex Bennée# General rule for building docker images. If we are a sub-make
49cff54773SAlex Bennée# invoked with SKIP_DOCKER_BUILD we still check the image is up to date
50cff54773SAlex Bennée# though
51cff54773SAlex Bennéeifdef SKIP_DOCKER_BUILD
52cff54773SAlex Bennéedocker-image-%: $(DOCKER_FILES_DIR)/%.docker
53cff54773SAlex Bennée	$(call quiet-command, \
54767b6bd2SAlex Bennée		$(DOCKER_SCRIPT) check --quiet qemu/$* $<, \
55cff54773SAlex Bennée		"CHECK", "$*")
56cff54773SAlex Bennéeelse
57324027c2SFam Zhengdocker-image-%: $(DOCKER_FILES_DIR)/%.docker
58324027c2SFam Zheng	$(call quiet-command,\
59767b6bd2SAlex Bennée		$(DOCKER_SCRIPT) build -t qemu/$* -f $< \
60e6f1306bSAlex Bennée		$(if $V,,--quiet) \
61e6f1306bSAlex Bennée		$(if $(NOCACHE),--no-cache, \
62e6f1306bSAlex Bennée			$(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
63414a8ce5SAlex Bennée		$(if $(NOUSER),,--add-current-user) \
644c84f662SPhilippe Mathieu-Daudé		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
65b7c851b2SAlex Bennée		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
660bdb12c7SPeter Maydell		"BUILD","$*")
67dc338fdbSAlex Bennée
68dc338fdbSAlex Bennée# Special rule for debootstraped binfmt linux-user images
69dc338fdbSAlex Bennéedocker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
70dc338fdbSAlex Bennée	$(if $(EXECUTABLE),,\
71dc338fdbSAlex Bennée		$(error EXECUTABLE not set, debootstrap of debian-$* would fail))
72dc338fdbSAlex Bennée	$(if $(DEB_ARCH),,\
73dc338fdbSAlex Bennée		$(error DEB_ARCH not set, debootstrap of debian-$* would fail))
74dc338fdbSAlex Bennée	$(if $(DEB_TYPE),,\
75dc338fdbSAlex Bennée		$(error DEB_TYPE not set, debootstrap of debian-$* would fail))
7627963460SAlex Bennée	$(if $(wildcard $(EXECUTABLE)),						\
77dc338fdbSAlex Bennée		$(call quiet-command,						\
78dc338fdbSAlex Bennée			DEB_ARCH=$(DEB_ARCH)					\
79dc338fdbSAlex Bennée			DEB_TYPE=$(DEB_TYPE) 					\
804d26c7feSAlex Bennée			$(if $(DEB_URL),DEB_URL=$(DEB_URL),)			\
81*182ec060SAlex Bennée			$(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< 		\
82dc338fdbSAlex Bennée			$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) 		\
83dc338fdbSAlex Bennée			$(if $(NOUSER),,--add-current-user) 			\
84dc338fdbSAlex Bennée			$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))	\
85dc338fdbSAlex Bennée			$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
8619c9a18fSAlex Bennée			"BUILD","binfmt debian-$* (debootstrapped)"),		\
8719c9a18fSAlex Bennée		$(call quiet-command,						\
88767b6bd2SAlex Bennée			$(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || 	\
89d19f5fc0SAlex Bennée			{ echo "You will need to build $(EXECUTABLE)"; exit 1;},\
9019c9a18fSAlex Bennée			"CHECK", "debian-$* exists"))
91dc338fdbSAlex Bennée
92cff54773SAlex Bennéeendif
93324027c2SFam Zheng
949d5e546aSPhilippe Mathieu-Daudé# Enforce dependencies for composite images
9567bd36beSAlex Bennéedocker-image-debian9-mxe: docker-image-debian9
9606746324SAlex Bennéeifeq ($(HOST_ARCH),x86_64)
97698a71edSAlex Bennéedocker-image-debian-amd64: docker-image-debian10
98ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64-cross
99ec22b726SAlex Bennéeelse
100ec22b726SAlex Bennéedocker-image-debian-amd64-cross: docker-image-debian10
101ec22b726SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-amd64
102ec22b726SAlex Bennéeendif
10367bd36beSAlex Bennéedocker-image-debian-win32-cross: docker-image-debian9-mxe
10467bd36beSAlex Bennéedocker-image-debian-win64-cross: docker-image-debian9-mxe
105409c1c9cSAlex Bennée
106e5389e44SAlex Bennée# For non-x86 hosts not all cross-compilers have been packaged
10706746324SAlex Bennéeifneq ($(HOST_ARCH),x86_64)
108e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
109e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
110e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-s390x-cross
111e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += debian-win32-cross debian-win64-cross
112e5389e44SAlex BennéeDOCKER_PARTIAL_IMAGES += fedora travis
113e5389e44SAlex Bennéeendif
114e5389e44SAlex Bennée
115e3386c27SAlex Bennéedocker-image-debian-alpha-cross: docker-image-debian10
11618b6be43SAlex Bennéedocker-image-debian-arm64-cross: docker-image-debian10
117698a71edSAlex Bennéedocker-image-debian-armel-cross: docker-image-debian10
118698a71edSAlex Bennéedocker-image-debian-armhf-cross: docker-image-debian10
119c268700bSAlex Bennéedocker-image-debian-hppa-cross: docker-image-debian10
12020f8b1a2SAlex Bennéedocker-image-debian-m68k-cross: docker-image-debian10
121698a71edSAlex Bennéedocker-image-debian-mips-cross: docker-image-debian10
1224575a701SAlex Bennéedocker-image-debian-mips64-cross: docker-image-debian10
123698a71edSAlex Bennéedocker-image-debian-mips64el-cross: docker-image-debian10
124698a71edSAlex Bennéedocker-image-debian-mipsel-cross: docker-image-debian10
125ce494209SAlex Bennéedocker-image-debian-powerpc-cross: docker-image-debian10
1268a4daee5SAlex Bennéedocker-image-debian-ppc64-cross: docker-image-debian10
127698a71edSAlex Bennéedocker-image-debian-ppc64el-cross: docker-image-debian10
128c6e8f512SAlex Bennéedocker-image-debian-riscv64-cross: docker-image-debian10
129698a71edSAlex Bennéedocker-image-debian-s390x-cross: docker-image-debian10
130923984c5SAlex Bennéedocker-image-debian-sh4-cross: docker-image-debian10
13178d1d696SAlex Bennéedocker-image-debian-sparc64-cross: docker-image-debian10
13218b6be43SAlex Bennée
1331fddbf7cSAlex Bennéedocker-image-travis: NOUSER=1
13424e0131fSAlex Bennée
13536dc5fedSPhilippe Mathieu-Daudé# Specialist build images, sometimes very limited tools
136e035e6ffSAlex Bennéedocker-image-debian-tricore-cross: docker-image-debian9
13727ebeda0SAlex Bennéedocker-image-debian-all-test-cross: docker-image-debian10
138c729a99dSAlex Bennéedocker-image-debian-arm64-test-cross: docker-image-debian11
13936dc5fedSPhilippe Mathieu-Daudé
14077b08f73SAlex Bennée# These images may be good enough for building tests but not for test builds
141ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-alpha-cross
142c729a99dSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-arm64-test-cross
143ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-hppa-cross
144ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
145ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-powerpc-cross debian-ppc64-cross
146ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-riscv64-cross
147ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
148ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-tricore-cross
149ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += debian-xtensa-cross
150ac276a4bSAlex BennéeDOCKER_PARTIAL_IMAGES += fedora-i386-cross fedora-cris-cross
15177b08f73SAlex Bennée
15219c9a18fSAlex Bennée# Rules for building linux-user powered images
15319c9a18fSAlex Bennée#
15419c9a18fSAlex Bennée# These are slower than using native cross compiler setups but can
15519c9a18fSAlex Bennée# work around issues with poorly working multi-arch systems and broken
15619c9a18fSAlex Bennée# packages.
15719c9a18fSAlex Bennée
158324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination
159ef8f3283SJohn Snow$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
160324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
161324027c2SFam Zheng		$(eval .PHONY: docker-$t@$i) \
162324027c2SFam Zheng		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
163324027c2SFam Zheng	) \
164324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS), \
165ff1a3902SAlex Bennée		$(eval docker-all-tests: docker-$t@$i) \
166ff1a3902SAlex Bennée		$(eval docker-$t: docker-$t@$i) \
167324027c2SFam Zheng	) \
168324027c2SFam Zheng)
169324027c2SFam Zheng
170324027c2SFam Zhengdocker:
17105af039dSMarc-André Lureau	@echo 'Build QEMU and run tests inside Docker or Podman containers'
172324027c2SFam Zheng	@echo
173324027c2SFam Zheng	@echo 'Available targets:'
174324027c2SFam Zheng	@echo
175324027c2SFam Zheng	@echo '    docker:              Print this help.'
176ff1a3902SAlex Bennée	@echo '    docker-all-tests:    Run all image/test combinations.'
17787db9018SWainer dos Santos Moschetta	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
178324027c2SFam Zheng	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
179324027c2SFam Zheng	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
180324027c2SFam Zheng	@echo '                         Note: "TEST" is one of the listed test name,'
181324027c2SFam Zheng	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
18287db9018SWainer dos Santos Moschetta	@echo '                         "IMAGE" is one of the listed container name.'
183324027c2SFam Zheng	@echo '    docker-image:        Build all images.'
184324027c2SFam Zheng	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
18587db9018SWainer dos Santos Moschetta	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
186324027c2SFam Zheng	@echo
187324027c2SFam Zheng	@echo 'Available container images:'
188324027c2SFam Zheng	@echo '    $(DOCKER_IMAGES)'
18919c9a18fSAlex Bennéeifneq ($(DOCKER_USER_IMAGES),)
19019c9a18fSAlex Bennée	@echo
19119c9a18fSAlex Bennée	@echo 'Available linux-user images (docker-binfmt-image-debian-%):'
19219c9a18fSAlex Bennée	@echo '    $(DOCKER_USER_IMAGES)'
19319c9a18fSAlex Bennéeendif
194324027c2SFam Zheng	@echo
195324027c2SFam Zheng	@echo 'Available tests:'
196324027c2SFam Zheng	@echo '    $(DOCKER_TESTS)'
197324027c2SFam Zheng	@echo
198324027c2SFam Zheng	@echo 'Available tools:'
199324027c2SFam Zheng	@echo '    $(DOCKER_TOOLS)'
200324027c2SFam Zheng	@echo
201324027c2SFam Zheng	@echo 'Special variables:'
202324027c2SFam Zheng	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
20335e0f959SFam Zheng	@echo '    EXTRA_CONFIGURE_OPTS="..."'
20435e0f959SFam Zheng	@echo '                         Extra configure options.'
205324027c2SFam Zheng	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
206324027c2SFam Zheng	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
207324027c2SFam Zheng	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
208324027c2SFam Zheng	@echo '                         (default is 1)'
209324027c2SFam Zheng	@echo '    DEBUG=1              Stop and drop to shell in the created container'
210324027c2SFam Zheng	@echo '                         before running the command.'
2118a2390a4SDaniel P. Berrange	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
2126423795eSPhilippe Mathieu-Daudé	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
213414a8ce5SAlex Bennée	@echo '    NOUSER               Define to disable adding current user to containers passwd.'
214324027c2SFam Zheng	@echo '    NOCACHE=1            Ignore cache when build images.'
215b7c851b2SAlex Bennée	@echo '    EXECUTABLE=<path>    Include executable in image.'
2164c84f662SPhilippe Mathieu-Daudé	@echo '    EXTRA_FILES="<path> [... <path>]"'
2174c84f662SPhilippe Mathieu-Daudé	@echo '                         Include extra files in image.'
21805af039dSMarc-André Lureau	@echo '    ENGINE=auto/docker/podman'
21905af039dSMarc-André Lureau	@echo '                         Specify which container engine to run.'
220e6f1306bSAlex Bennée	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
221324027c2SFam Zheng
222e86c9a64SAlex Bennée# This rule if for directly running against an arbitrary docker target.
223e86c9a64SAlex Bennée# It is called by the expanded docker targets (e.g. make
224e86c9a64SAlex Bennée# docker-test-foo@bar) which will do additional verification.
225e86c9a64SAlex Bennée#
226e86c9a64SAlex Bennée# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
227e86c9a64SAlex Bennée#
228e86c9a64SAlex Bennéedocker-run: docker-qemu-src
229324027c2SFam Zheng	@mkdir -p "$(DOCKER_CCACHE_DIR)"
230e86c9a64SAlex Bennée	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
231e86c9a64SAlex Bennée		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
232324027c2SFam Zheng	fi
233e86c9a64SAlex Bennée	$(if $(EXECUTABLE),						\
234324027c2SFam Zheng		$(call quiet-command,					\
235e27cae26SAlex Bennée			$(DOCKER_SCRIPT) update 			\
236e86c9a64SAlex Bennée			$(IMAGE) $(EXECUTABLE),				\
237e86c9a64SAlex Bennée			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
238e86c9a64SAlex Bennée	$(call quiet-command,						\
239e27cae26SAlex Bennée		$(DOCKER_SCRIPT) run 					\
2402461d80eSMarc-André Lureau			$(if $(NOUSER),,--run-as-current-user) 		\
24144707491SFam Zheng			--security-opt seccomp=unconfined		\
242e86c9a64SAlex Bennée			$(if $V,,--rm) 					\
2434b2c6bc5SFam Zheng			$(if $(DEBUG),-ti,)				\
2448a2390a4SDaniel P. Berrange			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
2455609c512SAlex Bennée			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
246e86c9a64SAlex Bennée			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
247e86c9a64SAlex Bennée			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
248e86c9a64SAlex Bennée			-e SHOW_ENV=$(SHOW_ENV) 			\
24936ac78e6SFam Zheng			$(if $(NOUSER),,				\
250324027c2SFam Zheng				-e CCACHE_DIR=/var/tmp/ccache 		\
251324027c2SFam Zheng				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
25236ac78e6SFam Zheng			)						\
25336ac78e6SFam Zheng			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
254e86c9a64SAlex Bennée			$(IMAGE) 					\
255c8158513SFam Zheng			/var/tmp/qemu/run 				\
256e86c9a64SAlex Bennée			$(TEST), "  RUN $(TEST) in ${IMAGE}")
25708c3f6d5SPeter Xu	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
25808c3f6d5SPeter Xu		"  CLEANUP $(DOCKER_SRC_COPY)")
259e86c9a64SAlex Bennée
260e86c9a64SAlex Bennée# Run targets:
261e86c9a64SAlex Bennée#
262e86c9a64SAlex Bennée# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
263e86c9a64SAlex Bennéedocker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
264e86c9a64SAlex Bennéedocker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
265e86c9a64SAlex Bennéedocker-run-%:
266767b6bd2SAlex Bennée	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu/$(IMAGE)
267324027c2SFam Zheng
268324027c2SFam Zhengdocker-clean:
269e27cae26SAlex Bennée	$(call quiet-command, $(DOCKER_SCRIPT) clean)
270