xref: /openbmc/qemu/tests/docker/Makefile.include (revision 90f9e35b)
1# Makefile for Docker tests
2
3.PHONY: docker docker-help docker-test docker-clean docker-image docker-qemu-src
4
5NULL :=
6SPACE := $(NULL) #
7COMMA := ,
8
9HOST_ARCH = $(if $(ARCH),$(ARCH),$(shell uname -m))
10
11# These variables can be set by the user to limit the set of docker
12# images and tests to a more restricted subset
13TESTS ?= %
14IMAGES ?= %
15
16DOCKER_SUFFIX := .docker
17DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
18# we don't run tests on intermediate images (used as base by another image)
19DOCKER_PARTIAL_IMAGES := debian10 debian11
20# we don't directly build virtual images (they are used to build other images)
21DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain empty
22__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
23DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
24DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
25# Use a global constant ccache directory to speed up repetitive builds
26DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
27ifeq ($(HOST_ARCH),x86_64)
28DOCKER_DEFAULT_REGISTRY := registry.gitlab.com/qemu-project/qemu
29endif
30DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),$(DOCKER_DEFAULT_REGISTRY))
31
32__TESTS := $(notdir $(shell \
33		find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
34DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
35
36ENGINE := auto
37
38DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
39
40CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
41DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
42
43.DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
44$(DOCKER_SRC_COPY):
45	@mkdir $@
46	$(if $(SRC_ARCHIVE), \
47		$(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
48			"CP", "$@/qemu.tar"), \
49		$(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
50			"GEN", "$@/qemu.tar"))
51	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
52		"COPY","RUNNER")
53
54docker-qemu-src: $(DOCKER_SRC_COPY)
55
56docker-image: ${DOCKER_TARGETS}
57
58# General rule for building docker images. If we are a sub-make
59# invoked with SKIP_DOCKER_BUILD we still check the image is up to date
60# though
61ifdef SKIP_DOCKER_BUILD
62docker-image-%: $(DOCKER_FILES_DIR)/%.docker
63	$(call quiet-command, \
64		$(DOCKER_SCRIPT) check --quiet qemu/$* $<, \
65		"CHECK", "$*")
66else
67docker-image-%: $(DOCKER_FILES_DIR)/%.docker
68	$(call quiet-command,\
69		$(DOCKER_SCRIPT) build -t qemu/$* -f $< \
70		$(if $V,,--quiet) \
71		$(if $(NOCACHE),--no-cache, \
72			$(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
73		$(if $(NOUSER),,--add-current-user) \
74		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
75		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
76		"BUILD","$*")
77
78# Special rule for debootstraped binfmt linux-user images
79docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
80	$(if $(EXECUTABLE),,\
81		$(error EXECUTABLE not set, debootstrap of debian-$* would fail))
82	$(if $(DEB_ARCH),,\
83		$(error DEB_ARCH not set, debootstrap of debian-$* would fail))
84	$(if $(DEB_TYPE),,\
85		$(error DEB_TYPE not set, debootstrap of debian-$* would fail))
86	$(if $(wildcard $(EXECUTABLE)),						\
87		$(call quiet-command,						\
88			DEB_ARCH=$(DEB_ARCH)					\
89			DEB_TYPE=$(DEB_TYPE) 					\
90			$(if $(DEB_URL),DEB_URL=$(DEB_URL),)			\
91			$(DOCKER_SCRIPT) build -t qemu/debian-$* -f $< 		\
92			$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) 		\
93			$(if $(NOUSER),,--add-current-user) 			\
94			$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))	\
95			$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
96			"BUILD","binfmt debian-$* (debootstrapped)"),		\
97		$(call quiet-command,						\
98			$(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< || 	\
99			{ echo "You will need to build $(EXECUTABLE)"; exit 1;},\
100			"CHECK", "debian-$* exists"))
101
102# These are test targets
103USER_TCG_TARGETS=$(patsubst %-linux-user,qemu-%,$(filter %-linux-user,$(TARGET_DIRS)))
104EXEC_COPY_TESTS=$(patsubst %,docker-exec-copy-test-%, $(USER_TCG_TARGETS))
105
106$(EXEC_COPY_TESTS): docker-exec-copy-test-%: $(DOCKER_FILES_DIR)/empty.docker
107	$(call quiet-command,							\
108		$(DOCKER_SCRIPT) build -t qemu/exec-copy-test-$* -f $< 		\
109			$(if $V,,--quiet) --no-cache 				\
110			--include-executable=$*					\
111			--skip-binfmt,						\
112			"TEST","copy $* to container")
113	$(call quiet-command,							\
114		$(DOCKER_SCRIPT) run qemu/exec-copy-test-$* 			\
115			/$* -version > tests/docker-exec-copy-test-$*.out,	\
116			"TEST","check $* works in container")
117
118docker-exec-copy-test: $(EXEC_COPY_TESTS)
119
120endif
121
122# Enforce dependencies for composite images
123ifeq ($(HOST_ARCH),x86_64)
124docker-image-debian-amd64: docker-image-debian10
125DOCKER_PARTIAL_IMAGES += debian-amd64-cross
126else
127docker-image-debian-amd64-cross: docker-image-debian10
128DOCKER_PARTIAL_IMAGES += debian-amd64
129endif
130
131# For non-x86 hosts not all cross-compilers have been packaged
132ifneq ($(HOST_ARCH),x86_64)
133DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
134DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
135DOCKER_PARTIAL_IMAGES += debian-s390x-cross
136DOCKER_PARTIAL_IMAGES += fedora
137endif
138
139docker-image-debian-alpha-cross: docker-image-debian10
140docker-image-debian-armel-cross: docker-image-debian10
141docker-image-debian-armhf-cross: docker-image-debian10
142docker-image-debian-hppa-cross: docker-image-debian10
143docker-image-debian-m68k-cross: docker-image-debian10
144docker-image-debian-mips-cross: docker-image-debian10
145docker-image-debian-mips64-cross: docker-image-debian10
146docker-image-debian-mips64el-cross: docker-image-debian10
147docker-image-debian-mipsel-cross: docker-image-debian10
148docker-image-debian-ppc64el-cross: docker-image-debian10
149docker-image-debian-sh4-cross: docker-image-debian10
150docker-image-debian-sparc64-cross: docker-image-debian10
151
152# The native build should never use the registry
153docker-image-debian-native: DOCKER_REGISTRY=
154
155# base images should not add a local user
156docker-image-debian10: NOUSER=1
157docker-image-debian11: NOUSER=1
158
159# alpine has no adduser
160docker-image-alpine: NOUSER=1
161
162#
163# The build rule for hexagon-cross is special in so far for most of
164# the time we don't want to build it. While dockers caching does avoid
165# this most of the time sometimes we want to force the issue.
166#
167docker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker
168	$(if $(NOCACHE), 								\
169		$(call quiet-command,							\
170			$(DOCKER_SCRIPT) build -t qemu/debian-hexagon-cross -f $< 	\
171			$(if $V,,--quiet) --no-cache 					\
172			--registry $(DOCKER_REGISTRY) --extra-files			\
173			$(DOCKER_FILES_DIR)/debian-hexagon-cross.docker.d/build-toolchain.sh, \
174			"BUILD", "debian-hexagon-cross"),				\
175		$(call quiet-command,							\
176			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)			\
177				qemu/debian-hexagon-cross $(DOCKER_REGISTRY),		\
178			"FETCH", "debian-hexagon-cross")				\
179		$(call quiet-command,							\
180			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 			\
181				qemu/debian-hexagon-cross --add-current-user,		\
182			"PREPARE", "debian-hexagon-cross"))
183
184debian-toolchain-run = \
185	$(if $(NOCACHE), 						\
186		$(call quiet-command,					\
187			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
188			$(if $V,,--quiet) --no-cache 			\
189			--registry $(DOCKER_REGISTRY) --extra-files	\
190			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
191			"BUILD", $1),				        \
192		$(call quiet-command,					\
193			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
194				qemu/$1 $(DOCKER_REGISTRY),		\
195			"FETCH", $1)					\
196		$(call quiet-command,					\
197			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
198				qemu/$1 				\
199				$(if $(NOUSER),,--add-current-user) 	\
200			"PREPARE", $1))
201debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
202
203docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
204    $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
205	$(call debian-toolchain, $@)
206
207docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
208    $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
209	$(call debian-toolchain, $@)
210
211# Specialist build images, sometimes very limited tools
212docker-image-debian-tricore-cross: docker-image-debian10
213docker-image-debian-all-test-cross: docker-image-debian10
214docker-image-debian-microblaze-cross: docker-image-debian10
215docker-image-debian-nios2-cross: docker-image-debian10
216docker-image-debian-powerpc-test-cross: docker-image-debian11
217docker-image-debian-riscv64-test-cross: docker-image-debian11
218
219# These images may be good enough for building tests but not for test builds
220DOCKER_PARTIAL_IMAGES += debian-alpha-cross
221DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
222DOCKER_PARTIAL_IMAGES += debian-hppa-cross
223DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
224DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
225DOCKER_PARTIAL_IMAGES += debian-nios2-cross
226DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
227DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
228DOCKER_PARTIAL_IMAGES += debian-tricore-cross
229DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
230DOCKER_PARTIAL_IMAGES += fedora-cris-cross
231
232# Rules for building linux-user powered images
233#
234# These are slower than using native cross compiler setups but can
235# work around issues with poorly working multi-arch systems and broken
236# packages.
237
238# Expand all the pre-requistes for each docker image and test combination
239$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES) $(DOCKER_VIRTUAL_IMAGES),$(DOCKER_IMAGES)), \
240	$(foreach t,$(DOCKER_TESTS), \
241		$(eval .PHONY: docker-$t@$i) \
242		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
243	) \
244	$(foreach t,$(DOCKER_TESTS), \
245		$(eval docker-all-tests: docker-$t@$i) \
246		$(eval docker-$t: docker-$t@$i) \
247	) \
248)
249
250docker:
251	@echo 'Build QEMU and run tests inside Docker or Podman containers'
252	@echo
253	@echo 'Available targets:'
254	@echo
255	@echo '    docker:              Print this help.'
256	@echo '    docker-all-tests:    Run all image/test combinations.'
257	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
258	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
259	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
260	@echo '                         Note: "TEST" is one of the listed test name,'
261	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
262	@echo '                         "IMAGE" is one of the listed container name.'
263	@echo '    docker-image:        Build all images.'
264	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
265	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
266	@echo
267	@echo 'Available container images:'
268	@echo '    $(DOCKER_IMAGES)'
269ifneq ($(DOCKER_USER_IMAGES),)
270	@echo
271	@echo 'Available linux-user images (docker-binfmt-image-debian-%):'
272	@echo '    $(DOCKER_USER_IMAGES)'
273endif
274	@echo
275	@echo 'Available tests:'
276	@echo '    $(DOCKER_TESTS)'
277	@echo
278	@echo 'Special variables:'
279	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
280	@echo '    EXTRA_CONFIGURE_OPTS="..."'
281	@echo '                         Extra configure options.'
282	@echo '    IMAGES="a b c ..":   Restrict available images to subset.'
283	@echo '    TESTS="x y z .."     Restrict available tests to subset.'
284	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
285	@echo '                         (default is 1)'
286	@echo '    DEBUG=1              Stop and drop to shell in the created container'
287	@echo '                         before running the command.'
288	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
289	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
290	@echo '    NOUSER=1             Define to disable adding current user to containers passwd.'
291	@echo '    NOCACHE=1            Ignore cache when build images.'
292	@echo '    EXECUTABLE=<path>    Include executable in image.'
293	@echo '    EXTRA_FILES="<path> [... <path>]"'
294	@echo '                         Include extra files in image.'
295	@echo '    ENGINE=auto/docker/podman'
296	@echo '                         Specify which container engine to run.'
297	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
298
299docker-help: docker
300
301# This rule if for directly running against an arbitrary docker target.
302# It is called by the expanded docker targets (e.g. make
303# docker-test-foo@bar) which will do additional verification.
304#
305# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
306#
307docker-run: docker-qemu-src
308	@mkdir -p "$(DOCKER_CCACHE_DIR)"
309	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
310		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
311	fi
312	$(if $(EXECUTABLE),						\
313		$(call quiet-command,					\
314			$(DOCKER_SCRIPT) update 			\
315			$(IMAGE) --executable $(EXECUTABLE),		\
316			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
317	$(call quiet-command,						\
318		$(DOCKER_SCRIPT) run 					\
319			$(if $(NOUSER),,--run-as-current-user) 		\
320			--security-opt seccomp=unconfined		\
321			$(if $(DEBUG),-ti,)				\
322			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
323			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
324			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
325			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
326			-e SHOW_ENV=$(SHOW_ENV) 			\
327			$(if $(NOUSER),,				\
328				-e CCACHE_DIR=/var/tmp/ccache 		\
329				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
330			)						\
331			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
332			$(IMAGE) 					\
333			/var/tmp/qemu/run 				\
334			$(TEST), "  RUN $(TEST) in ${IMAGE}")
335	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
336		"  CLEANUP $(DOCKER_SRC_COPY)")
337
338# Run targets:
339#
340# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
341docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
342docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
343docker-run-%:
344	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu/$(IMAGE)
345
346docker-clean:
347	$(call quiet-command, $(DOCKER_SCRIPT) clean)
348