util/docker: Unify local build targets
[coreboot.git] / util / docker / Makefile
blob32714fcae50401136f689dcdda1cda7705e36322
1 ##
2 ## This file is part of the coreboot project.
3 ##
4 ## Copyright (C) 2016 Google, Inc.
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; version 2 of the License.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 export top=$(abspath $(CURDIR)/../..)
16 export crossgcc_version=$(shell $(top)/util/crossgcc/buildgcc --version | grep 'cross toolchain' | sed 's/^.*\sv//' | sed 's/\s.*$$//')
17 export DOCKER:=$(shell $(SHELL) -c "command -v docker")
19 # Local port to forward to the jenkins image for the test server
20 export COREBOOT_JENKINS_PORT?=49151
22 # Local cache directory - for storing files shared with the docker image
23 export COREBOOT_JENKINS_CACHE_DIR?=/srv/docker/coreboot-builder/cache
25 # Name of the jenkins container
26 export COREBOOT_JENKINS_CONTAINER?=coreboot_jenkins
28 # Version of the jenkins / sdk container
29 export COREBOOT_CONTAINER_VERSION?=$(crossgcc_version)
31 # Commit id to build from
32 export DOCKER_COMMIT?=$(shell git log -n 1 --pretty=%h)
34 # SDK architecture
35 export COREBOOT_CROSSGCC_PARAM?=all_without_gdb
37 UID ?= $(shell id -u)
38 GID ?= $(shell id -g)
40 test-docker:
41 $(if $(DOCKER),,\
42 $(warning Docker command not found. Please install docker) \
43 $(warning https://docs.docker.com/engine/installation ) \
44 $(error halting))
46 test-docker-login: test-docker
47 $(if $(shell if [ ! -f ~/.docker/config.json ]; then \
48 echo "docker authentication file not found"; fi), \
49 $(error Docker authentication file not found. Run 'docker login'))
51 coreboot-sdk: test-docker
52 @echo "Building coreboot SDK $(crossgcc_version) from commit $(DOCKER_COMMIT)"
53 cat coreboot-sdk/Dockerfile | \
54 sed "s/{{DOCKER_COMMIT}}/$(DOCKER_COMMIT)/" | \
55 sed "s/{{SDK_VERSION}}/$(COREBOOT_CONTAINER_VERSION)/" | \
56 sed "s/{{CROSSGCC_PARAM}}/$(COREBOOT_CROSSGCC_PARAM)/" | \
57 $(DOCKER) build -t coreboot/coreboot-sdk:$(COREBOOT_CONTAINER_VERSION) -
59 upload-coreboot-sdk: test-docker-login
60 $(DOCKER) push coreboot/coreboot-sdk:$(COREBOOT_CONTAINER_VERSION)
62 coreboot-jenkins-node: test-docker
63 cat coreboot-jenkins-node/Dockerfile | \
64 sed "s/{{SDK_VERSION}}/$(COREBOOT_CONTAINER_VERSION)/" | \
65 sed "s|{{SSH_KEY}}|$$(cat coreboot-jenkins-node/authorized_keys)|" | \
66 $(DOCKER) build -t coreboot/coreboot-jenkins-node:$(COREBOOT_CONTAINER_VERSION) -
68 upload-coreboot-jenkins-node: test-docker-login
69 $(DOCKER) push coreboot/coreboot-jenkins-node:$(COREBOOT_CONTAINER_VERSION)
71 docker-killall: test-docker
72 @if [ -n "$$($(DOCKER) ps | grep 'coreboot')" ]; then \
73 $(DOCKER) kill $$($(DOCKER) ps | grep 'coreboot' | cut -f1 -d ' '); \
76 clean-coreboot-containers: docker-killall
77 @if [ -n "$$($(DOCKER) ps -a | grep 'coreboot')" ]; then \
78 $(DOCKER) rm $$($(DOCKER) ps -a | grep 'coreboot' | sed 's|\s.*$$||'); \
81 clean-coreboot-images: docker-killall
82 @if [ -n "$$($(DOCKER) images | grep 'coreboot')" ]; then \
83 $(DOCKER) rmi $$($(DOCKER) images | grep coreboot | sed 's|^\S\+\s\+\S\+\s\+||' | sed 's|\s.*$$||'); \
86 docker-clean: clean-coreboot-containers
87 @$(MAKE) clean-coreboot-images
89 docker-cleanall:
90 @if [ -n "$$($(DOCKER) ps -a | grep -v "CONTAINER")" ]; then \
91 $(DOCKER) kill $$($(DOCKER) ps -a | grep -v "CONTAINER" | sed 's|\s.*$$||'); \
92 $(DOCKER) rm $$($(DOCKER) ps -a | grep -v "CONTAINER" | sed 's|\s.*$$||'); \
94 @if [ -n "$$($(DOCKER) images | grep -v "REPOSITORY")" ]; then \
95 $(DOCKER) rmi $$($(DOCKER) images | grep -v "REPOSITORY" | tr -s ' ' | cut -f3 -d ' '); \
98 docker-run-local: test-docker
99 $(DOCKER) run -u root -it -v $(top):/home/coreboot/coreboot \
100 --user $(UID):$(GID) \
101 --rm coreboot/coreboot-sdk:$(COREBOOT_CONTAINER_VERSION) \
102 /bin/bash -c "cd /home/coreboot/coreboot && $(DOCKER_RUN_LOCAL)"
104 docker-build-coreboot: docker-run-local
105 docker-build-coreboot: override DOCKER_RUN_LOCAL := \
106 make clean && make $(BUILD_CMD); rm -f .xcompile
108 docker-abuild: docker-run-local
109 docker-abuild: override DOCKER_RUN_LOCAL := \
110 make clean && util/abuild/abuild $(ABUILD_ARGS); rm -f .xcompile
112 docker-what-jenkins-does: docker-run-local
113 docker-what-jenkins-does: override DOCKER_RUN_LOCAL := \
114 make clean && make what-jenkins-does CPUS=$(CPUS)
116 docker-jenkins-server: test-docker
117 @if [ ! -d "$(COREBOOT_JENKINS_CACHE_DIR)" ]; then \
118 printf "\nError: %s does not exist.\n" "$(COREBOOT_JENKINS_CACHE_DIR)"; \
119 printf "Please run:\nsudo mkdir -p %s\n\n" "$(COREBOOT_JENKINS_CACHE_DIR)"; \
120 false; \
122 @if ! touch "$(COREBOOT_JENKINS_CACHE_DIR)/testfile" 2>/dev/null; then \
123 printf "\nError: Cannot create file in %s." "$(COREBOOT_JENKINS_CACHE_DIR)"; \
124 printf "Please run:\nsudo chown -R $(whoami):$(whoami) %s\n\n" "$(COREBOOT_JENKINS_CACHE_DIR)"; \
125 else \
126 rm -f "$(COREBOOT_JENKINS_CACHE_DIR)/testfile"; \
128 $(DOCKER) run -d --privileged --restart=always \
129 -p $(COREBOOT_JENKINS_PORT):49151 \
130 -v $(COREBOOT_JENKINS_CACHE_DIR):/data/cache \
131 --name=$(COREBOOT_JENKINS_CONTAINER) \
132 coreboot/coreboot-jenkins-node:$(COREBOOT_CONTAINER_VERSION)
134 docker-jenkins-shell: test-docker
135 @if [ ! -d $(COREBOOT_JENKINS_CACHE_DIR) ]; then printf "\nError: %s does not exist.\nPlease run:\nsudo mkdir -p %s\n\n" "$(COREBOOT_JENKINS_CACHE_DIR)" "$(COREBOOT_JENKINS_CACHE_DIR)"; false; fi
136 $(DOCKER) run -u root -it --privileged \
137 --entrypoint="/bin/bash" \
138 -p $(COREBOOT_JENKINS_PORT):49151 \
139 -v $(top):/home/coreboot/coreboot \
140 -v $(COREBOOT_JENKINS_CACHE_DIR):/data/cache \
141 --name=$(COREBOOT_JENKINS_CONTAINER) \
142 --rm coreboot/coreboot-jenkins-node:$(COREBOOT_CONTAINER_VERSION)
144 docker-shell: USER=coreboot
145 docker-shell: test-docker
146 $(DOCKER) run -u $(USER) -it \
147 -e COLUMNS=$(shell tput cols) -e LINES=$(shell tput lines) -e TERM=$(TERM) \
148 -w /home/coreboot/coreboot \
149 -v $(top):/home/coreboot/coreboot \
150 --rm coreboot/coreboot-sdk:$(COREBOOT_CONTAINER_VERSION) \
151 /bin/bash -l
153 docker-jenkins-attach: USER=root
154 docker-jenkins-attach:
155 docker exec --user $(USER) \
156 -e COLUMNS=$(shell tput cols) -e LINES=$(shell tput lines) -e TERM=$(TERM) \
157 -it "$$(docker ps | grep coreboot-jenkins-node | cut -f1 -d' ')" \
158 /bin/bash -l
160 help:
161 @echo "Commands for working with docker images:"
162 @echo " coreboot-sdk - Build coreboot-sdk container"
163 @echo " upload-coreboot-sdk - Upload coreboot-sdk to hub.docker.com"
164 @echo " coreboot-jenkins-node - Build coreboot-jenkins-node container"
165 @echo " upload-coreboot-jenkins-node - Upload coreboot-jenkins-node to hub.docker.com"
166 @echo " clean-coreboot-containers - Remove all docker coreboot containers"
167 @echo " clean-coreboot-images - Remove all docker coreboot images"
168 @echo " clean-docker - Remove docker coreboot containers & images"
169 @echo
170 @echo "Commands for using docker images"
171 @echo " docker-build-coreboot - Build coreboot under coreboot-sdk"
172 @echo " <BUILD_CMD=target> "
173 @echo " docker-abuild - Run abuild under coreboot-sdk"
174 @echo " <ABUILD_ARGS='-a -B'>"
175 @echo " docker-what-jenkins-does - Run 'what-jenkins-does' target"
176 @echo " docker-shell - Bash prompt in coreboot-jenkins-node"
177 @echo " <USER=root or USER=coreboot>"
178 @echo " docker-jenkins-server - Run coreboot-jenkins-node image (for server)"
179 @echo " docker-jenkins-attach - Open shell in running jenkins server"
180 @echo
181 @echo "Variables:"
182 @echo " COREBOOT_JENKINS_PORT=$(COREBOOT_JENKINS_PORT)"
183 @echo " COREBOOT_JENKINS_CACHE_DIR=$(COREBOOT_JENKINS_CACHE_DIR)"
184 @echo " COREBOOT_JENKINS_CONTAINER=$(COREBOOT_JENKINS_CONTAINER)"
185 @echo " COREBOOT_CONTAINER_VERSION=$(COREBOOT_CONTAINER_VERSION)"
186 @echo " DOCKER_COMMIT=$(DOCKER_COMMIT)"
188 .PHONY: test-docker test-docker-login
189 .PHONY: coreboot-jenkins-node upload-coreboot-jenkins-node
190 .PHONY: coreboot-sdk upload-coreboot-sdk
191 .PHONY: clean-coreboot-containers clean-coreboot-images
192 .PHONY: docker-abuild
193 .PHONY: docker-what-jenkins-does docker-shell docker-jenkins-server docker-jenkins-attach
194 .PHONY: help