forked from m-kraus/check_nsc_web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
229 lines (197 loc) · 6.15 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
#!/usr/bin/make -f
PROJECT=checknscweb
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)
REVISION:=$(shell printf "%04d" $$( git rev-list --all --count))
# 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) -X main.Revision=$(REVISION)"
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 }'); do \
( cd buildtools && $(GO) install $$DEP@latest ) ; \
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/. \
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 | entr -sr "$(MAKE) build && ./check_nsc_web $(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
build-freebsd-i386: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=freebsd GOARCH=386 CGO_ENABLED=0 $(GO) build $(BUILD_FLAGS) -o ../../$$CMD.freebsd.i386 ) ; \
done
build-darwin-aarch64: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 $(GO) build $(BUILD_FLAGS) -o ../../$$CMD.darwin.aarch64 ) ; \
done
test: vendor
$(GO) test -short -v $(TEST_FLAGS) ./pkg/* ./cmd/*
if grep -Irn TODO: ./cmd/ ./pkg/; 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
#
# 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
$(MAKE) build-freebsd-i386
$(MAKE) build-darwin-aarch64
#
# 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 *.windows.*.exe
rm -f *.linux.*
rm -f *.darwin.*
rm -f *.freebsd.*
rm -f go.work
rm -f go.work.sum
rm -f cover.out
rm -f coverage.html
rm -f coverage.txt
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/...
govulncheck: tools
govulncheck ./...
version:
OLDVERSION="$(shell grep "VERSION =" ./pkg/$(PROJECT)/check.go | awk '{print $$4}' | 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)/check.go
check_nsc_web: build
# just skip unknown make targets
.DEFAULT:
@if [[ "$(MAKECMDGOALS)" =~ ^testf ]]; then \
: ; \
else \
echo "unknown make target(s): $(MAKECMDGOALS)"; \
exit 1; \
fi