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