-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
343 lines (316 loc) · 8.91 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
SHELL := /bin/bash
ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
UMASK := 022
VERSION := 0.7.0
ITERATION := 1
REVISION := $(shell cd '$(ROOT)' && git rev-parse --short HEAD)
ENVIRONMENT ?= production
GO111MODULE := on
ARCHITECTURE := $(shell go env GOARCH)
export GO111MODULE
# Precompiled DuckDB static bundles work fine for all platforms except RHEL 8
# due to an outdated glibc version. In this case, the bundle is built on demand
# (see the 'duckdb-static-bundle' target). This is a costly operation, so GitHub
# Actions caching is used to populate 'duckdb-static-bundle/libduckdb_bundle.a'
# and avoid rebuilding the bundle on every run (see
# 'extras/github/build-action/action.yml').
ifeq ($(PLATFORM),rhel8)
CGO_LDFLAGS=-lstdc++ -lm -ldl -lduckdb_bundle -L$(ROOT)/duckdb-static-bundle/
GO_BUILD_TAGS=duckdb_use_static_lib
else
CGO_LDFLAGS=
GO_BUILD_TAGS=
endif
export CGO_LDFLAGS
FPM = \
fpm -s dir \
--name varnishmon \
--version '$(VERSION)' \
--architecture '$(ARCHITECTURE)' \
--description 'varnishmon' \
--maintainer 'Allenta Consulting S.L. <[email protected]>' \
--vendor 'Allenta Consulting S.L. <[email protected]>' \
--url 'https://github.com/allenta/varnishmon' \
--license 'BSD 2-Clause License' \
--config-files /etc/varnish/varnishmon.yml
.PHONY: build
build: mrproper duckdb-static-bundle
@( \
set -e; \
\
export LD_FLAGS="\
-X github.com/allenta/varnishmon/pkg/config.version=$(VERSION) \
-X github.com/allenta/varnishmon/pkg/config.revision=$(REVISION) \
-X github.com/allenta/varnishmon/pkg/config.environment=$(ENVIRONMENT) \
-s -w"; \
export CGO_ENABLED=1; \
\
echo '> Building...'; \
for CMD in '$(ROOT)/cmd/'*; do \
echo "- $$CMD (linux $(ARCHITECTURE))"; \
GOOS=linux GOARCH=$(ARCHITECTURE) go build \
-tags=$(GO_BUILD_TAGS) \
-trimpath \
-ldflags "$$LD_FLAGS" \
-o build/bin/$${CMD##*/} \
./cmd/$${CMD##*/}; \
done; \
)
.PHONY: fmt
fmt:
@( \
set -e; \
\
echo '> Running goimports (includes gofmt)...'; \
FILES=$$(find '$(ROOT)/cmd' '$(ROOT)/pkg' -name '*.go'); \
for FILE in $$(go tool goimports -l $$FILES); do \
echo "- $$FILE"; \
go tool goimports -w "$$FILE"; \
done; \
)
.PHONY: lint
lint:
@( \
set -e; \
\
if [ ! -f "$$HOME/go/bin/golangci-lint" ]; then \
echo '> Installing golangci-lint...'; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.64.5; \
fi; \
\
echo '> Running golangci-lint...'; \
~/go/bin/golangci-lint cache clean; \
~/go/bin/golangci-lint run --timeout 5m '$(ROOT)/cmd/...' '$(ROOT)/pkg/...'; \
)
.PHONY: vet
vet:
@( \
set -e; \
\
echo '> Running go vet...'; \
go vet ./...; \
)
.PHONY: modernize
modernize:
@( \
set -e; \
\
echo '> Running modernize...'; \
go tool modernize -fix ./...; \
)
TEST_PACKAGES ?= '$(ROOT)/...'
TEST_PATTERN ?= .
.PHONY: test
test: duckdb-static-bundle
@( \
set -e; \
\
echo '> Running tests...'; \
go test \
$(TEST_PACKAGES) \
-failfast \
-race \
-coverprofile=$(ROOT)/coverage.txt \
-covermode=atomic \
-run '$(TEST_PATTERN)' \
-tags=$(GO_BUILD_TAGS) \
-timeout=2m; \
)
.PHONY: mod
mod:
@( \
set -e; \
\
echo '> Adding missing and removing unused modules...'; \
go mod tidy -compat=1.24; \
\
echo '> Printing module requirement graph...'; \
go mod graph; \
)
.PHONY: mocks
mocks:
@( \
set -e; \
\
echo '> Removing previous mocks from Git...'; \
find '$(ROOT)/pkg' -name 'mock_*.go' -type f -exec git rm -f {} \;; \
\
echo '> Running mockery...'; \
go tool mockery; \
\
echo '> Add new mocks to Git...'; \
find '$(ROOT)/pkg' -name 'mock_*.go' -type f -exec git add -v {} \;; \
)
.PHONY: dist
dist: build
@( \
set -e; \
umask $(UMASK); \
\
[ '$(PLATFORM)' = 'noble' -o '$(PLATFORM)' = 'jammy' -o \
'$(PLATFORM)' = 'bookworm' -o '$(PLATFORM)' = 'rhel9' -o \
'$(PLATFORM)' = 'rhel8' ] || \
{ echo >&2 'Invalid platform ($(PLATFORM))'; exit 1; }; \
\
[ '$(ENVIRONMENT)' = 'production' ] || \
{ echo >&2 'Invalid environment ($(ENVIRONMENT))'; exit 1; }; \
\
echo '> Building distribution...'; \
\
mkdir -p \
'$(ROOT)/build/dist/usr/bin' \
'$(ROOT)/build/dist/etc/varnish' \
'$(ROOT)/build/dist/var/log/varnishmon' \
'$(ROOT)/build/dist/var/lib/varnishmon' \
'$(ROOT)/build/dist/usr/share/doc/varnishmon' \
'$(ROOT)/build/dist/usr/share/man/man1'; \
\
find '$(ROOT)/build/bin/' -type f ! -name helper \
-exec cp -t '$(ROOT)/build/dist/usr/bin/' {} +; \
\
cp '$(ROOT)/extras/packaging/varnishmon.yml' '$(ROOT)/build/dist/etc/varnish/'; \
\
cp \
'$(ROOT)/README.md' \
'$(ROOT)/LICENSE.txt' \
'$(ROOT)/CHANGELOG.md' \
'$(ROOT)/build/dist/usr/share/doc/varnishmon/'; \
\
'$(ROOT)/build/bin/helper' man 1 '$(ROOT)/build/dist/usr/share/man/man1/'; \
gzip '$(ROOT)/build/dist/usr/share/man/man1/'*.1; \
)
ifeq ($(PLATFORM),$(filter $(PLATFORM),noble jammy bookworm))
@( \
set -e; \
umask $(UMASK); \
\
mkdir -p \
'$(ROOT)/build/dist/etc/default' \
'$(ROOT)/build/dist/etc/logrotate.d' \
'$(ROOT)/build/dist/lib/systemd/system'; \
\
cp '$(ROOT)/extras/packaging/varnishmon.params' '$(ROOT)/build/dist/etc/default/varnishmon'; \
cp '$(ROOT)/extras/packaging/debian/varnishmon.logrotate' '$(ROOT)/build/dist/etc/logrotate.d/varnishmon'; \
cp '$(ROOT)/extras/packaging/debian/varnishmon.service' '$(ROOT)/build/dist/lib/systemd/system/'; \
)
else ifeq ($(PLATFORM),$(filter $(PLATFORM),rhel9 rhel8))
@( \
set -e; \
umask $(UMASK); \
\
mkdir -p \
'$(ROOT)/build/dist/etc/sysconfig' \
'$(ROOT)/build/dist/etc/logrotate.d' \
'$(ROOT)/build/dist/lib/systemd/system'; \
\
cp '$(ROOT)/extras/packaging/varnishmon.params' '$(ROOT)/build/dist/etc/sysconfig/varnishmon'; \
cp '$(ROOT)/extras/packaging/redhat/varnishmon.logrotate' '$(ROOT)/build/dist/etc/logrotate.d/varnishmon'; \
cp '$(ROOT)/extras/packaging/redhat/varnishmon.service' '$(ROOT)/build/dist/lib/systemd/system/'; \
)
endif
@( \
set -e; \
umask $(UMASK); \
\
NAME='varnishmon-$(VERSION)-$(ITERATION)-$(REVISION)-$(PLATFORM)-$(ARCHITECTURE)'; \
cd '$(ROOT)/build'; \
tar cfz "$$NAME.tgz" \
--transform "s,^.*/,$$NAME/," \
dist/usr/bin/* \
dist/usr/share/doc/varnishmon/*; \
)
.PHONY: package
package: dist
ifeq ($(PLATFORM),$(filter $(PLATFORM),noble jammy bookworm))
@( \
set -e; \
umask $(UMASK); \
\
echo '> Building package...'; \
\
cd '$(ROOT)/build'; \
$(FPM) -t deb \
--iteration '$(ITERATION)+$(PLATFORM)' \
--after-install '$(ROOT)/extras/packaging/debian/varnishmon.postinst' \
--before-remove '$(ROOT)/extras/packaging/debian/varnishmon.prerm' \
--depends logrotate \
--config-files /etc/default/varnishmon \
--config-files /etc/logrotate.d/varnishmon \
-C '$(ROOT)/build/dist' .; \
)
else ifeq ($(PLATFORM),$(filter $(PLATFORM),rhel9 rhel8))
@( \
set -e; \
umask $(UMASK); \
\
echo '> Building package...'; \
\
cd '$(ROOT)/build'; \
$(FPM) -t rpm \
--iteration '$(ITERATION).$(PLATFORM)' \
--after-install '$(ROOT)/extras/packaging/redhat/varnishmon.postinst' \
--before-remove '$(ROOT)/extras/packaging/redhat/varnishmon.prerm' \
--depends logrotate \
--config-files /etc/sysconfig/varnishmon \
--config-files /etc/logrotate.d/varnishmon \
-C '$(ROOT)/build/dist' .; \
)
endif
define WEB_NPM_TASK
@( \
set -e; \
cd '$(ROOT)/assets/web'; \
npm install; \
npm run $(1); \
)
endef
.PHONY: web-serve
web-serve:
$(call WEB_NPM_TASK,serve)
.PHONY: web-watch
web-watch:
$(call WEB_NPM_TASK,watch)
.PHONY: web-build
web-build:
$(call WEB_NPM_TASK,build)
.PHONY: web-lint
web-lint:
$(call WEB_NPM_TASK,lint)
.PHONY: web-prettier
web-prettier:
$(call WEB_NPM_TASK,prettier)
# Beware of the hardcoded DuckDB version in the 'git clone' command. See:
# https://github.com/marcboeker/go-duckdb/blob/main/.github/workflows/deps.yaml.
.PHONY: duckdb-static-bundle
duckdb-static-bundle:
@( \
set -e; \
\
if [ ! -f '$(ROOT)/duckdb-static-bundle/libduckdb_bundle.a' ]; then \
if [ '$(PLATFORM)' = 'rhel8' ]; then \
echo '> Building DuckDB static bundle for RHEL8...'; \
rm -rf /tmp/duckdb; \
git clone -b v1.1.3 --depth 1 https://github.com/duckdb/duckdb.git /tmp/duckdb; \
cd /tmp/duckdb; \
CFLAGS='-O3' \
CXXFLAGS='-O3' \
BUILD_SHELL=0 \
BUILD_UNITTESTS=0 \
DUCKDB_PLATFORM=any \
ENABLE_EXTENSION_AUTOLOADING=1 \
ENABLE_EXTENSION_AUTOINSTALL=1 \
BUILD_EXTENSIONS='json' \
make bundle-library -j 2; \
\
mkdir -p '$(ROOT)/duckdb-static-bundle'; \
cp /tmp/duckdb/build/release/libduckdb_bundle.a '$(ROOT)/duckdb-static-bundle'; \
fi; \
fi; \
)
.PHONY: mrproper
mrproper:
@( \
echo '> Cleaning up...'; \
rm -rf '$(ROOT)/build'; \
git clean -f -x -d -e .env -e assets/web/node_modules -e duckdb-static-bundle -e varnishmon.db $(ROOT); \
)