xref: /openbmc/qemu/tests/docker/Makefile.include (revision 159975f3)
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 $(EXECUTABLE),--include-executable=$(EXECUTABLE)),\
54		"BUILD","$*")
55
56# Expand all the pre-requistes for each docker image and test combination
57$(foreach i,$(DOCKER_IMAGES), \
58	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
59		$(eval .PHONY: docker-$t@$i) \
60		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
61	) \
62	$(foreach t,$(DOCKER_TESTS), \
63		$(eval docker-test: docker-$t@$i) \
64	) \
65)
66
67docker:
68	@echo 'Build QEMU and run tests inside Docker containers'
69	@echo
70	@echo 'Available targets:'
71	@echo
72	@echo '    docker:              Print this help.'
73	@echo '    docker-test:         Run all image/test combinations.'
74	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
75	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
76	@echo '                         Note: "TEST" is one of the listed test name,'
77	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
78	@echo '                         "IMAGE" is one of the listed container name."'
79	@echo '    docker-image:        Build all images.'
80	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
81	@echo
82	@echo 'Available container images:'
83	@echo '    $(DOCKER_IMAGES)'
84	@echo
85	@echo 'Available tests:'
86	@echo '    $(DOCKER_TESTS)'
87	@echo
88	@echo 'Available tools:'
89	@echo '    $(DOCKER_TOOLS)'
90	@echo
91	@echo 'Special variables:'
92	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
93	@echo '    EXTRA_CONFIGURE_OPTS="..."'
94	@echo '                         Extra configure options.'
95	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
96	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
97	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
98	@echo '                         (default is 1)'
99	@echo '    DEBUG=1              Stop and drop to shell in the created container'
100	@echo '                         before running the command.'
101	@echo '    NOCACHE=1            Ignore cache when build images.'
102	@echo '    EXECUTABLE=<path>    Include executable in image.'
103
104docker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
105docker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
106docker-run-%: docker-qemu-src
107	@mkdir -p "$(DOCKER_CCACHE_DIR)"
108	@if test -z "$(IMAGE)" || test -z "$(CMD)"; \
109		then echo "Invalid target"; exit 1; \
110	fi
111	$(if $(filter $(TESTS),$(CMD)),$(if $(filter $(IMAGES),$(IMAGE)), \
112		$(call quiet-command,\
113			if $(SRC_PATH)/tests/docker/docker.py images | \
114				awk '$$1=="qemu" && $$2=="$(IMAGE)"{found=1} END{exit(!found)}'; then \
115				$(SRC_PATH)/tests/docker/docker.py run $(if $V,,--rm) \
116				-t \
117				$(if $(DEBUG),-i,--net=none) \
118				-e TARGET_LIST=$(TARGET_LIST) \
119				-e EXTRA_CONFIGURE_OPTS=$(EXTRA_CONFIGURE_OPTS) \
120				-e V=$V -e J=$J -e DEBUG=$(DEBUG) -e SHOW_ENV=$(SHOW_ENV)\
121				-e CCACHE_DIR=/var/tmp/ccache \
122				-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
123				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
124				qemu:$(IMAGE) \
125				/var/tmp/qemu/run \
126				$(CMD); \
127			fi \
128			,"RUN","$(CMD) in $(IMAGE)")))
129
130docker-clean:
131	$(call quiet-command, $(SRC_PATH)/tests/docker/docker.py clean)
132