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