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