-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
212 lines (189 loc) · 6.5 KB
/
Makefile
File metadata and controls
212 lines (189 loc) · 6.5 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
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) Lewis Cook <lcook@FreeBSD.org>
.POSIX:
VERSION= 0.2.0
CONFIG= config.yaml
GH_ACCOUNT= lcook
GH_PROJECT= pulsar
OSNAME= ${.MAKE.OS}
.if ${OSNAME} == FreeBSD
PREFIX?= /usr/local
.elif ${OSNAME} == Linux
PREFIX?= /usr
.else
.error ${.newline}=> ${OSNAME} is an unsupported OS
.endif
ETCDIR= ${PREFIX}/etc
CFGDIR= ${ETCDIR}/${GH_PROJECT}
.if ${OSNAME} == FreeBSD
RCDIR= ${ETCDIR}/rc.d
RCCFG= ${GH_PROJECT}.in
.endif
BINDIR= ${PREFIX}/bin
SBINDIR= ${PREFIX}/sbin
GIT_CMD= ${BINDIR}/git
.if exists(${.CURDIR}/.git)
. if exists(${GIT_CMD})
GIT_REPO=
. else
.error ${.newline}=> git directory found '${.CURDIR}/.git' but '${GIT_CMD}' ${.newline} not found on the system. Check if `PREFIX` is set correctly and ${.newline} whether the accompanying git package is installed
. endif
.endif
GO_CMD= ${BINDIR}/go
GOLANGCI_CMD= ${BINDIR}/golangci-lint
PODMAN_CMD= ${BINDIR}/podman
BUILD_DEPENDS= ${GO_CMD}
CONTAINER_DEPENDS= ${PODMAN_CMD}
.if defined(GIT_REPO)
GIT_HASH!= git rev-parse --short HEAD
GIT_BRANCH!= git symbolic-ref HEAD 2>/dev/null | sed 's,refs/heads/,,'
GIT_DIRTY!= git status --porcelain
. if ${GIT_DIRTY}
GIT_HASH:= ${GIT_HASH}-dirty
. endif
VERSION:= ${GIT_BRANCH}/${VERSION}-${GIT_HASH}
.endif
GO_MODULE= github.com/${GH_ACCOUNT}/${GH_PROJECT}
GO_FLAGS= -v -ldflags \
"-s -w -X ${GO_MODULE}/internal/version.Build=${VERSION}"
.if ${OSNAME} == FreeBSD
PODMAN_ARGS= --network=host
.endif
OCI_REPO?= localhost
OCI_TAG= ${OCI_REPO}/${GH_PROJECT}:${GIT_HASH}
.if ${OCI_REPO} != localhost
OCI_TAG= ${OCI_REPO}/${GH_ACCOUNT}/${GH_PROJECT}:${GIT_HASH}
.endif
PROGRAMS?= bot relay
default: build
build: build-requirements
@echo -------------------------------------------------------------------
@echo ">>> Building ${GH_PROJECT}@${VERSION} for ${OSNAME}"
@echo -------------------------------------------------------------------
.for prog in ${PROGRAMS}
GOOS=${OSNAME:tl} ${GO_CMD} build ${GO_FLAGS} -o ${GH_PROJECT}-${prog} cmd/${GH_PROJECT}-${prog}/${prog}.go\
&& strip -s ${GH_PROJECT}-${prog}
.endfor
@echo
run: build-requirements
@echo -------------------------------------------------------------------
@echo ">>> Running ${GH_PROJECT}@${VERSION}"
@echo -------------------------------------------------------------------
${GO_CMD} run ${GO_FLAGS} cmd/pulsar-bot/bot.go -V 2
clean:
@echo -------------------------------------------------------------------
@echo ">>> Cleaning up project root directory"
@echo -------------------------------------------------------------------
${GO_CMD} clean
.for prog in ${PROGRAMS}
rm -f ${GH_PROJECT}-${prog}
.endfor
@echo
install: build
@echo -------------------------------------------------------------------
@echo ">>> Installing ${GH_PROJECT}@${VERSION} and configuration file"
@echo -------------------------------------------------------------------
.if !exists(${CFGDIR})
mkdir -p ${CFGDIR}
.endif
.if exists(${CONFIG})
@echo "=> No configuration file \`${CONFIG}\` found in project root directory"
@echo " You may use the example configuration \`config.example.yaml\` to get"
@echo " started. Make sure to rename the example afterwards accordingly and"
@echo " reinstall, or copy to the directory \`${CFGDIR}\`"
@sleep 4
.else
install -m600 ${CONFIG} ${CFGDIR}
.endif
.for prog in ${PROGRAMS}
install -m755 ${GH_PROJECT}-${prog} ${SBINDIR}
.endfor
.if ${OSNAME} == FreeBSD
install -m755 ${RCCFG} ${RCDIR}/${RCCFG:C/\.in//}
.endif
@echo
deinstall:
@echo -------------------------------------------------------------------
@echo ">>> Deinstalling ${GH_PROJECT}@${VERSION}"
@echo -------------------------------------------------------------------
rm -rfv ${CFGDIR}
.for prog in ${PROGRAMS}
rm -fv ${SBINDIR}/${GH_PROJECT}-${prog}
.endfor
.if ${OSNAME} == FreeBSD
echo rm -f ${RCDIR}/${RCCFG:C/\.in//}
.endif
@echo
container:
@echo -------------------------------------------------------------------
@echo ">>> Building ${GH_PROJECT}@${VERSION} container images for ${OSNAME}"
@echo -------------------------------------------------------------------
.if !exists(container/${OSNAME}-bot)
@echo "=> '${OSNAME}' is an unsupported operating system"
@false
.endif
.for prog in ${PROGRAMS}
@if [ ${OCI_REPO} != localhost ]; then \
TAG_NAME=${OCI_TAG:S/${GH_PROJECT}/${GH_PROJECT}\/${prog}/}; \
else \
TAG_NAME=${OCI_TAG:S/${GH_PROJECT}/${GH_PROJECT}-${prog}/}; \
fi; \
${PODMAN_CMD} build ${PODMAN_ARGS} --file container/${OSNAME}-${prog} --tag $$TAG_NAME .
.endfor
@echo
container-publish: container-requirements
@echo -------------------------------------------------------------------
@echo ">>> Publishing container images to ${OCI_REPO}"
@echo -------------------------------------------------------------------
.for prog in ${PROGRAMS}
@if [ ${OCI_REPO} != localhost ]; then \
TAG_NAME=${OCI_TAG:S/${GH_PROJECT}/${GH_PROJECT}\/${prog}/}; \
else \
TAG_NAME=${OCI_TAG:S/${GH_PROJECT}/${GH_PROJECT}-${prog}/}; \
fi; \
${PODMAN_CMD} push $$TAG_NAME
.endfor
@echo
update:
@echo -------------------------------------------------------------------
@echo ">>> Updating and tidying up Go dependencies"
@echo -------------------------------------------------------------------
${GO_CMD} get -u -v ./...
${GO_CMD} mod tidy -v
${GO_CMD} mod verify
@echo
test:
@echo -------------------------------------------------------------------
@echo ">>> Running Go unit tests"
@echo -------------------------------------------------------------------
${GO_CMD} test -v ./...
@echo
lint:
@echo -------------------------------------------------------------------
@echo ">>> Linting Go files"
@echo -------------------------------------------------------------------
.if !exists(${GOLANGCI_CMD})
@echo "=> golangci-lint binary \`${GOLANGCI_CMD}\` not found on host"
@echo " Check if \`PREFIX\` is set correctly and whether the accompanying package is installed"
@false
.endif
${GOLANGCI_CMD} run
@echo
build-requirements:
.for dep in ${BUILD_DEPENDS}
. if !exists(${dep})
@echo "=> Build dependency '${dep}' not found. Check if `PREFIX` is"
@echo " set correctly and whether the accompanying package is installed"
@false
. endif
.endfor
container-requirements:
.for dep in ${CONTAINER_DEPENDS}
. if !exists(${dep})
@echo "=> Container dependency '${dep}' not found. Check if \`PREFIX\` is"
@echo " set correctly and whether the accompanying package is installed"
@false
. endif
.endfor
.PHONY: build run clean install deinstall container container-publish update test lint build-requirements container-requirements