xref: /openbmc/qemu/tests/docker/Makefile.include (revision 324027c2)
1*324027c2SFam Zheng# Makefile for Docker tests
2*324027c2SFam Zheng
3*324027c2SFam Zheng.PHONY: docker docker-test docker-clean docker-image docker-qemu-src
4*324027c2SFam Zheng
5*324027c2SFam ZhengDOCKER_SUFFIX := .docker
6*324027c2SFam ZhengDOCKER_FILES_DIR := $(SRC_PATH)/tests/docker/dockerfiles
7*324027c2SFam ZhengDOCKER_IMAGES := $(notdir $(basename $(wildcard $(DOCKER_FILES_DIR)/*.docker)))
8*324027c2SFam ZhengDOCKER_TARGETS := $(patsubst %,docker-image-%,$(DOCKER_IMAGES))
9*324027c2SFam Zheng# Use a global constant ccache directory to speed up repetitive builds
10*324027c2SFam ZhengDOCKER_CCACHE_DIR := $$HOME/.cache/qemu-docker-ccache
11*324027c2SFam Zheng
12*324027c2SFam ZhengDOCKER_TESTS := $(notdir $(shell \
13*324027c2SFam Zheng	find $(SRC_PATH)/tests/docker/ -name 'test-*' -type f))
14*324027c2SFam Zheng
15*324027c2SFam ZhengDOCKER_TOOLS := travis
16*324027c2SFam Zheng
17*324027c2SFam ZhengTESTS ?= %
18*324027c2SFam ZhengIMAGES ?= %
19*324027c2SFam Zheng
20*324027c2SFam Zheng# Make archive from git repo $1 to tar.gz $2
21*324027c2SFam Zhengmake-archive-maybe = $(if $(wildcard $1/*), \
22*324027c2SFam Zheng	$(call quiet-command, \
23*324027c2SFam Zheng		(cd $1; if git diff-index --quiet HEAD -- &>/dev/null; then \
24*324027c2SFam Zheng			git archive -1 HEAD --format=tar.gz -o $2; \
25*324027c2SFam Zheng		else \
26*324027c2SFam Zheng			git archive -1 $$(git stash create) --format=tar.gz -o $2; \
27*324027c2SFam Zheng		fi), \
28*324027c2SFam Zheng		"  ARCHIVE $(notdir $2)"))
29*324027c2SFam Zheng
30*324027c2SFam ZhengCUR_TIME := $(shell date +%Y-%m-%d-%H.%M.%S.$$$$)
31*324027c2SFam Zheng# Makes the definition constant after the first expansion
32*324027c2SFam ZhengDOCKER_SRC_COPY = $(eval DOCKER_SRC_COPY := docker-src.$(CUR_TIME))$(DOCKER_SRC_COPY)
33*324027c2SFam Zheng
34*324027c2SFam Zheng$(DOCKER_SRC_COPY):
35*324027c2SFam Zheng	@mkdir $@
36*324027c2SFam Zheng	$(call make-archive-maybe, $(SRC_PATH), $@/qemu.tgz)
37*324027c2SFam Zheng	$(call make-archive-maybe, $(SRC_PATH)/dtc, $@/dtc.tgz)
38*324027c2SFam Zheng	$(call make-archive-maybe, $(SRC_PATH)/pixman, $@/pixman.tgz)
39*324027c2SFam Zheng	$(call quiet-command, cp $(SRC_PATH)/tests/docker/run $@/run, \
40*324027c2SFam Zheng		"  COPY RUNNER")
41*324027c2SFam Zheng
42*324027c2SFam Zhengdocker-qemu-src: $(DOCKER_SRC_COPY)
43*324027c2SFam Zheng
44*324027c2SFam Zhengdocker-image: ${DOCKER_TARGETS}
45*324027c2SFam Zheng
46*324027c2SFam Zheng# General rule for building docker images
47*324027c2SFam Zhengdocker-image-%: $(DOCKER_FILES_DIR)/%.docker
48*324027c2SFam Zheng	$(call quiet-command,\
49*324027c2SFam Zheng		$(SRC_PATH)/tests/docker/docker.py build qemu:$* $< \
50*324027c2SFam Zheng		$(if $V,,--quiet) $(if $(NOCACHE),--no-cache),\
51*324027c2SFam Zheng		"  BUILD $*")
52*324027c2SFam Zheng
53*324027c2SFam Zheng# Expand all the pre-requistes for each docker image and test combination
54*324027c2SFam Zheng$(foreach i,$(DOCKER_IMAGES), \
55*324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS) $(DOCKER_TOOLS), \
56*324027c2SFam Zheng		$(eval .PHONY: docker-$t@$i) \
57*324027c2SFam Zheng		$(eval docker-$t@$i: docker-image-$i docker-run-$t@$i) \
58*324027c2SFam Zheng	) \
59*324027c2SFam Zheng	$(foreach t,$(DOCKER_TESTS), \
60*324027c2SFam Zheng		$(eval docker-test: docker-$t@$i) \
61*324027c2SFam Zheng	) \
62*324027c2SFam Zheng)
63*324027c2SFam Zheng
64*324027c2SFam Zhengdocker:
65*324027c2SFam Zheng	@echo 'Build QEMU and run tests inside Docker containers'
66*324027c2SFam Zheng	@echo
67*324027c2SFam Zheng	@echo 'Available targets:'
68*324027c2SFam Zheng	@echo
69*324027c2SFam Zheng	@echo '    docker:              Print this help.'
70*324027c2SFam Zheng	@echo '    docker-test:         Run all image/test combinations.'
71*324027c2SFam Zheng	@echo '    docker-clean:        Kill and remove residual docker testing containers.'
72*324027c2SFam Zheng	@echo '    docker-TEST@IMAGE:   Run "TEST" in container "IMAGE".'
73*324027c2SFam Zheng	@echo '                         Note: "TEST" is one of the listed test name,'
74*324027c2SFam Zheng	@echo '                         or a script name under $$QEMU_SRC/tests/docker/;'
75*324027c2SFam Zheng	@echo '                         "IMAGE" is one of the listed container name."'
76*324027c2SFam Zheng	@echo '    docker-image:        Build all images.'
77*324027c2SFam Zheng	@echo '    docker-image-IMAGE:  Build image "IMAGE".'
78*324027c2SFam Zheng	@echo
79*324027c2SFam Zheng	@echo 'Available container images:'
80*324027c2SFam Zheng	@echo '    $(DOCKER_IMAGES)'
81*324027c2SFam Zheng	@echo
82*324027c2SFam Zheng	@echo 'Available tests:'
83*324027c2SFam Zheng	@echo '    $(DOCKER_TESTS)'
84*324027c2SFam Zheng	@echo
85*324027c2SFam Zheng	@echo 'Available tools:'
86*324027c2SFam Zheng	@echo '    $(DOCKER_TOOLS)'
87*324027c2SFam Zheng	@echo
88*324027c2SFam Zheng	@echo 'Special variables:'
89*324027c2SFam Zheng	@echo '    TARGET_LIST=a,b,c    Override target list in builds.'
90*324027c2SFam Zheng	@echo '    IMAGES="a b c ..":   Filters which images to build or run.'
91*324027c2SFam Zheng	@echo '    TESTS="x y z .."     Filters which tests to run (for docker-test).'
92*324027c2SFam Zheng	@echo '    J=[0..9]*            Overrides the -jN parameter for make commands'
93*324027c2SFam Zheng	@echo '                         (default is 1)'
94*324027c2SFam Zheng	@echo '    DEBUG=1              Stop and drop to shell in the created container'
95*324027c2SFam Zheng	@echo '                         before running the command.'
96*324027c2SFam Zheng	@echo '    NOCACHE=1            Ignore cache when build images.'
97*324027c2SFam Zheng
98*324027c2SFam Zhengdocker-run-%: CMD = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\1/')
99*324027c2SFam Zhengdocker-run-%: IMAGE = $(shell echo '$@' | sed -e 's/docker-run-\([^@]*\)@\(.*\)/\2/')
100*324027c2SFam Zhengdocker-run-%: docker-qemu-src
101*324027c2SFam Zheng	@mkdir -p "$(DOCKER_CCACHE_DIR)"
102*324027c2SFam Zheng	@if test -z "$(IMAGE)" || test -z "$(CMD)"; \
103*324027c2SFam Zheng		then echo "Invalid target"; exit 1; \
104*324027c2SFam Zheng	fi
105*324027c2SFam Zheng	$(if $(filter $(TESTS),$(CMD)),$(if $(filter $(IMAGES),$(IMAGE)), \
106*324027c2SFam Zheng		$(call quiet-command,\
107*324027c2SFam Zheng			$(SRC_PATH)/tests/docker/docker.py run $(if $V,,--rm) \
108*324027c2SFam Zheng				-t \
109*324027c2SFam Zheng				$(if $(DEBUG),-i,--net=none) \
110*324027c2SFam Zheng				-e TARGET_LIST=$(TARGET_LIST) \
111*324027c2SFam Zheng				-e V=$V -e J=$J -e DEBUG=$(DEBUG)\
112*324027c2SFam Zheng				-e CCACHE_DIR=/var/tmp/ccache \
113*324027c2SFam Zheng				-v $$(realpath $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
114*324027c2SFam Zheng				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
115*324027c2SFam Zheng				-w /var/tmp/qemu \
116*324027c2SFam Zheng				qemu:$(IMAGE) \
117*324027c2SFam Zheng				$(if $V,/bin/bash -x ,) \
118*324027c2SFam Zheng				./run \
119*324027c2SFam Zheng				$(CMD); \
120*324027c2SFam Zheng			, "  RUN $(CMD) in $(IMAGE)")))
121*324027c2SFam Zheng
122*324027c2SFam Zhengdocker-clean:
123*324027c2SFam Zheng	$(call quiet-command, $(SRC_PATH)/tests/docker/docker.py clean)
124