-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
171 lines (140 loc) · 6.05 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
.DEFAULT_GOAL := all
GO_VERSION_MANIFEST := https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json
REQUIRED_COVERAGE_PERCENT := 0
COVERAGE_FILE := cover.out
CGO_ENABLED ?= 1
GOOS ?=
GOARCH ?=
SERVICE_NAME ?=
export CGO_ENABLED GOOS GOARCH SERVICE_NAME
define getLatestGoPatchVersion
$(shell curl -s $(GO_VERSION_MANIFEST) | jq -r '.[0].version')
endef
define getLatestGoMinorVersion
$(shell echo $(call getLatestGoPatchVersion) | cut -f1,2 -d'.')
endef
latestGoVersion:
@echo $(call getLatestGoPatchVersion)
latestGoMinorVersion:
@echo $(call getLatestGoMinorVersion)
updateGoModVersion:
#TODO Temporary hack to figure; remove it asap
go mod edit -go $(call getLatestGoPatchVersion)
checkModVersion: updateGoModVersion
@if git status --porcelain | grep -q go.mod; then \
echo "Outdated go version in go.mod. Please update it using 'make updateGoModVersion' and make sure everything works correctly and tests pass then commit the changes."; \
exit 1; \
fi; \
true;
updateAllDependencies:
go get -t -u ./...
go mod tidy
checkIfAllDependenciesAreUpToDate: updateAllDependencies
@if git status --porcelain | grep -q go.sum; then \
echo "Some dependencies are outdated. Please update all dependencies using 'make updateAllDependencies' and make sure everything works correctly and tests pass then commit the changes."; \
exit 1; \
fi; \
true;
generate:
$(MAKE) addLicense
$(MAKE) format-imports
checkGenerated: generate
@if git status --porcelain | grep -e [.]go -e [.]json -e [.]yaml; then \
echo "Please commit generated files, using 'make generate'."; \
git --no-pager diff; \
exit 1; \
fi; \
true;
build-all@ci/cd:
go build -tags "go_json linux sqlite_stat4 sqlite_icu" -a -v -race ./...
build: build-all@ci/cd
test:
set -xe; \
mf="$$(pwd)/Makefile"; \
find . -mindepth 1 -maxdepth 4 -type d -print | grep -v '\./\.' | grep -v '/\.' | sed 's/\.\///g' | while read service; do \
cd $${service} ; \
if [[ $$(find . -mindepth 1 -maxdepth 1 -type f -print | grep -E '_test.go' | wc -l | sed "s/ //g") -gt 0 ]]; then \
make -f $$mf test@ci/cd; \
fi ; \
for ((i=0;i<$$(echo "$${service}" | grep -o "/" | wc -l | sed "s/ //g");i++)); do \
cd .. ; \
done; \
cd .. ; \
done;
# TODO should be improved to a per file check and maybe against a previous value
#(maybe we should use something like SonarQube for this?)
coverage: $(COVERAGE_FILE)
@t=`go tool cover -func=$(COVERAGE_FILE) | grep total | grep -Eo '[0-9]+\.[0-9]+'`;\
echo "Total coverage: $${t}%"; \
if [ "$${t%.*}" -lt $(REQUIRED_COVERAGE_PERCENT) ]; then \
echo "ERROR: It has to be at least $(REQUIRED_COVERAGE_PERCENT)%"; \
exit 1; \
fi;
test@ci/cd:
# TODO make -race work
go test -timeout 20m -tags "go_json test linux sqlite_stat4 sqlite_icu" -v -cover -coverprofile=$(COVERAGE_FILE) -covermode atomic
benchmark@ci/cd:
# TODO make -race work
go test -timeout 20m -tags "go_json test linux sqlite_stat4 sqlite_icu" -run=^$ -v -bench=. -benchmem -benchtime 10s
benchmark:
set -xe; \
mf="$$(pwd)/Makefile"; \
find . -mindepth 1 -maxdepth 4 -type d -print | grep -v '\./\.' | grep -v '/\.' | sed 's/\.\///g' | while read service; do \
cd $${service} ; \
if [[ $$(find . -mindepth 1 -maxdepth 1 -type f -print | grep -E '_test.go' | wc -l | sed "s/ //g") -gt 0 ]]; then \
make -f $$mf benchmark@ci/cd; \
fi ; \
for ((i=0;i<$$(echo "$${service}" | grep -o "/" | wc -l | sed "s/ //g");i++)); do \
cd .. ; \
done; \
cd .. ; \
done;
print-all-packages-with-tests:
set -xe; \
find . -mindepth 1 -maxdepth 4 -type d -print | grep -v '\./\.' | grep -v '/\.' | sed 's/\.\///g' | while read service; do \
cd $${service} ; \
if [[ $$(find . -mindepth 1 -maxdepth 1 -type f -print | grep -E '_test.go' | wc -l | sed "s/ //g") -gt 0 ]]; then \
echo "Found tests in package >> $${service}"; \
fi ; \
for ((i=0;i<$$(echo "$${service}" | grep -o "/" | wc -l | sed "s/ //g");i++)); do \
cd .. ; \
done; \
cd .. ; \
done;
clean:
@go clean
@rm -f tmp$(COVERAGE_FILE) $(COVERAGE_FILE) 2>/dev/null || true
@test -d cmd && find ./cmd -mindepth 2 -maxdepth 2 -type f -name bin -exec rm -f {} \; || true;
@test -d cmd && find ./cmd -mindepth 2 -maxdepth 2 -type d -name bins -exec rm -Rf {} \; || true;
@find . -name ".tmp-*" -exec rm -Rf {} \; || true;
@find . -maxdepth 1 -name "*.bin" -exec rm -Rf {} \; || true;
@find . -mindepth 1 -maxdepth 3 -type f -name $(COVERAGE_FILE) -exec rm -Rf {} \; || true;
@find . -mindepth 1 -maxdepth 3 -type f -name tmp$(COVERAGE_FILE) -exec rm -Rf {} \; || true;
lint:
golangci-lint run
getAddLicense:
go install -v github.com/google/addlicense@latest
addLicense: getAddLicense
`go env GOPATH`/bin/addlicense -f LICENSE.header -ignore '**/.testdata/*.html' * .github/*
checkLicense: getAddLicense
`go env GOPATH`/bin/addlicense -f LICENSE.header -check -ignore '**/.testdata/*.html' * .github/*
fix-field-alignment:
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
fieldalignment -fix ./...
format-imports:
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/daixiang0/gci@latest
gci write -s standard -s default -s "prefix(github.com/ice-blockchain)" ./..
goimports -w -local github.com/ice-blockchain ./..
buildAllBinaries:
set -xe; \
find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | while read service; do \
env SERVICE_NAME=$${service##*/} env GOOS=$(GOOS) env GOARCH=$(GOARCH) $(MAKE) binary-specific-service; \
done;
binary-specific-service:
set -xe; \
echo "$@: $(SERVICE_NAME) / $(GOOS) / $(GOARCH)" ; \
go build -tags "go_json linux sqlite_stat4 sqlite_icu" -a -v -o ./cmd/$${SERVICE_NAME}/bin ./cmd/$${SERVICE_NAME}; \
cp ./cmd/$${SERVICE_NAME}/bin ./$${SERVICE_NAME}.$${GOOS}.$${GOARCH}.bin; \
all: checkLicense checkModVersion checkIfAllDependenciesAreUpToDate checkGenerated build test coverage benchmark clean
local: addLicense updateGoModVersion updateAllDependencies generate build test coverage benchmark lint clean