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