xref: /openbmc/qemu/tests/docker/Makefile.include (revision 0b1183e3)
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_IMAGES := $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))
8DOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
9# Use a global constant ccache directory to speed up repetitive builds
10DOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
11
12DOCKER_TESTS := $(notdir $(shell \
13	find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
14
15DOCKER_TOOLS := travis
16
17TESTS ?= %
18IMAGES ?= %
19
20# Make archive from git repo $1 to tar.gz $2
21make-archive-maybe = $(if $(wildcard $1/*), \
22	$(call quiet-command, \
23		(cd $1; if git diff-index --quiet HEAD -- &>/dev/null; then \
24			git archive -1 HEAD --format=tar.gz; \
25		else \
26			git archive -1 $$(git stash create) --format=tar.gz; \
27		fi) > $2, \
28		"ARCHIVE","$(notdir $2)"))
29
30CUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
31DOCKER_SRC_COPY := docker-src.$(CUR_TIME)
32
33$(DOCKER_SRC_COPY):
34	@mkdir $@
35	$(call make-archive-maybe, $(SRC_PATH), $@/qemu.tgz)
36	$(call make-archive-maybe, $(SRC_PATH)/dtc, $@/dtc.tgz)
37	$(call make-archive-maybe, $(SRC_PATH)/pixman, $@/pixman.tgz)
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
46docker-image-%: $(DOCKER_FILES_DIR)/%.docker
47	@if test "$@" = docker-image-debian-bootstrap -a -z "$(EXECUTABLE)"; then \
48		echo WARNING: EXECUTABLE is not set, debootstrap may fail. 2>&1 ; \
49	fi
50	$(call quiet-command,\
51		$(SRC_PATH)/tests/docker/docker.py build qemu:$* $< \
52		$(if $V,,--quiet) $(if $(NOCACHE),--no-cache) \
53		$(if $(NOUSER),,--add-current-user) \
54		$(if $(EXTRA_FILES),--extra-files $(EXTRA_FILES))\
55		$(if $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
56		"BUILD","$*")
57
58# Enforce dependancies for composite images
59docker-image-debian-armhf-cross: docker-image-debian
60docker-image-debian-arm64-cross: docker-image-debian
61docker-image-debian-mipsel-cross: docker-image-debian
62
63# Expand all the pre-requistes for each docker image and test combination
64$(foreach i,$(DOCKER_IMAGES), \
65	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
66		$(eval .PHONY: docker-$t@$i) \
67		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
68	) \
69	$(foreach t,$(DOCKER_TESTS), \
70		$(eval docker-test: docker-$t@$i) \
71	) \
72)
73
74docker:
75	@echo 'Build QEMU and run tests inside Docker containers'
76	@echo
77	@echo 'Available targets:'
78	@echo
79	@echo '    docker:              Print this help.'
80	@echo '    docker-test:         Run all image/test combinations.'
81	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
82	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
83	@echo '                         Note: "TEST" is one of the listed test name,'
84	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
85	@echo '                         "IMAGE" is one of the listed container name."'
86	@echo '    docker-image:        Build all images.'
87	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
88	@echo '    docker-run:          For manually running a "TEST" with "IMAGE"'
89	@echo
90	@echo 'Available container images:'
91	@echo '    $(DOCKER_IMAGES)'
92	@echo
93	@echo 'Available tests:'
94	@echo '    $(DOCKER_TESTS)'
95	@echo
96	@echo 'Available tools:'
97	@echo '    $(DOCKER_TOOLS)'
98	@echo
99	@echo 'Special variables:'
100	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
101	@echo '    EXTRA_CONFIGURE_OPTS="..."'
102	@echo '                         Extra configure options.'
103	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
104	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
105	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
106	@echo '                         (default is 1)'
107	@echo '    DEBUG=1              Stop and drop to shell in the created container'
108	@echo '                         before running the command.'
109	@echo '    NETWORK=1            Enable virtual network interface with default backend.'
110	@echo '    NETWORK=$BACKEND     Enable virtual network interface with $BACKEND.'
111	@echo '    NOUSER               Define to disable adding current user to containers passwd.'
112	@echo '    NOCACHE=1            Ignore cache when build images.'
113	@echo '    EXECUTABLE=<path>    Include executable in image.'
114	@echo '    EXTRA_FILES="<path> [... <path>]"'
115	@echo '                         Include extra files in image.'
116
117# This rule if for directly running against an arbitrary docker target.
118# It is called by the expanded docker targets (e.g. make
119# docker-test-foo@bar) which will do additional verification.
120#
121# For example: make docker-run TEST="test-quick" IMAGE="debian:arm64" EXECUTABLE=./aarch64-linux-user/qemu-aarch64
122#
123docker-run: docker-qemu-src
124	@mkdir -p "$(DOCKER_CCACHE_DIR)"
125	@if test -z "$(IMAGE)" || test -z "$(TEST)"; \
126		then echo "Invalid target $(IMAGE)/$(TEST)"; exit 1; \
127	fi
128	$(if $(EXECUTABLE),						\
129		$(call quiet-command,					\
130			$(SRC_PATH)/tests/docker/docker.py update 	\
131			$(IMAGE) $(EXECUTABLE),				\
132			"  COPYING $(EXECUTABLE) to $(IMAGE)"))
133	$(call quiet-command,						\
134		$(SRC_PATH)/tests/docker/docker.py run 			\
135			$(if $(NOUSER),,-u $(shell id -u)) -t 		\
136			$(if $V,,--rm) 					\
137			$(if $(DEBUG),-i,)				\
138			$(if $(NETWORK),$(if $(subst $(NETWORK),,1),--net=$(NETWORK)),--net=none) \
139			-e TARGET_LIST=$(TARGET_LIST) 			\
140			-e EXTRA_CONFIGURE_OPTS="$(EXTRA_CONFIGURE_OPTS)" \
141			-e V=$V -e J=$J -e DEBUG=$(DEBUG)		\
142			-e SHOW_ENV=$(SHOW_ENV) 			\
143			-e CCACHE_DIR=/var/tmp/ccache 			\
144			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
145			-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z 	\
146			$(IMAGE) 					\
147			/var/tmp/qemu/run 				\
148			$(TEST), "  RUN $(TEST) in ${IMAGE}")
149
150# Run targets:
151#
152# Of the form docker-TEST-FOO@IMAGE-BAR which will then be expanded into a call to "make docker-run"
153docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
154docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
155docker-run-%:
156	@$(MAKE) docker-run TEST=$(CMD) IMAGE=qemu:$(IMAGE)
157
158docker-clean:
159	$(call quiet-command, $(SRC_PATH)/tests/docker/docker.py clean)
160