xref: /openbmc/qemu/tests/docker/Makefile.include (revision ff31e225)
1324027c2SFam Zheng# Makefile for Docker tests
2324027c2SFam Zheng
3324027c2SFam Zheng.PHONY: docker docker-test docker-clean docker-image docker-qemu-src
4324027c2SFam Zheng
5324027c2SFam ZhengDOCKER_SUFFIX := .docker
6324027c2SFam ZhengDOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
7324027c2SFam ZhengDOCKER_IMAGES := $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))
8324027c2SFam ZhengDOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
9324027c2SFam Zheng# Use a global constant ccache directory to speed up repetitive builds
10324027c2SFam ZhengDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
11324027c2SFam Zheng
12324027c2SFam ZhengDOCKER_TESTS := $(notdir $(shell \
13324027c2SFam Zheng	find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
14324027c2SFam Zheng
15324027c2SFam ZhengDOCKER_TOOLS := travis
16324027c2SFam Zheng
17324027c2SFam ZhengTESTS ?= %
18324027c2SFam ZhengIMAGES ?= %
19324027c2SFam Zheng
20324027c2SFam Zheng# Make archive from git repo $1 to tar.gz $2
21324027c2SFam Zhengmake-archive-maybe = $(if $(wildcard $1/*), \
22324027c2SFam Zheng	$(call quiet-command, \
23324027c2SFam Zheng		(cd $1; if git diff-index --quiet HEAD -- &>/dev/null; then \
2434c98c54SPaolo Bonzini			git archive -1 HEAD --format=tar.gz; \
25324027c2SFam Zheng		else \
2634c98c54SPaolo Bonzini			git archive -1 $$(git stash create) --format=tar.gz; \
2734c98c54SPaolo Bonzini		fi) > $2, \
28324027c2SFam Zheng		"  ARCHIVE $(notdir $2)"))
29324027c2SFam Zheng
30324027c2SFam ZhengCUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
31d5aebef8SEduardo HabkostDOCKER_SRC_COPY := docker-src.$(CUR_TIME)
32324027c2SFam Zheng
33324027c2SFam Zheng$(DOCKER_SRC_COPY):
34324027c2SFam Zheng	@mkdir $@
35324027c2SFam Zheng	$(call make-archive-maybe, $(SRC_PATH), $@/qemu.tgz)
36324027c2SFam Zheng	$(call make-archive-maybe, $(SRC_PATH)/dtc, $@/dtc.tgz)
37324027c2SFam Zheng	$(call make-archive-maybe, $(SRC_PATH)/pixman, $@/pixman.tgz)
38324027c2SFam Zheng	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
39324027c2SFam Zheng		"  COPY RUNNER")
40324027c2SFam Zheng
41324027c2SFam Zhengdocker-qemu-src: $(DOCKER_SRC_COPY)
42324027c2SFam Zheng
43324027c2SFam Zhengdocker-image: ${DOCKER_TARGETS}
44324027c2SFam Zheng
45324027c2SFam Zheng# General rule for building docker images
46324027c2SFam Zhengdocker-image-%: $(DOCKER_FILES_DIR)/%.docker
47324027c2SFam Zheng	$(call quiet-command,\
48324027c2SFam Zheng		$(SRC_PATH)/tests/docker/docker.py build qemu:$* $< \
49324027c2SFam Zheng		$(if $V,,--quiet) $(if $(NOCACHE),--no-cache),\
50324027c2SFam Zheng		"  BUILD $*")
51324027c2SFam Zheng
52324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination
53324027c2SFam Zheng$(foreach i,$(DOCKER_IMAGES), \
54324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
55324027c2SFam Zheng		$(eval .PHONY: docker-$t@$i) \
56324027c2SFam Zheng		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
57324027c2SFam Zheng	) \
58324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS), \
59324027c2SFam Zheng		$(eval docker-test: docker-$t@$i) \
60324027c2SFam Zheng	) \
61324027c2SFam Zheng)
62324027c2SFam Zheng
63324027c2SFam Zhengdocker:
64324027c2SFam Zheng	@echo 'Build QEMU and run tests inside Docker containers'
65324027c2SFam Zheng	@echo
66324027c2SFam Zheng	@echo 'Available targets:'
67324027c2SFam Zheng	@echo
68324027c2SFam Zheng	@echo '    docker:              Print this help.'
69324027c2SFam Zheng	@echo '    docker-test:         Run all image/test combinations.'
70324027c2SFam Zheng	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
71324027c2SFam Zheng	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
72324027c2SFam Zheng	@echo '                         Note: "TEST" is one of the listed test name,'
73324027c2SFam Zheng	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
74324027c2SFam Zheng	@echo '                         "IMAGE" is one of the listed container name."'
75324027c2SFam Zheng	@echo '    docker-image:        Build all images.'
76324027c2SFam Zheng	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
77324027c2SFam Zheng	@echo
78324027c2SFam Zheng	@echo 'Available container images:'
79324027c2SFam Zheng	@echo '    $(DOCKER_IMAGES)'
80324027c2SFam Zheng	@echo
81324027c2SFam Zheng	@echo 'Available tests:'
82324027c2SFam Zheng	@echo '    $(DOCKER_TESTS)'
83324027c2SFam Zheng	@echo
84324027c2SFam Zheng	@echo 'Available tools:'
85324027c2SFam Zheng	@echo '    $(DOCKER_TOOLS)'
86324027c2SFam Zheng	@echo
87324027c2SFam Zheng	@echo 'Special variables:'
88324027c2SFam Zheng	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
8935e0f959SFam Zheng	@echo '    EXTRA_CONFIGURE_OPTS="..."'
9035e0f959SFam Zheng	@echo '                         Extra configure options.'
91324027c2SFam Zheng	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
92324027c2SFam Zheng	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
93324027c2SFam Zheng	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
94324027c2SFam Zheng	@echo '                         (default is 1)'
95324027c2SFam Zheng	@echo '    DEBUG=1              Stop and drop to shell in the created container'
96324027c2SFam Zheng	@echo '                         before running the command.'
97324027c2SFam Zheng	@echo '    NOCACHE=1            Ignore cache when build images.'
98324027c2SFam Zheng
99324027c2SFam Zhengdocker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
100324027c2SFam Zhengdocker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
101324027c2SFam Zhengdocker-run-%: docker-qemu-src
102324027c2SFam Zheng	@mkdir -p "$(DOCKER_CCACHE_DIR)"
103324027c2SFam Zheng	@if test -z "$(IMAGE)" || test -z "$(CMD)"; \
104324027c2SFam Zheng		then echo "Invalid target"; exit 1; \
105324027c2SFam Zheng	fi
106324027c2SFam Zheng	$(if $(filter $(TESTS),$(CMD)),$(if $(filter $(IMAGES),$(IMAGE)), \
107324027c2SFam Zheng		$(call quiet-command,\
108*ff31e225SFam Zheng			if $(SRC_PATH)/tests/docker/docker.py images \
109*ff31e225SFam Zheng				--format={{.Repository}}:{{.Tag}} | \
110*ff31e225SFam Zheng					grep -qx qemu:$(IMAGE); then \
111324027c2SFam Zheng				$(SRC_PATH)/tests/docker/docker.py run $(if $V,,--rm) \
112324027c2SFam Zheng				-t \
113324027c2SFam Zheng				$(if $(DEBUG),-i,--net=none) \
114324027c2SFam Zheng				-e TARGET_LIST=$(TARGET_LIST) \
11535e0f959SFam Zheng				-e EXTRA_CONFIGURE_OPTS=$(EXTRA_CONFIGURE_OPTS) \
116324027c2SFam Zheng				-e V=$V -e J=$J -e DEBUG=$(DEBUG)\
117324027c2SFam Zheng				-e CCACHE_DIR=/var/tmp/ccache \
118324027c2SFam Zheng				-v $$(realpath $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
119324027c2SFam Zheng				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
120324027c2SFam Zheng				qemu:$(IMAGE) \
121c8158513SFam Zheng				/var/tmp/qemu/run \
122324027c2SFam Zheng				$(CMD); \
123*ff31e225SFam Zheng			fi \
124324027c2SFam Zheng			, "  RUN $(CMD) in $(IMAGE)")))
125324027c2SFam Zheng
126324027c2SFam Zhengdocker-clean:
127324027c2SFam Zheng	$(call quiet-command, $(SRC_PATH)/tests/docker/docker.py clean)
128