xref: /openbmc/qemu/tests/docker/Makefile.include (revision ab1b2ba9)
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# Special case cross-compiling x86_64 on non-x86_64 systems.
73ifeq ($(HOST_ARCH),x86_64)
74DOCKER_PARTIAL_IMAGES += debian-amd64-cross
75else
76DOCKER_PARTIAL_IMAGES += debian-amd64
77endif
78
79# For non-x86 hosts not all cross-compilers have been packaged
80ifneq ($(HOST_ARCH),x86_64)
81DOCKER_PARTIAL_IMAGES += debian-mipsel-cross debian-mips64el-cross
82DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
83DOCKER_PARTIAL_IMAGES += debian-s390x-cross
84DOCKER_PARTIAL_IMAGES += fedora
85endif
86
87# The native build should never use the registry
88docker-image-debian-native: DOCKER_REGISTRY=
89
90# alpine has no adduser
91docker-image-alpine: NOUSER=1
92
93debian-toolchain-run = \
94	$(if $(NOCACHE), 						\
95		$(call quiet-command,					\
96			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
97			$(if $V,,--quiet) --no-cache 			\
98			--registry $(DOCKER_REGISTRY) --extra-files	\
99			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
100			"BUILD", $1),				        \
101		$(call quiet-command,					\
102			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
103				qemu/$1 $(DOCKER_REGISTRY),		\
104			"FETCH", $1)					\
105		$(call quiet-command,					\
106			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
107				qemu/$1 				\
108				$(if $(NOUSER),,--add-current-user) 	\
109			"PREPARE", $1))
110debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
111
112docker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker \
113	$(DOCKER_FILES_DIR)/debian-hexagon-cross.d/build-toolchain.sh
114	$(call debian-toolchain, $@)
115
116docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
117    $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
118	$(call debian-toolchain, $@)
119
120docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
121    $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
122	$(call debian-toolchain, $@)
123
124# These images may be good enough for building tests but not for test builds
125DOCKER_PARTIAL_IMAGES += debian-alpha-cross
126DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
127DOCKER_PARTIAL_IMAGES += debian-hppa-cross
128DOCKER_PARTIAL_IMAGES += debian-loongarch-cross
129DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
130DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
131DOCKER_PARTIAL_IMAGES += debian-mips-cross
132DOCKER_PARTIAL_IMAGES += debian-nios2-cross
133DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
134DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
135DOCKER_PARTIAL_IMAGES += debian-tricore-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		$(DOCKER_SCRIPT) run 					\
228			$(if $(NOUSER),,--run-as-current-user) 		\
229			--security-opt seccomp=unconfined		\
230			$(if $(DEBUG),-ti,)				\
231			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
232			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
233			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
234			-e TEST_COMMAND="$(TEST_COMMAND)" 		\
235			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
236			-e SHOW_ENV=$(SHOW_ENV) 			\
237			$(if $(NOUSER),,				\
238				-e CCACHE_DIR=/var/tmp/ccache 		\
239				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
240			)						\
241			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
242			$(IMAGE) 					\
243			/var/tmp/qemu/run 				\
244			$(TEST), "  RUN $(TEST) in ${IMAGE}")
245	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
246		"  CLEANUP $(DOCKER_SRC_COPY)")
247
248docker-image: ${DOCKER_IMAGES:%=docker-image-%}
249
250docker-clean:
251	$(call quiet-command, $(DOCKER_SCRIPT) clean)
252