/
Makefile
175 lines (148 loc) · 4.77 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Inspired by the Makefile of seL4/seL4-CAmkES-L4v-dockerfiles
#
# Also based on its Makefile
#
# License of seL4/seL4-CAmkES-L4v-dockerfiles:
#
# # Copyright 2020, Data61/CSIRO
# #
# # SPDX-License-Identifier: BSD-2-Clause
#
# Docker image tool to use
DOCKER ?= docker
DOCKERHUB ?= duskmoon/dev-env:
BASE_IMG ?= base-22
RUST_IMG ?= rust-22
QEMU_DEP_IMG ?= qemu-dep-22
QEMU_DEP_RUST_IMG ?= qemu-dep-rust-22
QEMU_IMG ?= qemu7-22
QEMU_RUST_IMG ?= qemu7-rust-22
GO_IMG ?= go-22
GO_RUST_IMG ?= go-rust-22
TINA_IMG ?= tina-20
# Base images
USER_BASE_IMG ?= $(BASE_IMG)
# Interactive images
EXTRAS_IMG := dev-env-extras
USER_IMG := dev-env-$(shell whoami)
HOST_DIR ?= $(shell pwd)
# Volumes
DOCKER_VOLUME_HOME ?= $(shell whoami)-dev-env-home
# Extra vars
DOCKER_BUILD ?= $(DOCKER) build
DOCKER_FLAGS ?= --force-rm=true
ifndef EXEC
EXEC := bash
DOCKER_RUN_FLAGS += -it
endif
ETC_LOCALTIME := $(realpath /etc/localtime)
# Extra arguments to pass to `docker run` if it is or is not `podman` - these
# are constructed in a very verbose way to be obvious about why we want to do
# certain things under regular `docker` vs` podman`
# Note that `docker --version` will not say "podman" if symlinked.
CHECK_DOCKER_IS_PODMAN := $(DOCKER) --help 2>&1 | grep -q podman
IF_DOCKER_IS_PODMAN := $(CHECK_DOCKER_IS_PODMAN) && echo
IF_DOCKER_IS_NOT_PODMAN := $(CHECK_DOCKER_IS_PODMAN) || echo
# If we're not `podman` then we'll use the `-u` and `-g` options to set the
# user in the container
EXTRA_DOCKER_IS_NOT_PODMAN_RUN_ARGS := $(shell $(IF_DOCKER_IS_NOT_PODMAN) \
"-u $(shell id -u):$(shell id -g)" \
)
# If we are `podman` then we'll prefer to use `--userns=keep-id` to set up and
# use the appropriate sub{u,g}id mappings rather than end up using UID 0 in the
# container
EXTRA_DOCKER_IS_PODMAN_RUN_ARGS := $(shell $(IF_DOCKER_IS_PODMAN) \
"--userns=keep-id" \
)
# And we'll jam them into one variable to reduce noise in `docker run` lines
EXTRA_DOCKER_RUN_ARGS := $(EXTRA_DOCKER_IS_NOT_PODMAN_RUN_ARGS) \
$(EXTRA_DOCKER_IS_PODMAN_RUN_ARGS)
##########
# Making docker easier to use by mapping current user into a container
##########
.PHONY: user
user: build_user user_run
.PHONY: user_base
user_base: build_user_base user_run
.PHONY: user_rust
user_rust: build_user_rust user_run
.PHONY: user_qemu_dep
user_qemu_dep: build_user_qemu_dep user_run
.PHONY: user_qemu_dep_rust
user_qemu_dep_rust: build_user_qemu_dep_rust user_run
.PHONY: user_qemu
user_qemu: build_user_qemu user_run
.PHONY: user_qemu_rust
user_qemu_rust: build_user_qemu_rust user_run
.PHONY: user_go
user_go: build_user_go user_run
.PHONY: user_go_rust
user_go_rust: build_user_go_rust user_run
.PHONY: user_tina
user_tina: build_user_tina user_run
.PHONY: user_run
user_run:
$(DOCKER) run \
$(DOCKER_RUN_FLAGS) \
--hostname devenv-container \
--name devenv-container \
--rm \
$(EXTRA_DOCKER_RUN_ARGS) \
--group-add sudo \
-v $(HOST_DIR):/host:z \
-v $(DOCKER_VOLUME_HOME):/home/$(shell whoami):z \
-v $(ETC_LOCALTIME):/etc/localtime:ro \
$(USER_IMG) $(EXEC)
.PHONY: run_checks
run_checks:
ifeq ($(shell id -u),0)
@echo "You are running this as root (either via sudo, or directly)."
@echo "This system is designed to run under your own user account."
@echo "You can add yourself to the docker group to make this work:"
@echo " sudo su -c usermod -aG docker your_username"
@exit 1
endif
.PHONY: build_user
build_user: run_checks
$(DOCKER_BUILD) $(DOCKER_FLAGS) \
--build-arg=USER_BASE_IMG=$(DOCKERHUB)$(USER_BASE_IMG) \
-f dockerfiles/extras.Dockerfile \
-t $(EXTRAS_IMG) \
.
$(DOCKER_BUILD) $(DOCKER_FLAGS) \
--build-arg=EXTRAS_IMG=$(EXTRAS_IMG) \
--build-arg=UNAME=$(shell whoami) \
--build-arg=UID=$(shell id -u) \
--build-arg=GID=$(shell id -g) \
--build-arg=GROUP=$(shell id -gn) \
-f dockerfiles/user.Dockerfile \
-t $(USER_IMG) \
.
build_user_base: USER_BASE_IMG = $(BASE_IMG)
build_user_base: build_user
build_user_rust: USER_BASE_IMG = $(RUST_IMG)
build_user_rust: build_user
build_user_qemu_dep: USER_BASE_IMG = $(QEMU_DEP_IMG)
build_user_qemu_dep: build_user
build_user_qemu_dep_rust: USER_BASE_IMG = $(QEMU_DEP_RUST_IMG)
build_user_qemu_dep_rust: build_user
build_user_qemu: USER_BASE_IMG = $(QEMU_IMG)
build_user_qemu: build_user
build_user_qemu_rust: USER_BASE_IMG = $(QEMU_RUST_IMG)
build_user_qemu_rust: build_user
build_user_go: USER_BASE_IMG = $(GO_IMG)
build_user_go: build_user
build_user_go_rust: USER_BASE_IMG = $(GO_RUST_IMG)
build_user_go_rust: build_user
build_user_tina: USER_BASE_IMG = $(TINA_IMG)
build_user_tina: build_user
.PHONY: clean_home_dir
clean_home_dir:
$(DOCKER) volume rm $(DOCKER_VOLUME_HOME)
.PHONY: clean_data
clean_data: clean_home_dir
.PHONY: clean_images
-$(DOCKER) rmi $(USER_IMG)
-$(DOCKER) rmi $(EXTRAS_IMG)
.PHONY: clean
clean: clean_data clean_images