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