Merge remote-tracking branch 'remotes/gkurz-gitlab/tags/9p-next-pull-request' into...
[qemu/ar7.git] / tests / docker / Makefile.include
blob0779dab5b96d3abc66326321883c8bae9c66dec9
1 # Makefile for Docker tests
3 .PHONY: docker docker-test docker-clean docker-image docker-qemu-src
5 NULL :=
6 SPACE := $(NULL) #
7 COMMA := ,
9 HOST_ARCH = $(if $(ARCH),$(ARCH),$(shell uname -m))
11 DOCKER_SUFFIX := .docker
12 DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
13 # we don't run tests on intermediate images (used as base by another image)
14 DOCKER_PARTIAL_IMAGES := debian10 debian11 debian-bootstrap
15 DOCKER_IMAGES := $(sort $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))
16 DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
17 # Use a global constant ccache directory to speed up repetitive builds
18 DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
19 DOCKER_REGISTRY := $(if $(REGISTRY),$(REGISTRY),registry.gitlab.com/qemu-project/qemu)
21 DOCKER_TESTS := $(notdir $(shell \
22         find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
24 DOCKER_TOOLS := travis
26 ENGINE := auto
28 DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py --engine $(ENGINE)
30 TESTS ?= %
31 IMAGES ?= %
33 CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
34 DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME)
36 .DELETE_ON_ERROR: $(DOCKER_SRC_COPY)
37 $(DOCKER_SRC_COPY):
38         @mkdir $@
39         $(if $(SRC_ARCHIVE), \
40                 $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \
41                         "CP", "$@/qemu.tar"), \
42                 $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
43                         "GEN", "$@/qemu.tar"))
44         $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
45                 "COPY","RUNNER")
47 docker-qemu-src: $(DOCKER_SRC_COPY)
49 docker-image: ${DOCKER_TARGETS}
51 # General rule for building docker images. If we are a sub-make
52 # invoked with SKIP_DOCKER_BUILD we still check the image is up to date
53 # though
54 ifdef SKIP_DOCKER_BUILD
55 docker-image-%: $(DOCKER_FILES_DIR)/%.docker
56         $(call quiet-command, \
57                 $(DOCKER_SCRIPT) check --quiet qemu/$* $<, \
58                 "CHECK", "$*")
59 else
60 docker-image-%: $(DOCKER_FILES_DIR)/%.docker
61         $(call quiet-command,\
62                 $(DOCKER_SCRIPT) build -t qemu/$* -f $< \
63                 $(if $V,,--quiet) \
64                 $(if $(NOCACHE),--no-cache, \
65                         $(if $(DOCKER_REGISTRY),--registry $(DOCKER_REGISTRY))) \
66                 $(if $(NOUSER),,--add-current-user) \
67                 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
68                 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
69                 "BUILD","$*")
71 # Special rule for debootstraped binfmt linux-user images
72 docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
73         $(if $(EXECUTABLE),,\
74                 $(error EXECUTABLE not set, debootstrap of debian-$* would fail))
75         $(if $(DEB_ARCH),,\
76                 $(error DEB_ARCH not set, debootstrap of debian-$* would fail))
77         $(if $(DEB_TYPE),,\
78                 $(error DEB_TYPE not set, debootstrap of debian-$* would fail))
79         $(if $(wildcard $(EXECUTABLE)),                                         \
80                 $(call quiet-command,                                           \
81                         DEB_ARCH=$(DEB_ARCH)                                    \
82                         DEB_TYPE=$(DEB_TYPE)                                    \
83                         $(if $(DEB_URL),DEB_URL=$(DEB_URL),)                    \
84                         $(DOCKER_SCRIPT) build -t qemu/debian-$* -f $<          \
85                         $(if $V,,--quiet) $(if $(NOCACHE),--no-cache)           \
86                         $(if $(NOUSER),,--add-current-user)                     \
87                         $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))       \
88                         $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
89                         "BUILD","binfmt debian-$* (debootstrapped)"),           \
90                 $(call quiet-command,                                           \
91                         $(DOCKER_SCRIPT) check --quiet qemu/debian-$* $< ||     \
92                         { echo "You will need to build $(EXECUTABLE)"; exit 1;},\
93                         "CHECK", "debian-$* exists"))
95 endif
97 # Enforce dependencies for composite images
98 ifeq ($(HOST_ARCH),x86_64)
99 docker-image-debian-amd64: docker-image-debian10
100 DOCKER_PARTIAL_IMAGES += debian-amd64-cross
101 else
102 docker-image-debian-amd64-cross: docker-image-debian10
103 DOCKER_PARTIAL_IMAGES += debian-amd64
104 endif
106 # For non-x86 hosts not all cross-compilers have been packaged
107 ifneq ($(HOST_ARCH),x86_64)
108 DOCKER_PARTIAL_IMAGES += debian-mips-cross debian-mipsel-cross debian-mips64el-cross
109 DOCKER_PARTIAL_IMAGES += debian-ppc64el-cross
110 DOCKER_PARTIAL_IMAGES += debian-s390x-cross
111 DOCKER_PARTIAL_IMAGES += fedora travis
112 endif
114 docker-image-debian-alpha-cross: docker-image-debian10
115 docker-image-debian-arm64-cross: docker-image-debian10
116 docker-image-debian-armel-cross: docker-image-debian10
117 docker-image-debian-armhf-cross: docker-image-debian10
118 docker-image-debian-hppa-cross: docker-image-debian10
119 docker-image-debian-m68k-cross: docker-image-debian10
120 docker-image-debian-mips-cross: docker-image-debian10
121 docker-image-debian-mips64-cross: docker-image-debian10
122 docker-image-debian-mips64el-cross: docker-image-debian10
123 docker-image-debian-mipsel-cross: docker-image-debian10
124 docker-image-debian-powerpc-cross: docker-image-debian10
125 docker-image-debian-ppc64-cross: docker-image-debian10
126 docker-image-debian-ppc64el-cross: docker-image-debian10
127 docker-image-debian-riscv64-cross: docker-image-debian10
128 docker-image-debian-s390x-cross: docker-image-debian10
129 docker-image-debian-sh4-cross: docker-image-debian10
130 docker-image-debian-sparc64-cross: docker-image-debian10
132 docker-image-travis: NOUSER=1
134 # Specialist build images, sometimes very limited tools
135 docker-image-debian-tricore-cross: docker-image-debian10
136 docker-image-debian-all-test-cross: docker-image-debian10
137 docker-image-debian-arm64-test-cross: docker-image-debian11
139 # These images may be good enough for building tests but not for test builds
140 DOCKER_PARTIAL_IMAGES += debian-alpha-cross
141 DOCKER_PARTIAL_IMAGES += debian-arm64-test-cross
142 DOCKER_PARTIAL_IMAGES += debian-hppa-cross
143 DOCKER_PARTIAL_IMAGES += debian-m68k-cross debian-mips64-cross
144 DOCKER_PARTIAL_IMAGES += debian-powerpc-cross debian-ppc64-cross
145 DOCKER_PARTIAL_IMAGES += debian-riscv64-cross
146 DOCKER_PARTIAL_IMAGES += debian-sh4-cross debian-sparc64-cross
147 DOCKER_PARTIAL_IMAGES += debian-tricore-cross
148 DOCKER_PARTIAL_IMAGES += debian-xtensa-cross
149 DOCKER_PARTIAL_IMAGES += fedora-i386-cross fedora-cris-cross
151 # Rules for building linux-user powered images
153 # These are slower than using native cross compiler setups but can
154 # work around issues with poorly working multi-arch systems and broken
155 # packages.
157 # Expand all the pre-requistes for each docker image and test combination
158 $(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES)), \
159         $(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
160                 $(eval .PHONY: docker-$t@$i) \
161                 $(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
162         ) \
163         $(foreach t,$(DOCKER_TESTS), \
164                 $(eval docker-all-tests: docker-$t@$i) \
165                 $(eval docker-$t: docker-$t@$i) \
166         ) \
169 docker:
170         @echo 'Build QEMU and run tests inside Docker or Podman containers'
171         @echo
172         @echo 'Available targets:'
173         @echo
174         @echo '    docker:              Print this help.'
175         @echo '    docker-all-tests:    Run all image/test combinations.'
176         @echo '    docker-TEST:         Run "TEST" on all image combinations.'
177         @echo '    docker-clean:        Kill and remove residual docker testing containers.'
178         @echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
179         @echo '                         Note: "TEST" is one of the listed test name,'
180         @echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
181         @echo '                         "IMAGE" is one of the listed container name.'
182         @echo '    docker-image:        Build all images.'
183         @echo '    docker-image-IMAGE:  Build image "IMAGE".'
184         @echo '    docker-run:          For manually running a "TEST" with "IMAGE".'
185         @echo
186         @echo 'Available container images:'
187         @echo '    $(DOCKER_IMAGES)'
188 ifneq ($(DOCKER_USER_IMAGES),)
189         @echo
190         @echo 'Available linux-user images (docker-binfmt-image-debian-%):'
191         @echo '    $(DOCKER_USER_IMAGES)'
192 endif
193         @echo
194         @echo 'Available tests:'
195         @echo '    $(DOCKER_TESTS)'
196         @echo
197         @echo 'Available tools:'
198         @echo '    $(DOCKER_TOOLS)'
199         @echo
200         @echo 'Special variables:'
201         @echo '    TARGET_LIST=a,b,c    Override target list in builds.'
202         @echo '    EXTRA_CONFIGURE_OPTS="..."'
203         @echo '                         Extra configure options.'
204         @echo '    IMAGES="a b c ..":   Filters which images to build or run.'
205         @echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
206         @echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
207         @echo '                         (default is 1)'
208         @echo '    DEBUG=1              Stop and drop to shell in the created container'
209         @echo '                         before running the command.'
210         @echo '    NETWORK=1            Enable virtual network interface with default backend.'
211         @echo '    NETWORK=$$BACKEND     Enable virtual network interface with $$BACKEND.'
212         @echo '    NOUSER               Define to disable adding current user to containers passwd.'
213         @echo '    NOCACHE=1            Ignore cache when build images.'
214         @echo '    EXECUTABLE=<path>    Include executable in image.'
215         @echo '    EXTRA_FILES="<path> [... <path>]"'
216         @echo '                         Include extra files in image.'
217         @echo '    ENGINE=auto/docker/podman'
218         @echo '                         Specify which container engine to run.'
219         @echo '    REGISTRY=url         Cache builds from registry (default:$(DOCKER_REGISTRY))'
221 # This rule if for directly running against an arbitrary docker target.
222 # It is called by the expanded docker targets (e.g. make
223 # docker-test-foo@bar) which will do additional verification.
225 # For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
227 docker-run: docker-qemu-src
228         @mkdir -p "$(DOCKER_CCACHE_DIR)"
229         @if test -z "$(IMAGE)" || test -z "$(TEST)"; \
230                 then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
231         fi
232         $(if $(EXECUTABLE),                                             \
233                 $(call quiet-command,                                   \
234                         $(DOCKER_SCRIPT) update                         \
235                         $(IMAGE) $(EXECUTABLE),                         \
236                         "  COPYING $(EXECUTABLE) to $(IMAGE)"))
237         $(call quiet-command,                                           \
238                 $(DOCKER_SCRIPT) run                                    \
239                         $(if $(NOUSER),,--run-as-current-user)          \
240                         --security-opt seccomp=unconfined               \
241                         $(if $(DEBUG),-ti,)                             \
242                         $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
243                         -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))        \
244                         -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
245                         -e V=$V -e J=$J -e DEBUG=$(DEBUG)               \
246                         -e SHOW_ENV=$(SHOW_ENV)                         \
247                         $(if $(NOUSER),,                                \
248                                 -e CCACHE_DIR=/var/tmp/ccache           \
249                                 -v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
250                         )                                               \
251                         -v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
252                         $(IMAGE)                                        \
253                         /var/tmp/qemu/run                               \
254                         $(TEST), "  RUN $(TEST) in ${IMAGE}")
255         $(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
256                 "  CLEANUP $(DOCKER_SRC_COPY)")
258 # Run targets:
260 # Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
261 docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
262 docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
263 docker-run-%:
264         @$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu/$(IMAGE)
266 docker-clean:
267         $(call quiet-command, $(DOCKER_SCRIPT) clean)