xref: /openbmc/qemu/tests/docker/Makefile.include (revision 756a98dd)
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-armel-cross: docker-image-debian10
93docker-image-debian-armhf-cross: docker-image-debian10
94docker-image-debian-hppa-cross: docker-image-debian10
95docker-image-debian-m68k-cross: docker-image-debian10
96docker-image-debian-mips-cross: docker-image-debian10
97docker-image-debian-mips64-cross: docker-image-debian10
98docker-image-debian-mips64el-cross: docker-image-debian10
99docker-image-debian-mipsel-cross: docker-image-debian10
100docker-image-debian-ppc64el-cross: docker-image-debian10
101docker-image-debian-sh4-cross: docker-image-debian10
102docker-image-debian-sparc64-cross: docker-image-debian10
103
104# The native build should never use the registry
105docker-image-debian-native: DOCKER_REGISTRY=
106
107# base images should not add a local user
108docker-image-debian10: NOUSER=1
109docker-image-debian11: NOUSER=1
110
111# alpine has no adduser
112docker-image-alpine: NOUSER=1
113
114debian-toolchain-run = \
115	$(if $(NOCACHE), 						\
116		$(call quiet-command,					\
117			$(DOCKER_SCRIPT) build -t qemu/$1 -f $< 	\
118			$(if $V,,--quiet) --no-cache 			\
119			--registry $(DOCKER_REGISTRY) --extra-files	\
120			$(DOCKER_FILES_DIR)/$1.d/build-toolchain.sh,	\
121			"BUILD", $1),				        \
122		$(call quiet-command,					\
123			$(DOCKER_SCRIPT) fetch $(if $V,,--quiet)	\
124				qemu/$1 $(DOCKER_REGISTRY),		\
125			"FETCH", $1)					\
126		$(call quiet-command,					\
127			$(DOCKER_SCRIPT) update $(if $V,,--quiet) 	\
128				qemu/$1 				\
129				$(if $(NOUSER),,--add-current-user) 	\
130			"PREPARE", $1))
131debian-toolchain = $(call debian-toolchain-run,$(patsubst docker-image-%,%,$1))
132
133docker-image-debian-hexagon-cross: $(DOCKER_FILES_DIR)/debian-hexagon-cross.docker \
134	$(DOCKER_FILES_DIR)/debian-hexagon-cross.d/build-toolchain.sh
135	$(call debian-toolchain, $@)
136
137docker-image-debian-microblaze-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
138    $(DOCKER_FILES_DIR)/debian-microblaze-cross.d/build-toolchain.sh
139	$(call debian-toolchain, $@)
140
141docker-image-debian-nios2-cross: $(DOCKER_FILES_DIR)/debian-toolchain.docker \
142    $(DOCKER_FILES_DIR)/debian-nios2-cross.d/build-toolchain.sh
143	$(call debian-toolchain, $@)
144
145# Specialist build images, sometimes very limited tools
146docker-image-debian-tricore-cross: docker-image-debian10
147docker-image-debian-all-test-cross: docker-image-debian10
148docker-image-debian-microblaze-cross: docker-image-debian10
149docker-image-debian-nios2-cross: docker-image-debian10
150docker-image-debian-powerpc-test-cross: docker-image-debian11
151docker-image-debian-riscv64-test-cross: docker-image-debian11
152
153# These images may be good enough for building tests but not for test builds
154DOCKER_PARTIAL_IMAGES += debian-alpha-cross
155DOCKER_PARTIAL_IMAGES += debian-powerpc-test-cross
156DOCKER_PARTIAL_IMAGES += debian-hppa-cross
157DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
158DOCKER_PARTIAL_IMAGES += debian-microblaze-cross
159DOCKER_PARTIAL_IMAGES += debian-nios2-cross
160DOCKER_PARTIAL_IMAGES += debian-riscv64-test-cross
161DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
162DOCKER_PARTIAL_IMAGES += debian-tricore-cross
163DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
164DOCKER_PARTIAL_IMAGES += fedora-cris-cross
165
166# images that are only used to build other images
167DOCKER_VIRTUAL_IMAGES := debian-bootstrap debian-toolchain
168
169__IMAGES := $(sort $(filter-out $(DOCKER_VIRTUAL_IMAGES), $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))))
170DOCKER_IMAGES := $(if $(IMAGES), $(filter $(IMAGES), $(__IMAGES)), $(__IMAGES))
171
172__TESTS := $(notdir $(shell find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
173DOCKER_TESTS := $(if $(TESTS), $(filter $(TESTS), $(__TESTS)), $(__TESTS))
174
175# Expand all the pre-requistes for each docker image and test combination
176$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
177	$(foreach t,$(DOCKER_TESTS), \
178		$(eval .PHONY: docker-$t@$i) \
179		$(eval docker-$t@$i: docker-image-$i; @$(MAKE) docker-run TEST=$t IMAGE=$i) \
180	) \
181	$(foreach t,$(DOCKER_TESTS), \
182		$(eval docker-all-tests: docker-$t@$i) \
183		$(eval docker-$t: docker-$t@$i) \
184	) \
185)
186
187docker:
188	@echo 'Build QEMU and run tests inside Docker or Podman containers'
189	@echo
190	@echo 'Available targets:'
191	@echo
192	@echo '    docker:              Print this help.'
193	@echo '    docker-all-tests:    Run all image/test combinations.'
194	@echo '    docker-TEST:         Run "TEST" on all image combinations.'
195	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
196	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
197	@echo '                         Note: "TEST" is one of the listed test name,'
198	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
199	@echo '                         "IMAGE" is one of the listed container name.'
200	@echo '    docker-image:        Build all images.'
201	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
202	@echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
203	@echo
204	@echo 'Available container images:'
205	@echo '    $(DOCKER_IMAGES)'
206	@echo
207	@echo 'Available tests:'
208	@echo '    $(DOCKER_TESTS)'
209	@echo
210	@echo 'Special variables:'
211	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
212	@echo '    EXTRA_CONFIGURE_OPTS="..."'
213	@echo '                         Extra configure options.'
214	@echo '    IMAGES="a b c ..":   Restrict available images to subset.'
215	@echo '    TESTS="x y z .."     Restrict available tests to subset.'
216	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
217	@echo '                         (default is 1)'
218	@echo '    DEBUG=1              Stop and drop to shell in the created container'
219	@echo '                         before running the command.'
220	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
221	@echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
222	@echo '    NOUSER=1             Define to disable adding current user to containers passwd.'
223	@echo '    NOCACHE=1            Ignore cache when build images.'
224	@echo '    EXECUTABLE=<path>    Include executable in image.'
225	@echo '    EXTRA_FILES="<path> [... <path>]"'
226	@echo '                         Include extra files in image.'
227	@echo '    ENGINE=auto/docker/podman'
228	@echo '                         Specify which container engine to run.'
229	@echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
230
231docker-help: docker
232
233# Use a global constant ccache directory to speed up repetitive builds
234DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
235
236# This rule if for directly running against an arbitrary docker target.
237# It is called by the expanded docker targets (e.g. make
238# docker-test-foo@bar) which will also ensure the image is up to date.
239#
240# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
241#
242docker-run: docker-qemu-src
243	@mkdir -p "$(DOCKER_CCACHE_DIR)"
244	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
245		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
246	fi
247	$(if $(EXECUTABLE),						\
248		$(call quiet-command,					\
249			$(DOCKER_SCRIPT) update 			\
250			$(IMAGE) --executable $(EXECUTABLE),		\
251			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
252	$(call quiet-command,						\
253		$(DOCKER_SCRIPT) run 					\
254			$(if $(NOUSER),,--run-as-current-user) 		\
255			--security-opt seccomp=unconfined		\
256			$(if $(DEBUG),-ti,)				\
257			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
258			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
259			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
260			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
261			-e SHOW_ENV=$(SHOW_ENV) 			\
262			$(if $(NOUSER),,				\
263				-e CCACHE_DIR=/var/tmp/ccache 		\
264				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
265			)						\
266			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
267			$(IMAGE) 					\
268			/var/tmp/qemu/run 				\
269			$(TEST), "  RUN $(TEST) in ${IMAGE}")
270	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
271		"  CLEANUP $(DOCKER_SRC_COPY)")
272
273docker-image: ${DOCKER_IMAGES:%=docker-image-%}
274
275docker-clean:
276	$(call quiet-command, $(DOCKER_SCRIPT) clean)
277