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