-
Notifications
You must be signed in to change notification settings - Fork 10
/
Makefile
237 lines (205 loc) · 6.99 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
#!/usr/bin/make -f
PROJECT=modgearman
MAKE:=make
SHELL:=bash
GOVERSION:=$(shell \
go version | \
awk -F'go| ' '{ split($$5, a, /\./); printf ("%04d%04d", a[1], a[2]); exit; }' \
)
# also update .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
GOPROXY=direct $(GO) get -t -u ./pkg/* ./cmd/*
$(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
GOWORK=off $(GO) mod vendor
go.work:
echo "go $(MINGOVERSIONSTR).0" > go.work
$(GO) work use \
. \
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 -- -debug=3 --logfile=stderr
build-watch: vendor tools
set -x ; ls pkg/*/*.go cmd/*/*.go | entr -sr "$(MAKE) build && ./mod_gearman_worker $(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
build-windows-i386: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=windows GOARCH=386 CGO_ENABLED=0 $(GO) build $(BUILD_FLAGS) -o ../../$$CMD.windows.i386.exe ) ; \
done
build-windows-amd64: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GO) build $(BUILD_FLAGS) -o ../../$$CMD.windows.amd64.exe ) ; \
done
send_gearman: pkg/*/*.go cmd/send_gearman/*.go
( cd ./cmd/send_gearman && $(GO) build $(BUILD_FLAGS) -o ../../send_gearman )
send_gearman.exe: pkg/*/*.go cmd/send_gearman/*.go
( cd ./cmd/send_gearman && GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GO) build $(BUILD_FLAGS) -o ../../send_gearman.exe )
test: dump vendor
$(GO) test -short -v $(TEST_FLAGS) ./pkg/* ./cmd/*
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/* ./cmd/* -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/* ./cmd/*
citest: tools vendor
#
# 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
#
# Test cross compilation
#
$(MAKE) build-linux-amd64
$(MAKE) build-windows-amd64
$(MAKE) build-windows-i386
#
# All CI tests successful
#
benchmark:
$(GO) test $(TEST_FLAGS) -v -bench=B\* -run=^$$ -benchmem ./pkg/*
racetest:
$(GO) test -race $(TEST_FLAGS) -coverprofile=coverage.txt -covermode=atomic ./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 *.windows.*.exe
rm -f *.linux.*
rm -rf go.work
rm -rf go.work.sum
rm -f cover.out
rm -f coverage.html
rm -f coverage.txt
rm -f mod-gearman*.html
rm -rf vendor/
rm -rf $(TOOLSFOLDER)
GOVET=$(GO) vet -all
SRCFOLDER=./cmd/. ./pkg/. ./buildtools/.
fmt: 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)
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
#
@echo " - GOOS=linux"; \
GOOS=linux CGO_ENABLED=0 golangci-lint run --timeout=5m pkg/... cmd/...
@echo " - GOOS=windows"; \
GOOS=windows CGO_ENABLED=0 golangci-lint run --timeout=5m pkg/... cmd/...
govulncheck: tools
govulncheck ./...
version:
OLDVERSION="$(shell grep "VERSION =" ./pkg/$(PROJECT)/mod_gearman_worker.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)/*.go cmd/*/*.go
# just skip unknown make targets
.DEFAULT:
@if [[ "$(MAKECMDGOALS)" =~ ^testf ]]; then \
: ; \
else \
echo "unknown make target(s): $(MAKECMDGOALS)"; \
exit 1; \
fi