xref: /qemu/tests/docker/Makefile.include (revision 7678b74a)
1# Makefile for Docker tests
2
3.PHONY: docker docker-test docker-clean docker-image docker-qemu-src
4
5DOCKER_SUFFIX := .docker
6DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
7# we don't run tests on intermediate images (used as base by another image)
8DOCKER_PARTIAL_IMAGES := debian9 debian10
9DOCKER_PARTIAL_IMAGES += debian9-mxe debian-bootstrap
10DOCKER_IMAGES := $(sort $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))
11DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
12# Use a global constant ccache directory to speed up repetitive builds
13DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
14
15DOCKER_TESTS := $(notdir $(shell \
16	find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
17
18DOCKER_TOOLS := travis
19
20ENGINE := auto
21
22DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
23
24TESTS ?= %
25IMAGES ?= %
26
27CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
28DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
29
30.DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
31$(DOCKER_SRC_COPY):
32	@mkdir $@
33	$(if $(SRC_ARCHIVE), \
34		$(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
35			"CP", "$@/qemu.tar"), \
36		$(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
37			"GEN", "$@/qemu.tar"))
38	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
39		"COPY","RUNNER")
40
41docker-qemu-src: $(DOCKER_SRC_COPY)
42
43docker-image: ${DOCKER_TARGETS}
44
45# General rule for building docker images. If we are a sub-make
46# invoked with SKIP_DOCKER_BUILD we still check the image is up to date
47# though
48ifdef SKIP_DOCKER_BUILD
49docker-image-%: $(DOCKER_FILES_DIR)/%.docker
50	$(call quiet-command, \
51		$(DOCKER_SCRIPT) check --quiet qemu:$* $<, \
52		"CHECK", "$*")
53else
54docker-image-%: $(DOCKER_FILES_DIR)/%.docker
55	$(call quiet-command,\
56		$(DOCKER_SCRIPT) build qemu:$* $< \
57		$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \
58		$(if $(NOUSER),,--add-current-user) \
59		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
60		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
61		"BUILD","$*")
62
63# Special rule for debootstraped binfmt linux-user images
64docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
65	$(if $(EXECUTABLE),,\
66		$(error EXECUTABLE not set, debootstrap of debian-$* would fail))
67	$(if $(DEB_ARCH),,\
68		$(error DEB_ARCH not set, debootstrap of debian-$* would fail))
69	$(if $(DEB_TYPE),,\
70		$(error DEB_TYPE not set, debootstrap of debian-$* would fail))
71	$(if $(wildcard $(EXECUTABLE)),						\
72		$(call quiet-command,						\
73			DEB_ARCH=$(DEB_ARCH)					\
74			DEB_TYPE=$(DEB_TYPE) 					\
75			$(if $(DEB_URL),DEB_URL=$(DEB_URL),)			\
76			$(DOCKER_SCRIPT) build qemu:debian-$* $< 		\
77			$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) 		\
78			$(if $(NOUSER),,--add-current-user) 			\
79			$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))	\
80			$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
81			"BUILD","binfmt debian-$* (debootstrapped)"),		\
82		$(call quiet-command,						\
83			$(DOCKER_SCRIPT) check --quiet qemu:debian-$* $< || 	\
84			{ echo "You will need to build $(EXECUTABLE)"; exit 1;},\
85			"CHECK", "debian-$* exists"))
86
87endif
88
89# Enforce dependencies for composite images
90docker-image-debian9-mxe: docker-image-debian9
91ifeq ($(ARCH),x86_64)
92docker-image-debian-amd64: docker-image-debian9
93DOCKER_PARTIAL_IMAGES += debian-amd64-cross
94else
95docker-image-debian-amd64-cross: docker-image-debian10
96DOCKER_PARTIAL_IMAGES += debian-amd64
97endif
98docker-image-debian-armel-cross: docker-image-debian9
99docker-image-debian-armhf-cross: docker-image-debian9
100docker-image-debian-mips-cross: docker-image-debian9
101docker-image-debian-mipsel-cross: docker-image-debian9
102docker-image-debian-mips64el-cross: docker-image-debian9
103docker-image-debian-ppc64el-cross: docker-image-debian9
104docker-image-debian-s390x-cross: docker-image-debian9
105docker-image-debian-win32-cross: docker-image-debian9-mxe
106docker-image-debian-win64-cross: docker-image-debian9-mxe
107
108# For non-x86 hosts not all cross-compilers have been packaged
109ifneq ($(ARCH),x86_64)
110DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
111DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
112DOCKER_PARTIAL_IMAGES += debian-s390x-cross
113DOCKER_PARTIAL_IMAGES += debian-win32-cross debian-win64-cross
114DOCKER_PARTIAL_IMAGES += fedora travis
115endif
116
117docker-image-debian-alpha-cross: docker-image-debian10
118docker-image-debian-arm64-cross: docker-image-debian10
119docker-image-debian-hppa-cross: docker-image-debian10
120docker-image-debian-m68k-cross: docker-image-debian10
121docker-image-debian-mips64-cross: docker-image-debian10
122docker-image-debian-powerpc-cross: docker-image-debian10
123docker-image-debian-ppc64-cross: docker-image-debian10
124docker-image-debian-riscv64-cross: docker-image-debian10
125docker-image-debian-sh4-cross: docker-image-debian10
126docker-image-debian-sparc64-cross: docker-image-debian10
127
128docker-image-travis: NOUSER=1
129
130# Specialist build images, sometimes very limited tools
131docker-image-tricore-cross: docker-image-debian9
132
133# These images may be good enough for building tests but not for test builds
134DOCKER_PARTIAL_IMAGES += debian-alpha-cross
135DOCKER_PARTIAL_IMAGES += debian-hppa-cross
136DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
137DOCKER_PARTIAL_IMAGES += debian-powerpc-cross debian-ppc64-cross
138DOCKER_PARTIAL_IMAGES += debian-riscv64-cross
139DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
140DOCKER_PARTIAL_IMAGES += debian-tricore-cross
141DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
142DOCKER_PARTIAL_IMAGES += fedora-i386-cross fedora-cris-cross
143
144# Rules for building linux-user powered images
145#
146# These are slower than using native cross compiler setups but can
147# work around issues with poorly working multi-arch systems and broken
148# packages.
149
150# Expand all the pre-requistes for each docker image and test combination
151$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
152	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
153		$(eval .PHONY: docker-$t@$i) \
154		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
155	) \
156	$(foreach t,$(DOCKER_TESTS), \
157		$(eval docker-all-tests: docker-$t@$i) \
158		$(eval docker-$t: docker-$t@$i) \
159	) \
160)
161
162docker:
163	@echo 'Build QEMU and run tests inside Docker or Podman containers'
164	@echo
165	@echo 'Available targets:'
166	@echo
167	@echo '    docker:              Print this help.'
168	@echo '    docker-all-tests:    Run all image/test combinations.'
169	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
170	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
171	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
172	@echo '                         Note: "TEST" is one of the listed test name,'
173	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
174	@echo '                         "IMAGE" is one of the listed container name.'
175	@echo '    docker-image:        Build all images.'
176	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
177	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
178	@echo
179	@echo 'Available container images:'
180	@echo '    $(DOCKER_IMAGES)'
181ifneq ($(DOCKER_USER_IMAGES),)
182	@echo
183	@echo 'Available linux-user images (docker-binfmt-image-debian-%):'
184	@echo '    $(DOCKER_USER_IMAGES)'
185endif
186	@echo
187	@echo 'Available tests:'
188	@echo '    $(DOCKER_TESTS)'
189	@echo
190	@echo 'Available tools:'
191	@echo '    $(DOCKER_TOOLS)'
192	@echo
193	@echo 'Special variables:'
194	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
195	@echo '    EXTRA_CONFIGURE_OPTS="..."'
196	@echo '                         Extra configure options.'
197	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
198	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
199	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
200	@echo '                         (default is 1)'
201	@echo '    DEBUG=1              Stop and drop to shell in the created container'
202	@echo '                         before running the command.'
203	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
204	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
205	@echo '    NOUSER               Define to disable adding current user to containers passwd.'
206	@echo '    NOCACHE=1            Ignore cache when build images.'
207	@echo '    EXECUTABLE=<path>    Include executable in image.'
208	@echo '    EXTRA_FILES="<path> [... <path>]"'
209	@echo '                         Include extra files in image.'
210	@echo '    ENGINE=auto/docker/podman'
211	@echo '                         Specify which container engine to run.'
212
213# This rule if for directly running against an arbitrary docker target.
214# It is called by the expanded docker targets (e.g. make
215# docker-test-foo@bar) which will do additional verification.
216#
217# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
218#
219docker-run: docker-qemu-src
220	@mkdir -p "$(DOCKER_CCACHE_DIR)"
221	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
222		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
223	fi
224	$(if $(EXECUTABLE),						\
225		$(call quiet-command,					\
226			$(DOCKER_SCRIPT) update 			\
227			$(IMAGE) $(EXECUTABLE),				\
228			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
229	$(call quiet-command,						\
230		$(DOCKER_SCRIPT) run 					\
231			$(if $(NOUSER),,--run-as-current-user) 		\
232			--security-opt seccomp=unconfined		\
233			$(if $V,,--rm) 					\
234			$(if $(DEBUG),-ti,)				\
235			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
236			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
237			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
238			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
239			-e SHOW_ENV=$(SHOW_ENV) 			\
240			$(if $(NOUSER),,				\
241				-e CCACHE_DIR=/var/tmp/ccache 		\
242				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
243			)						\
244			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
245			$(IMAGE) 					\
246			/var/tmp/qemu/run 				\
247			$(TEST), "  RUN $(TEST) in ${IMAGE}")
248	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
249		"  CLEANUP $(DOCKER_SRC_COPY)")
250
251# Run targets:
252#
253# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
254docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
255docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
256docker-run-%:
257	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu:$(IMAGE)
258
259docker-clean:
260	$(call quiet-command, $(DOCKER_SCRIPT) clean)
261