-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
169 lines (143 loc) · 5.1 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
.PHONY: test install git.commit git.branch default
all: install
app=$(notdir $(shell pwd))
appVersion := 1.0.0
goVersion := $(shell go version | sed 's/go version //'|sed 's/ /_/')
# e.g. 2021-10-28T11:49:52+0800
buildTime := $(shell date +%FT%T%z)
# https://git-scm.com/docs/git-rev-list#Documentation/git-rev-list.txt-emaIem
# e.g. ffd23d3@2022-04-06T18:07:14+08:00
gitCommit := $(shell [ -f git.commit ] && cat git.commit || git log --format=format:'%h@%aI' -1)
gitBranch := $(shell [ -f git.branch ] && cat git.branch || git rev-parse --abbrev-ref HEAD)
gitInfo = $(gitBranch)-$(gitCommit)
#gitCommit := $(shell git rev-list -1 HEAD)
# https://stackoverflow.com/a/47510909
pkg := github.com/bingoohuang/gg/pkg/v
extldflags := -extldflags -static
# https://ms2008.github.io/2018/10/08/golang-build-version/
# https://github.com/kubermatic/kubeone/blob/master/Makefile
flags1 = -s -w -X $(pkg).BuildTime=$(buildTime) -X $(pkg).AppVersion=$(appVersion) -X $(pkg).GitCommit=$(gitInfo) -X $(pkg).GoVersion=$(goVersion)
flags2 = ${extldflags} ${flags1}
buildTags = $(if $(TAGS),-tags=$(TAGS),)
buildFlags = ${buildTags} -trimpath -ldflags="'${flags1}'"
goinstall_target = $(if $(TARGET),$(TARGET),./...)
goinstall = go install ${buildTags} -trimpath -ldflags='${flags1}' ${goinstall_target}
gobin := $(shell go env GOBIN)
# try $GOPATN/bin if $gobin is empty
gobin := $(if $(gobin),$(gobin),$(shell go env GOPATH)/bin)
export GOPROXY=https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://goproxy.io,direct
# Active module mode, as we use go modules to manage dependencies
export GO111MODULE=on
# usage: t=$(mktemp); echo $t; echo "set -x; go build -o build/rig_linux_arm64 $(make -f ~/github/gg/Makefile build.flags) ./cmd/rig" > $t && sh $t
build.flags:
@echo ${buildFlags}
git.commit:
echo ${gitCommit} > git.commit
echo ${gitBranch} > git.branch
sec:
@gosec ./...
@echo "[OK] Go security check was completed!"
init:
lint-all:
golangci-lint run --enable-all
lint:
golangci-lint run ./...
fmt-update:
go install mvdan.cc/gofumpt@latest
go install golang.org/x/tools/cmd/...@latest # for goimports
# go install github.com/mgechev/revive@master
go install github.com/daixiang0/gci@latest
go install github.com/google/osv-scanner/cmd/osv-scanner@latest
go install github.com/polyfloyd/go-errorlint@latest
go install github.com/dkorunic/betteralign/cmd/betteralign@latest
go install -v github.com/go-critic/go-critic/cmd/gocritic@latest
# Use right mirror functions for string/[]byte performance bust
go install github.com/butuzov/mirror/cmd/mirror@latest
go install golang.org/x/vuln/cmd/govulncheck@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install go.uber.org/nilaway/cmd/nilaway@latest
fmt:
gofumpt -l -w .
gofmt -s -w .
go mod tidy
go fmt ./...
revive .
goimports -w .
gci write .
osv-scanner -r .
go-errorlint ./...
gocritic check ./...
betteralign ./...
nilaway ./...
# Use right mirror functions for string/[]byte performance bust
# too slow
# mirror ./...
govulncheck ./...
align:
betteralign -apply ./...
install-upx: init
${goinstall}
upx --best --lzma ${gobin}/${app}*
ls -lh ${gobin}/${app}*
install: init
${goinstall}
ls -lh ${gobin}/${app}*
linux: init
GOOS=linux GOARCH=amd64 ${goinstall}
ls -lh ${gobin}/linux_amd64/${app}*
linux-upx: init
GOOS=linux GOARCH=amd64 ${goinstall}
upx --best --lzma ${gobin}/linux_amd64/${app}*
ls -lh ${gobin}/linux_amd64/${app}*
windows: init
GOOS=windows GOARCH=amd64 ${goinstall}
ls -lh ${gobin}/windows_amd64/${app}*
windows-upx: init
GOOS=windows GOARCH=amd64 ${goinstall}
upx --best --lzma ${gobin}/windows_amd64/${app}*
ls -lh ${gobin}/windows_amd64/${app}*
arm: init
GOOS=linux GOARCH=arm64 ${goinstall}
ls -lh ${gobin}/linux_arm64/${app}*
arm-upx: init
GOOS=linux GOARCH=arm64 ${goinstall}
upx --best --lzma ${gobin}/linux_arm64/${app}*
ls -lh ${gobin}/linux_arm64/${app}*
mac-arm: init
GOOS=darwin GOARCH=arm64 ${goinstall}
# upx --best --lzma ${gobin}/darwin_arm64/${app}*
ls -lh ${gobin}/darwin_arm64/${app}*
upx:
ls -lh ${gobin}/${app}*
upx ${gobin}/${app}*
ls -lh ${gobin}/${app}*
ls -lh ${gobin}/linux_amd64/${app}*
upx ${gobin}/linux_amd64/${app}*
ls -lh ${gobin}/linux_amd64/${app}*
test: init
#go test -v ./...
go test -v -race ./...
bench: init
#go test -bench . ./...
go test -tags bench -benchmem -bench . ./...
clean:
rm coverage.out
cover:
go test -v -race -coverpkg=./... -coverprofile=coverage.out ./...
coverview:
go tool cover -html=coverage.out
# https://hub.docker.com/_/golang
# docker run --rm -v "$PWD":/usr/src/myapp -v "$HOME/dockergo":/go -w /usr/src/myapp golang make docker
# docker run --rm -it -v "$PWD":/usr/src/myapp -w /usr/src/myapp golang bash
# 静态连接 glibc
docker:
mkdir -p ~/dockergo
docker run --rm -v "$$PWD":/usr/src/myapp -v "$$HOME/dockergo":/go -w /usr/src/myapp golang make dockerinstall
#upx ~/dockergo/bin/${app}
gzip -f ~/dockergo/bin/${app}
dockerinstall:
go install -v -x -a -ldflags=${flags} ./...
targz:
find . -name ".DS_Store" -delete
find . -type f -name '\.*' -print
cd .. && rm -f ${app}.tar.gz && tar czvf ${app}.tar.gz --exclude .git --exclude .idea ${app}