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