-
Notifications
You must be signed in to change notification settings - Fork 31
/
Makefile
243 lines (210 loc) · 7 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
#!/usr/bin/make -f
PROJECT=lmd
MAKE:=make
SHELL:=bash
GOVERSION:=$(shell \
go version | \
awk -F'go| ' '{ split($$5, a, /\./); printf ("%04d%04d", a[1], a[2]); exit; }' \
)
# also update README.md and .github/workflows/citest.yml when changing minumum version
# find . -name go.mod
MINGOVERSION:=00010022
MINGOVERSIONSTR:=1.22
BUILD:=$(shell git rev-parse --short HEAD)
# see https://github.com/go-modules-by-example/index/blob/master/010_tools/README.md
# and https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
TOOLSFOLDER=$(shell pwd)/tools
export GOBIN := $(TOOLSFOLDER)
export PATH := $(GOBIN):$(PATH)
BUILD_FLAGS=-ldflags "-s -w -X main.Build=$(BUILD)"
TEST_FLAGS=-timeout=5m $(BUILD_FLAGS)
GO=go
all: build
CMDS = $(shell cd ./cmd && ls -1)
tools: | versioncheck
set -e; for DEP in $(shell grep "_ " buildtools/tools.go | awk '{ print $$2 }' | grep -v go-spew); do \
( cd buildtools && $(GO) install $$DEP@latest ) ; \
done
set -e; for DEP in $(shell grep "_ " buildtools/tools.go | awk '{ print $$2 }' | grep go-spew); do \
( cd buildtools && $(GO) install $$DEP ) ; \
done
( cd buildtools && $(GO) mod tidy )
updatedeps: versioncheck
$(MAKE) clean
$(MAKE) tools
$(GO) mod download
set -e; for dir in $(shell ls -d1 pkg/*); do \
( cd ./$$dir && $(GO) mod download ); \
( cd ./$$dir && GOPROXY=direct $(GO) get -u ); \
( cd ./$$dir && GOPROXY=direct $(GO) get -t -u ); \
done
$(GO) mod download
$(MAKE) cleandeps
cleandeps:
set -e; for dir in $(shell ls -d1 pkg/* cmd/*); do \
( cd ./$$dir && $(GO) mod tidy ); \
done
$(GO) mod tidy
( cd buildtools && $(GO) mod tidy )
vendor: go.work
$(GO) mod download
$(GO) mod tidy
GOWORK=off $(GO) mod vendor
go.work: pkg/*
echo "go $(MINGOVERSIONSTR)" > go.work
$(GO) work use . pkg/* cmd/* buildtools/.
dump:
if [ $(shell grep -r Dump ./cmd/*/*.go ./pkg/*/*.go | grep -v 'Data::Dumper' | grep -v 'httputil.Dump' | grep -v logThreadDump | grep -v dump.go | wc -l) -ne 0 ]; then \
sed -i.bak -e 's/\/\/go:build.*/\/\/ :build with debug functions/' -e 's/\/\/ +build.*/\/\/ build with debug functions/' pkg/$(PROJECT)/dump.go; \
else \
sed -i.bak -e 's/\/\/ :build.*/\/\/go:build ignore/' -e 's/\/\/ build.*/\/\/ +build ignore/' pkg/$(PROJECT)/dump.go; \
fi
rm -f pkg/$(PROJECT)/dump.go.bak
build: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && $(GO) build $(BUILD_FLAGS) -o ../../$$CMD ) ; \
done
# run build watch, ex. with tracing: make build-watch -- -vv
build-watch: vendor tools
set -x ; ls pkg/*/*.go cmd/*/*.go lmd.ini | entr -sr "$(MAKE) build && ./lmd $(filter-out $@,$(MAKECMDGOALS)) $(shell echo $(filter-out --,$(MAKEFLAGS)) | tac -s " ")"
build-linux-amd64: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GO) build $(BUILD_FLAGS) -o ../../$$CMD.linux.amd64 ) ; \
done
test: dump vendor
$(GO) test -short -v $(TEST_FLAGS) pkg/*
if grep -Irn TODO: ./cmd/ ./pkg/; then exit 1; fi
if grep -Irn Dump ./cmd/ ./pkg/ | grep -v 'Data::Dumper' | grep -v 'httputil.Dump' | grep -v logThreadDump | grep -v dump.go; then exit 1; fi
# test with filter
testf: vendor
$(GO) test -short -v $(TEST_FLAGS) pkg/* -run "$(filter-out $@,$(MAKECMDGOALS))" 2>&1 | grep -v "no test files" | grep -v "no tests to run" | grep -v "^PASS"
longtest: vendor
$(GO) test -v $(TEST_FLAGS) pkg/*
rm -f pkg/lmd/mock*.sock
citest: tools vendor
rm -f pkg/lmd/mock*.sock
#
# Checking gofmt errors
#
if [ $$(gofmt -s -l ./cmd/ ./pkg/ | wc -l) -gt 0 ]; then \
echo "found format errors in these files:"; \
gofmt -s -l ./cmd/ ./pkg/ ; \
exit 1; \
fi
#
# Checking TODO items
#
if grep -Irn TODO: ./cmd/ ./pkg/ ; then exit 1; fi
#
# Checking remaining debug calls
#
if grep -Irn Dump ./cmd/ ./pkg/ | grep -v 'Data::Dumper' | grep -v 'httputil.Dump' | grep -v logThreadDump | grep -v dump.go; then exit 1; fi
#
# Run other subtests
#
$(MAKE) golangci
-$(MAKE) govulncheck
$(MAKE) fmt
#
# Normal test cases
#
$(MAKE) test
#
# Benchmark tests
#
$(MAKE) benchmark
#
# Race rondition tests
#
$(MAKE) racetest
#
# All CI tests successful
#
benchmark:
$(GO) test $(TEST_FLAGS) -v -bench=B\* -run=^$$ -benchmem ./pkg/*
racetest:
$(GO) test -race -short $(TEST_FLAGS) -coverprofile=coverage.txt -covermode=atomic -gcflags "-d=checkptr=0" ./pkg/*
covertest:
$(GO) test -v $(TEST_FLAGS) -coverprofile=cover.out ./pkg/*
$(GO) tool cover -func=cover.out
$(GO) tool cover -html=cover.out -o coverage.html
coverweb:
$(GO) test -v $(TEST_FLAGS) -coverprofile=cover.out ./pkg/*
$(GO) tool cover -html=cover.out
clean:
set -e; for CMD in $(CMDS); do \
rm -f ./cmd/$$CMD/$$CMD; \
done
rm -f $(CMDS)
rm -f pkg/lmd/mock*.sock
rm -rf go.work
rm -rf go.work.sum
rm -f cover.out
rm -f coverage.html
rm -f coverage.txt
rm -f lmd-*.html
rm -rf vendor/
rm -rf $(TOOLSFOLDER)
GOVET=$(GO) vet -all
SRCFOLDER=./cmd/. ./pkg/. ./buildtools/.
fmt: generate tools
set -e; for CMD in $(CMDS); do \
$(GOVET) ./cmd/$$CMD; \
done
set -e; for dir in $(shell ls -d1 pkg/*); do \
$(GOVET) ./$$dir; \
done
gofmt -w -s $(SRCFOLDER)
./tools/gofumpt -w $(SRCFOLDER)
./tools/gci write --skip-generated $(SRCFOLDER)
./tools/goimports -w $(SRCFOLDER)
generate: tools
set -e; for dir in $(shell ls -d1 pkg/*); do \
cd $$dir && go generate; \
done
versioncheck:
@[ $$( printf '%s\n' $(GOVERSION) $(MINGOVERSION) | sort | head -n 1 ) = $(MINGOVERSION) ] || { \
echo "**** ERROR:"; \
echo "**** $(PROJECT) requires at least golang version $(MINGOVERSIONSTR) or higher"; \
echo "**** this is: $$(go version)"; \
exit 1; \
}
golangci: tools
#
# golangci combines a few static code analyzer
# See https://github.com/golangci/golangci-lint
#
@set -e; for dir in $$(ls -1d pkg/* cmd); do \
echo $$dir; \
echo " - GOOS=linux"; \
( cd $$dir && GOOS=linux golangci-lint run --timeout=5m ./... ); \
done
govulncheck: tools
govulncheck ./...
version:
OLDVERSION="$(shell grep "VERSION =" pkg/$(PROJECT)/main.go | awk '{print $$3}' | tr -d '"')"; \
NEWVERSION=$$(dialog --stdout --inputbox "New Version:" 0 0 "v$$OLDVERSION") && \
NEWVERSION=$$(echo $$NEWVERSION | sed "s/^v//g"); \
if [ "v$$OLDVERSION" = "v$$NEWVERSION" -o "x$$NEWVERSION" = "x" ]; then echo "no changes"; exit 1; fi; \
sed -i -e 's/VERSION =.*/VERSION = "'$$NEWVERSION'"/g' pkg/$(PROJECT)/main.go
zip: clean
CGO_ENABLED=0 $(MAKE) build
VERSION="$(shell grep "VERSION =" pkg/$(PROJECT)/main.go | awk '{print $$3}' | tr -d '"')"; \
COMMITS="$(shell git rev-list $$(git describe --tags --abbrev=0)..HEAD --count)"; \
DATE="$(shell LC_TIME=C date +%Y-%m-%d)"; \
FILE="$$(printf "%s+git~%03d~%s_%s" $${VERSION} $${COMMITS} $(BUILD) $${DATE})"; \
rm -f lmd-$$FILE.gz; \
cp lmd lmd-$$FILE; \
gzip -9 lmd-$$FILE; \
ls -la lmd-$$FILE.gz; \
echo "lmd-$$FILE.gz created";
lb: vendor buildtools/lb.go
$(GO) build $(BUILD_FLAGS) -o ./lb ./buildtools/lb.go
# just skip unknown make targets
.DEFAULT:
@if [[ "$(MAKECMDGOALS)" =~ ^testf ]]; then \
: ; \
else \
echo "unknown make target(s): $(MAKECMDGOALS)"; \
exit 1; \
fi