1# Makefile for Docker tests 2 3.PHONY: docker docker-test docker-clean docker-image docker-qemu-src 4 5DOCKER_SUFFIX := .docker 6DOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles 7DOCKER_DEPRECATED_IMAGES := debian 8# we don't run tests on intermediate images (used as base by another image) 9DOCKER_PARTIAL_IMAGES := debian debian8 debian9 debian8-mxe debian-ports debian-sid debian-bootstrap 10DOCKER_IMAGES := $(filter-out $(DOCKER_DEPRECATED_IMAGES),$(sort $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker))))) 11DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES)) 12# Use a global constant ccache directory to speed up repetitive builds 13DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache 14 15DOCKER_TESTS := $(notdir $(shell \ 16 find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f)) 17 18DOCKER_TOOLS := travis 19 20DOCKER_SCRIPT=$(SRC_PATH)/tests/docker/docker.py 21 22TESTS ?= % 23IMAGES ?= % 24 25CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$) 26DOCKER_SRC_COPY := $(BUILD_DIR)/docker-src.$(CUR_TIME) 27 28$(DOCKER_SRC_COPY): 29 @mkdir $@ 30 $(if $(SRC_ARCHIVE), \ 31 $(call quiet-command, cp "$(SRC_ARCHIVE)" $@/qemu.tar, \ 32 "CP", "$@/qemu.tar"), \ 33 $(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \ 34 "GEN", "$@/qemu.tar")) 35 $(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \ 36 "COPY","RUNNER") 37 38docker-qemu-src: $(DOCKER_SRC_COPY) 39 40docker-image: ${DOCKER_TARGETS} 41 42# General rule for building docker images. If we are a sub-make 43# invoked with SKIP_DOCKER_BUILD we still check the image is upto date 44# though 45ifdef SKIP_DOCKER_BUILD 46docker-image-%: $(DOCKER_FILES_DIR)/%.docker 47 $(call quiet-command, \ 48 $(DOCKER_SCRIPT) check --quiet qemu:$* $<, \ 49 "CHECK", "$*") 50else 51docker-image-%: $(DOCKER_FILES_DIR)/%.docker 52 $(call quiet-command,\ 53 $(DOCKER_SCRIPT) build qemu:$* $< \ 54 $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \ 55 $(if $(NOUSER),,--add-current-user) \ 56 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\ 57 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\ 58 "BUILD","$*") 59 60# Special rule for debootstraped binfmt linux-user images 61docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker 62 $(if $(EXECUTABLE),,\ 63 $(error EXECUTABLE not set, debootstrap of debian-$* would fail)) 64 $(if $(DEB_ARCH),,\ 65 $(error DEB_ARCH not set, debootstrap of debian-$* would fail)) 66 $(if $(DEB_TYPE),,\ 67 $(error DEB_TYPE not set, debootstrap of debian-$* would fail)) 68 $(if $(wildcard $(EXECUTABLE)), \ 69 $(call quiet-command, \ 70 DEB_ARCH=$(DEB_ARCH) \ 71 DEB_TYPE=$(DEB_TYPE) \ 72 $(DOCKER_SCRIPT) build qemu:debian-$* $< \ 73 $(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \ 74 $(if $(NOUSER),,--add-current-user) \ 75 $(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES)) \ 76 $(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \ 77 "BUILD","binfmt debian-$* (debootstrapped)"), \ 78 $(call quiet-command, \ 79 $(DOCKER_SCRIPT) check --quiet qemu:debian-$* $< || \ 80 { echo "You will need to build $(EXECUTABLE)"; exit 1;},\ 81 "CHECK", "debian-$* exists")) 82 83endif 84 85# Enforce dependencies for composite images 86docker-image-debian: docker-image-debian9 87docker-image-debian8-mxe: docker-image-debian8 88docker-image-debian-amd64: docker-image-debian9 89docker-image-debian-armel-cross: docker-image-debian9 90docker-image-debian-armhf-cross: docker-image-debian9 91docker-image-debian-arm64-cross: docker-image-debian9 92docker-image-debian-mips-cross: docker-image-debian9 93docker-image-debian-mipsel-cross: docker-image-debian9 94docker-image-debian-mips64el-cross: docker-image-debian9 95docker-image-debian-ppc64el-cross: docker-image-debian9 96docker-image-debian-s390x-cross: docker-image-debian9 97docker-image-debian-win32-cross: docker-image-debian8-mxe 98docker-image-debian-win64-cross: docker-image-debian8-mxe 99 100# Debian SID images - we are tracking a rolling distro so we want to 101# force a re-build of the base image if we ever need to build one of 102# its children. 103ifndef SKIP_DOCKER_BUILD 104ifeq ($(HAVE_USER_DOCKER),y) 105SID_AGE=$(shell $(DOCKER_SCRIPT) check --checktype=age --olderthan=180 --quiet qemu:debian-sid) 106ifeq ($(SID_AGE),) 107else 108docker-image-debian-sid: NOCACHE=1 109endif 110endif 111endif 112 113docker-image-debian-alpha-cross: docker-image-debian-sid 114docker-image-debian-hppa-cross: docker-image-debian-sid 115docker-image-debian-m68k-cross: docker-image-debian-sid 116docker-image-debian-sh4-cross: docker-image-debian-sid 117docker-image-debian-sparc64-cross: docker-image-debian-sid 118docker-image-debian-mips64-cross: docker-image-debian-sid 119docker-image-debian-riscv64-cross: docker-image-debian-sid 120docker-image-debian-powerpc-cross: docker-image-debian-sid 121docker-image-travis: NOUSER=1 122 123# Specialist build images, sometimes very limited tools 124docker-image-tricore-cross: docker-image-debian9 125 126# These images may be good enough for building tests but not for test builds 127DOCKER_PARTIAL_IMAGES += debian-alpha-cross debian-hppa-cross debian-m68k-cross debian-sh4-cross 128DOCKER_PARTIAL_IMAGES += debian-sparc64-cross debian-mips64-cross debian-riscv64-cross 129DOCKER_PARTIAL_IMAGES += debian-tricore-cross debian-powerpc-cross fedora-i386-cross 130 131# Rules for building linux-user powered images 132# 133# These are slower than using native cross compiler setups but can 134# work around issues with poorly working multi-arch systems and broken 135# packages. 136 137# Jessie is the last supported release for powerpc, but multi-arch is 138# broken so we need a qemu-linux-user for this target 139docker-binfmt-image-debian-powerpc-user: DEB_ARCH = powerpc 140docker-binfmt-image-debian-powerpc-user: DEB_TYPE = jessie 141docker-binfmt-image-debian-powerpc-user: EXECUTABLE = ${BUILD_DIR}/ppc-linux-user/qemu-ppc 142docker-image-debian-powerpc-user-cross: docker-binfmt-image-debian-powerpc-user 143DOCKER_USER_IMAGES += debian-powerpc-user 144 145# Expand all the pre-requistes for each docker image and test combination 146$(foreach i,$(filter-out $(DOCKER_PARTIAL_IMAGES),$(DOCKER_IMAGES) $(DOCKER_DEPRECATED_IMAGES)), \ 147 $(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \ 148 $(eval .PHONY: docker-$t@$i) \ 149 $(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \ 150 ) \ 151 $(foreach t,$(DOCKER_TESTS), \ 152 $(eval docker-all-tests: docker-$t@$i) \ 153 $(eval docker-$t: docker-$t@$i) \ 154 ) \ 155) 156 157docker: 158 @echo 'Build QEMU and run tests inside Docker containers' 159 @echo 160 @echo 'Available targets:' 161 @echo 162 @echo ' docker: Print this help.' 163 @echo ' docker-all-tests: Run all image/test combinations.' 164 @echo ' docker-TEST: Run TEST on all image combinations.' 165 @echo ' docker-clean: Kill and remove residual docker testing containers.' 166 @echo ' docker-TEST@IMAGE: Run "TEST" in container "IMAGE".' 167 @echo ' Note: "TEST" is one of the listed test name,' 168 @echo ' or a script name under $$QEMU_SRC/tests/docker/;' 169 @echo ' "IMAGE" is one of the listed container name."' 170 @echo ' docker-image: Build all images.' 171 @echo ' docker-image-IMAGE: Build image "IMAGE".' 172 @echo ' docker-run: For manually running a "TEST" with "IMAGE"' 173 @echo 174 @echo 'Available container images:' 175 @echo ' $(DOCKER_IMAGES)' 176ifneq ($(DOCKER_USER_IMAGES),) 177 @echo 178 @echo 'Available linux-user images (docker-binfmt-image-debian-%):' 179 @echo ' $(DOCKER_USER_IMAGES)' 180endif 181 @echo 182 @echo 'Available tests:' 183 @echo ' $(DOCKER_TESTS)' 184 @echo 185 @echo 'Available tools:' 186 @echo ' $(DOCKER_TOOLS)' 187 @echo 188 @echo 'Special variables:' 189 @echo ' TARGET_LIST=a,b,c Override target list in builds.' 190 @echo ' EXTRA_CONFIGURE_OPTS="..."' 191 @echo ' Extra configure options.' 192 @echo ' IMAGES="a b c ..": Filters which images to build or run.' 193 @echo ' TESTS="x y z .." Filters which tests to run (for docker-test).' 194 @echo ' J=[0..9]* Overrides the -jN parameter for make commands' 195 @echo ' (default is 1)' 196 @echo ' DEBUG=1 Stop and drop to shell in the created container' 197 @echo ' before running the command.' 198 @echo ' NETWORK=1 Enable virtual network interface with default backend.' 199 @echo ' NETWORK=$$BACKEND Enable virtual network interface with $$BACKEND.' 200 @echo ' NOUSER Define to disable adding current user to containers passwd.' 201 @echo ' NOCACHE=1 Ignore cache when build images.' 202 @echo ' EXECUTABLE=<path> Include executable in image.' 203 @echo ' EXTRA_FILES="<path> [... <path>]"' 204 @echo ' Include extra files in image.' 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 do additional verification. 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), \ 221 " COPYING $(EXECUTABLE) to $(IMAGE)")) 222 $(call quiet-command, \ 223 $(DOCKER_SCRIPT) run \ 224 $(if $(NOUSER),,-u $(shell id -u)) \ 225 --security-opt seccomp=unconfined \ 226 $(if $V,,--rm) \ 227 $(if $(DEBUG),-ti,) \ 228 $(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \ 229 -e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST)) \ 230 -e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \ 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 244# Run targets: 245# 246# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run" 247docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/') 248docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/') 249docker-run-%: 250 @$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu:$(IMAGE) 251 252docker-clean: 253 $(call quiet-command, $(DOCKER_SCRIPT) clean) 254