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