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