xref: /openbmc/qemu/tests/docker/Makefile.include (revision ed3a06b1)
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-microblaze-cross: docker-image-debian10
144docker-image-debian-nios2-cross: docker-image-debian10
145docker-image-debian-powerpc-test-cross: docker-image-debian11
146docker-image-debian-riscv64-test-cross: docker-image-debian11
147
148# These images may be good enough for building tests but not for test builds
149DOCKER_PARTIAL_IMAGES += debian-alpha-cross
150DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
151DOCKER_PARTIAL_IMAGES += debian-hppa-cross
152DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
153DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
154DOCKER_PARTIAL_IMAGES += debian-nios2-cross
155DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
156DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
157DOCKER_PARTIAL_IMAGES += debian-tricore-cross
158DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
159DOCKER_PARTIAL_IMAGES += fedora-cris-cross
160
161# images that are only used to build other images
162DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
163
164__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
165DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
166
167__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
168DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
169
170# Expand all the pre-requistes for each docker image and test combination
171$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
172	$(foreach t,$(DOCKER_TESTS), \
173		$(eval .PHONY: docker-$t@$i) \
174		$(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=qemu/$i) \
175	) \
176	$(foreach t,$(DOCKER_TESTS), \
177		$(eval docker-all-tests: docker-$t@$i) \
178		$(eval docker-$t: docker-$t@$i) \
179	) \
180)
181
182docker:
183	@echo 'Build QEMU and run tests inside Docker or Podman containers'
184	@echo
185	@echo 'Available targets:'
186	@echo
187	@echo '    docker:              Print this help.'
188	@echo '    docker-all-tests:    Run all image/test combinations.'
189	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
190	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
191	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
192	@echo '                         Note: "TEST" is one of the listed test name,'
193	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
194	@echo '                         "IMAGE" is one of the listed container name.'
195	@echo '    docker-image:        Build all images.'
196	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
197	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
198	@echo
199	@echo 'Available container images:'
200	@echo '    $(DOCKER_IMAGES)'
201	@echo
202	@echo 'Available tests:'
203	@echo '    $(DOCKER_TESTS)'
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 ..":   Restrict available images to subset.'
210	@echo '    TESTS="x y z .."     Restrict available tests to subset.'
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=1             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
226docker-help: docker
227
228# Use a global constant ccache directory to speed up repetitive builds
229DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
230
231# This rule if for directly running against an arbitrary docker target.
232# It is called by the expanded docker targets (e.g. make
233# docker-test-foo@bar) which will also ensure the image is up to date.
234#
235# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
236#
237docker-run: docker-qemu-src
238	@mkdir -p "$(DOCKER_CCACHE_DIR)"
239	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
240		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
241	fi
242	$(if $(EXECUTABLE),						\
243		$(call quiet-command,					\
244			$(DOCKER_SCRIPT) update 			\
245			$(IMAGE) --executable $(EXECUTABLE),		\
246			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
247	$(call quiet-command,						\
248		$(DOCKER_SCRIPT) run 					\
249			$(if $(NOUSER),,--run-as-current-user) 		\
250			--security-opt seccomp=unconfined		\
251			$(if $(DEBUG),-ti,)				\
252			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
253			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
254			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
255			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
256			-e SHOW_ENV=$(SHOW_ENV) 			\
257			$(if $(NOUSER),,				\
258				-e CCACHE_DIR=/var/tmp/ccache 		\
259				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
260			)						\
261			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
262			$(IMAGE) 					\
263			/var/tmp/qemu/run 				\
264			$(TEST), "  RUN $(TEST) in ${IMAGE}")
265	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
266		"  CLEANUP $(DOCKER_SRC_COPY)")
267
268docker-image: ${DOCKER_IMAGES:%=docker-image-%}
269
270docker-clean:
271	$(call quiet-command, $(DOCKER_SCRIPT) clean)
272