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