xref: /openbmc/qemu/tests/docker/Makefile.include (revision 62aa1d88)
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_INTERMEDIATE_IMAGES := debian8 debian9 debian8-mxe debian-ports debian-sid
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	$(call quiet-command, cd $(SRC_PATH) && scripts/archive-source.sh $@/qemu.tar, \
31		"GEN", "$@/qemu.tar")
32	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
33		"COPY","RUNNER")
34
35docker-qemu-src: $(DOCKER_SRC_COPY)
36
37docker-image: ${DOCKER_TARGETS}
38
39# General rule for building docker images. If we are a sub-make
40# invoked with SKIP_DOCKER_BUILD we still check the image is upto date
41# though
42ifdef SKIP_DOCKER_BUILD
43docker-image-%: $(DOCKER_FILES_DIR)/%.docker
44	$(call quiet-command, \
45		$(DOCKER_SCRIPT) check --quiet qemu:$* $<, \
46		"CHECK", "$*")
47else
48docker-image-%: $(DOCKER_FILES_DIR)/%.docker
49	$(call quiet-command,\
50		$(DOCKER_SCRIPT) build qemu:$* $< \
51		$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \
52		$(if $(NOUSER),,--add-current-user) \
53		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
54		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
55		"BUILD","$*")
56
57# Special rule for debootstraped binfmt linux-user images
58docker-binfmt-image-debian-%: $(DOCKER_FILES_DIR)/debian-bootstrap.docker
59	$(if $(EXECUTABLE),,\
60		$(error EXECUTABLE not set, debootstrap of debian-$* would fail))
61	$(if $(wildcard $(EXECUTABLE)),,\
62		$(error Please build $(EXECUTABLE) first))
63	$(if $(DEB_ARCH),,\
64		$(error DEB_ARCH not set, debootstrap of debian-$* would fail))
65	$(if $(DEB_TYPE),,\
66		$(error DEB_TYPE not set, debootstrap of debian-$* would fail))
67	$(if $(filter $(QEMU_TARGET),$(TARGET_DIRS)),				\
68		$(call quiet-command,						\
69			DEB_ARCH=$(DEB_ARCH)					\
70			DEB_TYPE=$(DEB_TYPE)					\
71			$(DOCKER_SCRIPT) build qemu:debian-$* $< 		\
72			$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) 		\
73			$(if $(NOUSER),,--add-current-user) 			\
74			$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))	\
75			$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)), \
76			"BUILD","binfmt debian-$* (debootstrapped)"),		\
77		$(call quiet-command,						\
78			$(DOCKER_SCRIPT) check --quiet qemu:debian-$* $<,	\
79			"CHECK", "debian-$* exists"))
80
81endif
82
83# Enforce dependencies for composite images
84docker-image-debian: docker-image-debian9
85docker-image-debian8-mxe: docker-image-debian8
86docker-image-debian-amd64: docker-image-debian9
87docker-image-debian-armel-cross: docker-image-debian9
88docker-image-debian-armhf-cross: docker-image-debian9
89docker-image-debian-arm64-cross: docker-image-debian9
90docker-image-debian-mips-cross: docker-image-debian9
91docker-image-debian-mipsel-cross: docker-image-debian9
92docker-image-debian-mips64el-cross: docker-image-debian9
93docker-image-debian-ppc64el-cross: docker-image-debian9
94docker-image-debian-s390x-cross: docker-image-debian9
95docker-image-debian-win32-cross: docker-image-debian8-mxe
96docker-image-debian-win64-cross: docker-image-debian8-mxe
97
98# Debian SID images - we are tracking a rolling distro so we want to
99# force a re-build of the base image if we ever need to build one of
100# its children.
101ifndef SKIP_DOCKER_BUILD
102ifeq ($(HAVE_USER_DOCKER),y)
103SID_AGE=$(shell $(DOCKER_SCRIPT) check --checktype=age --olderthan=180 --quiet qemu:debian-sid)
104ifeq ($(SID_AGE),)
105else
106docker-image-debian-sid: NOCACHE=1
107endif
108endif
109endif
110
111docker-image-debian-alpha-cross: docker-image-debian-sid
112docker-image-debian-hppa-cross: docker-image-debian-sid
113docker-image-debian-m68k-cross: docker-image-debian-sid
114docker-image-debian-sh4-cross: docker-image-debian-sid
115docker-image-debian-sparc64-cross: docker-image-debian-sid
116docker-image-debian-mips64-cross: docker-image-debian-sid
117docker-image-debian-riscv64-cross: docker-image-debian-sid
118docker-image-debian-powerpc-cross: docker-image-debian-sid
119docker-image-travis: NOUSER=1
120
121# Specialist build images, sometimes very limited tools
122docker-image-tricore-cross: docker-image-debian9
123
124# Rules for building linux-user powered images
125#
126# These are slower than using native cross compiler setups but can
127# work around issues with poorly working multi-arch systems and broken
128# packages.
129
130# Jessie is the last supported release for powerpc, but multi-arch is
131# broken so we need a qemu-linux-user for this target
132docker-binfmt-image-debian-powerpc-user: DEB_ARCH = powerpc
133docker-binfmt-image-debian-powerpc-user: DEB_TYPE = jessie
134docker-binfmt-image-debian-powerpc-user: QEMU_TARGET = ppc-linux-user
135docker-binfmt-image-debian-powerpc-user: EXECUTABLE = ${BUILD_DIR}/ppc-linux-user/qemu-ppc
136docker-image-debian-powerpc-user-cross: docker-binfmt-image-debian-powerpc-user
137DOCKER_USER_IMAGES += debian-powerpc-user
138
139# Expand all the pre-requistes for each docker image and test combination
140$(foreach i,$(filter-out $(DOCKER_INTERMEDIATE_IMAGES),$(DOCKER_IMAGES) $(DOCKER_DEPRECATED_IMAGES)), \
141	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
142		$(eval .PHONY: docker-$t@$i) \
143		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
144	) \
145	$(foreach t,$(DOCKER_TESTS), \
146		$(eval docker-test: docker-$t@$i) \
147	) \
148)
149
150docker:
151	@echo 'Build QEMU and run tests inside Docker containers'
152	@echo
153	@echo 'Available targets:'
154	@echo
155	@echo '    docker:              Print this help.'
156	@echo '    docker-test:         Run all image/test combinations.'
157	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
158	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
159	@echo '                         Note: "TEST" is one of the listed test name,'
160	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
161	@echo '                         "IMAGE" is one of the listed container name."'
162	@echo '    docker-image:        Build all images.'
163	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
164	@echo '    docker-run:          For manually running a "TEST" with "IMAGE"'
165	@echo
166	@echo 'Available container images:'
167	@echo '    $(DOCKER_IMAGES)'
168ifneq ($(DOCKER_USER_IMAGES),)
169	@echo
170	@echo 'Available linux-user images (docker-binfmt-image-debian-%):'
171	@echo '    $(DOCKER_USER_IMAGES)'
172endif
173	@echo
174	@echo 'Available tests:'
175	@echo '    $(DOCKER_TESTS)'
176	@echo
177	@echo 'Available tools:'
178	@echo '    $(DOCKER_TOOLS)'
179	@echo
180	@echo 'Special variables:'
181	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
182	@echo '    EXTRA_CONFIGURE_OPTS="..."'
183	@echo '                         Extra configure options.'
184	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
185	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
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               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
198# This rule if for directly running against an arbitrary docker target.
199# It is called by the expanded docker targets (e.g. make
200# docker-test-foo@bar) which will do additional verification.
201#
202# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
203#
204docker-run: docker-qemu-src
205	@mkdir -p "$(DOCKER_CCACHE_DIR)"
206	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
207		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
208	fi
209	$(if $(EXECUTABLE),						\
210		$(call quiet-command,					\
211			$(DOCKER_SCRIPT) update 			\
212			$(IMAGE) $(EXECUTABLE),				\
213			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
214	$(call quiet-command,						\
215		$(DOCKER_SCRIPT) run 					\
216			$(if $(NOUSER),,-u $(shell id -u)) 		\
217			--security-opt seccomp=unconfined		\
218			$(if $V,,--rm) 					\
219			$(if $(DEBUG),-ti,)				\
220			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
221			-e TARGET_LIST=$(subst $(SPACE),$(COMMA),$(TARGET_LIST))	\
222			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
223			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
224			-e SHOW_ENV=$(SHOW_ENV) 			\
225			$(if $(NOUSER),,				\
226				-e CCACHE_DIR=/var/tmp/ccache 		\
227				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
228			)						\
229			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
230			$(IMAGE) 					\
231			/var/tmp/qemu/run 				\
232			$(TEST), "  RUN $(TEST) in ${IMAGE}")
233	$(call quiet-command, rm -r $(DOCKER_SRC_COPY), \
234		"  CLEANUP $(DOCKER_SRC_COPY)")
235
236# Run targets:
237#
238# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
239docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
240docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
241docker-run-%:
242	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu:$(IMAGE)
243
244docker-clean:
245	$(call quiet-command, $(DOCKER_SCRIPT) clean)
246