-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
303 lines (273 loc) · 11.4 KB
/
Copy pathMakefile
File metadata and controls
303 lines (273 loc) · 11.4 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# WaveControl build and installation workflow.
#
# This file intentionally uses the common GNU make/BSD make (pmake) subset so the same
# targets work with GNU make on Linux and BSD make on OpenBSD. Do not add GNU
# make functions, pattern-specific variables, or shell-specific install flags.
GO?=go
GOFMT?=gofmt
CGO_ENABLED?=0
GOFLAGS?=
GO_BUILD_FLAGS?=-trimpath
BUILD_DIR?=build
BINARY?=${BUILD_DIR}/wavecontrol
TARGET_ARCH?=amd64
DESTDIR?=
PREFIX?=/usr/local
BINDIR?=${PREFIX}/bin
SHAREDIR?=${PREFIX}/share/wavecontrol
DOCDIR?=${PREFIX}/share/doc/wavecontrol
SYSCONFDIR?=/etc
CONFDIR?=${SYSCONFDIR}/wavecontrol
ENVFILE?=${CONFDIR}/wavecontrol.env
RUNDIR?=/var/wavecontrol
SYSTEMD_UNITDIR?=${SYSCONFDIR}/systemd/system
RCDIR?=${SYSCONFDIR}/rc.d
RUN_USER?=_wavecontrol
RUN_GROUP?=_wavecontrol
INSTALL_OS?=
.NOTPARALLEL:
.PHONY: all build test vet fmt fmt-check check env env-check run clean \
cross-linux cross-openbsd cross-windows install install-user \
install-files install-config install-service enable start restart status \
stop uninstall help print-config
all: build
build:
@mkdir -p "${BUILD_DIR}"
@echo "==> building ${BINARY}"
@CGO_ENABLED=${CGO_ENABLED} ${GO} build ${GOFLAGS} ${GO_BUILD_FLAGS} -o "${BINARY}" ./cmd/server
test:
@echo "==> running Go tests"
@CGO_ENABLED=${CGO_ENABLED} ${GO} test ${GOFLAGS} ./...
vet:
@echo "==> running go vet"
@CGO_ENABLED=${CGO_ENABLED} ${GO} vet ${GOFLAGS} ./...
fmt:
@echo "==> formatting Go sources"
@${GOFMT} -w $$(find cmd internal -type f -name '*.go' -print)
fmt-check:
@echo "==> checking Go formatting"
@bad=`${GOFMT} -l $$(find cmd internal -type f -name '*.go' -print)`; \
if [ -n "$$bad" ]; then \
echo "The following files require gofmt:" >&2; \
echo "$$bad" >&2; \
exit 1; \
fi
env-check:
@cmp -s wavecontrol.env.example systemd/wavecontrol.env.example || { \
echo "systemd/wavecontrol.env.example is out of sync with wavecontrol.env.example" >&2; \
exit 1; \
}
@cmp -s wavecontrol.env.example windows/wavecontrol.env.example || { \
echo "windows/wavecontrol.env.example is out of sync with wavecontrol.env.example" >&2; \
exit 1; \
}
check: fmt-check env-check test vet
env:
@if [ -f wavecontrol.env ]; then \
chmod 0600 wavecontrol.env; \
echo "wavecontrol.env already exists; leaving its contents unchanged"; \
else \
cp wavecontrol.env.example wavecontrol.env; \
chmod 0600 wavecontrol.env; \
echo "created ./wavecontrol.env from the sample; edit it before running WaveControl"; \
fi
run: build env
@root=`pwd`; \
if [ `id -u` -eq 0 ]; then \
echo "make run is for an unprivileged development account; do not run it as root" >&2; \
exit 1; \
fi; \
mkdir -p "$$root/.wavecontrol/firmware" "$$root/.wavecontrol/backups"; \
set -a; . "$$root/wavecontrol.env"; set +a; \
exec "$$root/${BINARY}" -d -workdir "$$root/.wavecontrol" -webroot "$$root/web"
cross-linux:
@mkdir -p "${BUILD_DIR}/linux-${TARGET_ARCH}"
@echo "==> cross-building Linux/${TARGET_ARCH}"
@CGO_ENABLED=0 GOOS=linux GOARCH=${TARGET_ARCH} ${GO} build ${GOFLAGS} ${GO_BUILD_FLAGS} -o "${BUILD_DIR}/linux-${TARGET_ARCH}/wavecontrol" ./cmd/server
cross-openbsd:
@mkdir -p "${BUILD_DIR}/openbsd-${TARGET_ARCH}"
@echo "==> cross-building OpenBSD/${TARGET_ARCH}"
@CGO_ENABLED=0 GOOS=openbsd GOARCH=${TARGET_ARCH} ${GO} build ${GOFLAGS} ${GO_BUILD_FLAGS} -o "${BUILD_DIR}/openbsd-${TARGET_ARCH}/wavecontrol" ./cmd/server
cross-windows:
@mkdir -p "${BUILD_DIR}/windows-${TARGET_ARCH}"
@echo "==> cross-building Windows/${TARGET_ARCH} executable only"
@CGO_ENABLED=0 GOOS=windows GOARCH=${TARGET_ARCH} ${GO} build ${GOFLAGS} ${GO_BUILD_FLAGS} -o "${BUILD_DIR}/windows-${TARGET_ARCH}/wavecontrol.exe" ./cmd/server
@echo "Use windows/build.ps1 or windows/WaveControl.proj for a complete Windows package."
install: env-check install-user install-files install-config install-service
@echo ""
@echo "WaveControl installed."
@echo "Edit ${ENVFILE}; for a new database load ${SHAREDIR}/schema.sql, then run:"
@echo " make enable"
@echo " make start"
install-user:
@if [ -n "${DESTDIR}" ]; then \
echo "==> staged install: not creating ${RUN_USER}"; \
exit 0; \
fi; \
os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
if ! grep -q '^${RUN_GROUP}:' /etc/group; then \
case "$$os" in \
Linux) groupadd -r "${RUN_GROUP}" ;; \
OpenBSD) groupadd "${RUN_GROUP}" ;; \
*) echo "unsupported install host: $$os (supported: Linux, OpenBSD)" >&2; exit 1 ;; \
esac; \
fi; \
if ! id "${RUN_USER}" >/dev/null 2>&1; then \
case "$$os" in \
Linux) \
nologin=/usr/sbin/nologin; [ -x "$$nologin" ] || nologin=/sbin/nologin; \
useradd -r -g "${RUN_GROUP}" -d "${RUNDIR}" -s "$$nologin" -M "${RUN_USER}" ;; \
OpenBSD) \
useradd -g "${RUN_GROUP}" -d "${RUNDIR}" -s /sbin/nologin "${RUN_USER}" ;; \
*) echo "unsupported install host: $$os (supported: Linux, OpenBSD)" >&2; exit 1 ;; \
esac; \
fi; \
if [ `id -gn "${RUN_USER}"` != "${RUN_GROUP}" ]; then \
usermod -g "${RUN_GROUP}" "${RUN_USER}"; \
fi; \
home=`awk -F: -v user="${RUN_USER}" '$$1 == user { print $$6; exit }' /etc/passwd`; \
if [ "$$home" != "${RUNDIR}" ]; then \
echo "${RUN_USER} exists with home $$home; WaveControl requires ${RUNDIR}" >&2; \
exit 1; \
fi
install-files:
@test -x "${BINARY}" || { echo "${BINARY} is missing; run make before make install" >&2; exit 1; }
@echo "==> installing binary and runtime assets"
@install -d -m 0755 "${DESTDIR}${BINDIR}"
@install -m 0755 "${BINARY}" "${DESTDIR}${BINDIR}/wavecontrol"
@install -d -m 0755 "${DESTDIR}${SHAREDIR}" "${DESTDIR}${DOCDIR}"
@install -m 0644 schema.sql "${DESTDIR}${SHAREDIR}/schema.sql"
@rm -rf "${DESTDIR}${SHAREDIR}/migrations"
@install -d -m 0755 "${DESTDIR}${SHAREDIR}/migrations"
@cp -R migrations/. "${DESTDIR}${SHAREDIR}/migrations/"
@find "${DESTDIR}${SHAREDIR}/migrations" -type d -exec chmod 0755 {} \;
@find "${DESTDIR}${SHAREDIR}/migrations" -type f -exec chmod 0644 {} \;
@install -m 0644 README.md "${DESTDIR}${DOCDIR}/README.md"
@rm -rf "${DESTDIR}${DOCDIR}/docs"
@install -d -m 0755 "${DESTDIR}${DOCDIR}/docs"
@cp -R docs/. "${DESTDIR}${DOCDIR}/docs/"
@find "${DESTDIR}${DOCDIR}/docs" -type d -exec chmod 0755 {} \;
@find "${DESTDIR}${DOCDIR}/docs" -type f -exec chmod 0644 {} \;
@install -d -m 0750 "${DESTDIR}${RUNDIR}"
@rm -rf "${DESTDIR}${RUNDIR}/web.new"
@install -d -m 0755 "${DESTDIR}${RUNDIR}/web.new"
@cp -R web/. "${DESTDIR}${RUNDIR}/web.new/"
@find "${DESTDIR}${RUNDIR}/web.new" -type d -exec chmod 0755 {} \;
@find "${DESTDIR}${RUNDIR}/web.new" -type f -exec chmod 0644 {} \;
@rm -rf "${DESTDIR}${RUNDIR}/web.old"
@if [ -d "${DESTDIR}${RUNDIR}/web" ]; then mv "${DESTDIR}${RUNDIR}/web" "${DESTDIR}${RUNDIR}/web.old"; fi
@if mv "${DESTDIR}${RUNDIR}/web.new" "${DESTDIR}${RUNDIR}/web"; then \
rm -rf "${DESTDIR}${RUNDIR}/web.old"; \
else \
if [ -d "${DESTDIR}${RUNDIR}/web.old" ]; then mv "${DESTDIR}${RUNDIR}/web.old" "${DESTDIR}${RUNDIR}/web"; fi; \
exit 1; \
fi
@install -d -m 0750 "${DESTDIR}${RUNDIR}/firmware" "${DESTDIR}${RUNDIR}/backups"
@if [ -z "${DESTDIR}" ]; then \
chown root:"${RUN_GROUP}" "${RUNDIR}"; \
chown -R root:"${RUN_GROUP}" "${RUNDIR}/web"; \
chown -R "${RUN_USER}":"${RUN_GROUP}" "${RUNDIR}/firmware" "${RUNDIR}/backups"; \
fi
install-config:
@echo "==> installing environment template"
@install -d -m 0750 "${DESTDIR}${CONFDIR}"
@install -m 0640 wavecontrol.env.example "${DESTDIR}${CONFDIR}/wavecontrol.env.example"
@if [ -e "${DESTDIR}${ENVFILE}" ]; then \
chmod 0600 "${DESTDIR}${ENVFILE}"; \
echo "preserving existing ${ENVFILE}"; \
else \
install -m 0600 wavecontrol.env.example "${DESTDIR}${ENVFILE}"; \
echo "created ${ENVFILE}; it must be edited before first start"; \
fi; \
if [ -z "${DESTDIR}" ]; then chown root "${ENVFILE}"; fi
install-service:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
case "$$os" in \
Linux) \
echo "==> installing systemd unit"; \
install -d -m 0755 "${DESTDIR}${SYSTEMD_UNITDIR}"; \
install -m 0644 systemd/wavecontrol.service "${DESTDIR}${SYSTEMD_UNITDIR}/wavecontrol.service" ;; \
OpenBSD) \
echo "==> installing OpenBSD rc.d script"; \
install -d -m 0755 "${DESTDIR}${RCDIR}"; \
install -m 0555 rc.d/wavecontrol "${DESTDIR}${RCDIR}/wavecontrol" ;; \
*) echo "unsupported install host: $$os (supported: Linux, OpenBSD)" >&2; exit 1 ;; \
esac
enable:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
case "$$os" in \
Linux) systemctl daemon-reload && systemctl enable wavecontrol ;; \
OpenBSD) rcctl enable wavecontrol ;; \
*) echo "unsupported service host: $$os" >&2; exit 1 ;; \
esac
start:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
case "$$os" in \
Linux) systemctl start wavecontrol ;; \
OpenBSD) rcctl start wavecontrol ;; \
*) echo "unsupported service host: $$os" >&2; exit 1 ;; \
esac
restart:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
case "$$os" in \
Linux) systemctl daemon-reload && systemctl restart wavecontrol ;; \
OpenBSD) rcctl restart wavecontrol ;; \
*) echo "unsupported service host: $$os" >&2; exit 1 ;; \
esac
status:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
case "$$os" in \
Linux) systemctl status wavecontrol ;; \
OpenBSD) rcctl check wavecontrol ;; \
*) echo "unsupported service host: $$os" >&2; exit 1 ;; \
esac
stop:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
case "$$os" in \
Linux) systemctl stop wavecontrol ;; \
OpenBSD) rcctl stop wavecontrol ;; \
*) echo "unsupported service host: $$os" >&2; exit 1 ;; \
esac
uninstall:
@os="${INSTALL_OS}"; [ -n "$$os" ] || os=`uname -s`; \
if [ -z "${DESTDIR}" ]; then \
case "$$os" in \
Linux) systemctl disable --now wavecontrol >/dev/null 2>&1 || true ;; \
OpenBSD) rcctl stop wavecontrol >/dev/null 2>&1 || true; rcctl disable wavecontrol >/dev/null 2>&1 || true ;; \
esac; \
fi; \
case "$$os" in \
Linux) \
rm -f "${DESTDIR}${SYSTEMD_UNITDIR}/wavecontrol.service"; \
if [ -z "${DESTDIR}" ]; then systemctl daemon-reload >/dev/null 2>&1 || true; fi ;; \
OpenBSD) rm -f "${DESTDIR}${RCDIR}/wavecontrol" ;; \
*) echo "unsupported install host: $$os" >&2; exit 1 ;; \
esac
@rm -f "${DESTDIR}${BINDIR}/wavecontrol"
@rm -rf "${DESTDIR}${SHAREDIR}" "${DESTDIR}${DOCDIR}" "${DESTDIR}${RUNDIR}/web"
@echo "Preserved ${CONFDIR}, firmware, and configuration backups; remove them manually only after backing up secrets and data."
clean:
@rm -rf "${BUILD_DIR}"
print-config:
@echo "GO=${GO}"
@echo "BUILD_DIR=${BUILD_DIR}"
@echo "BINARY=${BINARY}"
@echo "DESTDIR=${DESTDIR}"
@echo "BINDIR=${BINDIR}"
@echo "SHAREDIR=${SHAREDIR}"
@echo "CONFDIR=${CONFDIR}"
@echo "RUNDIR=${RUNDIR}"
@echo "INSTALL_OS=${INSTALL_OS}"
help:
@printf '%s\n' \
'make Build WaveControl' \
'make check Run formatting, env-template, test, and vet checks' \
'make env Create ./wavecontrol.env without overwriting it' \
'make run Run a foreground development instance' \
'make cross-openbsd Cross-build OpenBSD (TARGET_ARCH=amd64 or arm64)' \
'make cross-linux Cross-build Linux (TARGET_ARCH=amd64 or arm64)' \
'make cross-windows Cross-build only the Windows executable' \
'make install Install on Linux or OpenBSD; preserves existing env/data' \
'make enable|start|restart Manage the installed system service' \
'make uninstall Remove program files, preserving config and runtime data' \
'make clean Remove build output'