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