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