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