xref: /openbmc/qemu/tests/docker/Makefile.include (revision 93dd625f)
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 debian11
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 -t qemu:$* -f $< \
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-debian10
95DOCKER_PARTIAL_IMAGES += debian-amd64-cross
96else
97docker-image-debian-amd64-cross: docker-image-debian10
98DOCKER_PARTIAL_IMAGES += debian-amd64
99endif
100docker-image-debian-win32-cross: docker-image-debian9-mxe
101docker-image-debian-win64-cross: docker-image-debian9-mxe
102
103# For non-x86 hosts not all cross-compilers have been packaged
104ifneq ($(HOST_ARCH),x86_64)
105DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
106DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
107DOCKER_PARTIAL_IMAGES += debian-s390x-cross
108DOCKER_PARTIAL_IMAGES += debian-win32-cross debian-win64-cross
109DOCKER_PARTIAL_IMAGES += fedora travis
110endif
111
112docker-image-debian-alpha-cross: docker-image-debian10
113docker-image-debian-arm64-cross: docker-image-debian10
114docker-image-debian-armel-cross: docker-image-debian10
115docker-image-debian-armhf-cross: docker-image-debian10
116docker-image-debian-hppa-cross: docker-image-debian10
117docker-image-debian-m68k-cross: docker-image-debian10
118docker-image-debian-mips-cross: docker-image-debian10
119docker-image-debian-mips64-cross: docker-image-debian10
120docker-image-debian-mips64el-cross: docker-image-debian10
121docker-image-debian-mipsel-cross: docker-image-debian10
122docker-image-debian-powerpc-cross: docker-image-debian10
123docker-image-debian-ppc64-cross: docker-image-debian10
124docker-image-debian-ppc64el-cross: docker-image-debian10
125docker-image-debian-riscv64-cross: docker-image-debian10
126docker-image-debian-s390x-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-debian-tricore-cross: docker-image-debian9
134docker-image-debian-arm64-test-cross: docker-image-debian11
135
136# These images may be good enough for building tests but not for test builds
137DOCKER_PARTIAL_IMAGES += debian-alpha-cross
138DOCKER_PARTIAL_IMAGES += debian-arm64-test-cross
139DOCKER_PARTIAL_IMAGES += debian-hppa-cross
140DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
141DOCKER_PARTIAL_IMAGES += debian-powerpc-cross debian-ppc64-cross
142DOCKER_PARTIAL_IMAGES += debian-riscv64-cross
143DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
144DOCKER_PARTIAL_IMAGES += debian-tricore-cross
145DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
146DOCKER_PARTIAL_IMAGES += fedora-i386-cross fedora-cris-cross
147
148# Rules for building linux-user powered images
149#
150# These are slower than using native cross compiler setups but can
151# work around issues with poorly working multi-arch systems and broken
152# packages.
153
154# Expand all the pre-requistes for each docker image and test combination
155$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
156	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
157		$(eval .PHONY: docker-$t@$i) \
158		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
159	) \
160	$(foreach t,$(DOCKER_TESTS), \
161		$(eval docker-all-tests: docker-$t@$i) \
162		$(eval docker-$t: docker-$t@$i) \
163	) \
164)
165
166docker:
167	@echo 'Build QEMU and run tests inside Docker or Podman containers'
168	@echo
169	@echo 'Available targets:'
170	@echo
171	@echo '    docker:              Print this help.'
172	@echo '    docker-all-tests:    Run all image/test combinations.'
173	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
174	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
175	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
176	@echo '                         Note: "TEST" is one of the listed test name,'
177	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
178	@echo '                         "IMAGE" is one of the listed container name.'
179	@echo '    docker-image:        Build all images.'
180	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
181	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
182	@echo
183	@echo 'Available container images:'
184	@echo '    $(DOCKER_IMAGES)'
185ifneq ($(DOCKER_USER_IMAGES),)
186	@echo
187	@echo 'Available linux-user images (docker-binfmt-image-debian-%):'
188	@echo '    $(DOCKER_USER_IMAGES)'
189endif
190	@echo
191	@echo 'Available tests:'
192	@echo '    $(DOCKER_TESTS)'
193	@echo
194	@echo 'Available tools:'
195	@echo '    $(DOCKER_TOOLS)'
196	@echo
197	@echo 'Special variables:'
198	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
199	@echo '    EXTRA_CONFIGURE_OPTS="..."'
200	@echo '                         Extra configure options.'
201	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
202	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
203	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
204	@echo '                         (default is 1)'
205	@echo '    DEBUG=1              Stop and drop to shell in the created container'
206	@echo '                         before running the command.'
207	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
208	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
209	@echo '    NOUSER               Define to disable adding current user to containers passwd.'
210	@echo '    NOCACHE=1            Ignore cache when build images.'
211	@echo '    EXECUTABLE=<path>    Include executable in image.'
212	@echo '    EXTRA_FILES="<path> [... <path>]"'
213	@echo '                         Include extra files in image.'
214	@echo '    ENGINE=auto/docker/podman'
215	@echo '                         Specify which container engine to run.'
216
217# This rule if for directly running against an arbitrary docker target.
218# It is called by the expanded docker targets (e.g. make
219# docker-test-foo@bar) which will do additional verification.
220#
221# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
222#
223docker-run: docker-qemu-src
224	@mkdir -p "$(DOCKER_CCACHE_DIR)"
225	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
226		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
227	fi
228	$(if $(EXECUTABLE),						\
229		$(call quiet-command,					\
230			$(DOCKER_SCRIPT) update 			\
231			$(IMAGE) $(EXECUTABLE),				\
232			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
233	$(call quiet-command,						\
234		$(DOCKER_SCRIPT) run 					\
235			$(if $(NOUSER),,--run-as-current-user) 		\
236			--security-opt seccomp=unconfined		\
237			$(if $V,,--rm) 					\
238			$(if $(DEBUG),-ti,)				\
239			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
240			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
241			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
242			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
243			-e SHOW_ENV=$(SHOW_ENV) 			\
244			$(if $(NOUSER),,				\
245				-e CCACHE_DIR=/var/tmp/ccache 		\
246				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
247			)						\
248			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
249			$(IMAGE) 					\
250			/var/tmp/qemu/run 				\
251			$(TEST), "  RUN $(TEST) in ${IMAGE}")
252	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
253		"  CLEANUP $(DOCKER_SRC_COPY)")
254
255# Run targets:
256#
257# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
258docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
259docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
260docker-run-%:
261	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu:$(IMAGE)
262
263docker-clean:
264	$(call quiet-command, $(DOCKER_SCRIPT) clean)
265