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