-
Notifications
You must be signed in to change notification settings - Fork 47
/
Makefile
227 lines (185 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
# environment
BUILDDIR := ${CURDIR}/build
TARBUILDDIR := ${BUILDDIR}/tar
ARCH := $(shell go env GOHOSTARCH)
OS := $(shell go env GOHOSTOS)
GOVER := $(shell go version | awk '{print $$3}' | tr -d '.')
SIGN_KEY ?= ${HOME}/.minisign/go-camo.key
# app specific info
APP_NAME := go-camo
APP_VER := $(shell git describe --always --tags|sed 's/^v//')
GOPATH := $(shell go env GOPATH)
GOBIN := ${CURDIR}/.tools
VERSION_VAR := main.ServerVersion
# flags and build configuration
GOBUILD_OPTIONS := -trimpath
GOTEST_FLAGS := -cpu=1,2
GOTEST_BENCHFLAGS :=
GOBUILD_DEPFLAGS := -tags netgo
GOBUILD_LDFLAGS ?= -s -w
GOBUILD_FLAGS := ${GOBUILD_DEPFLAGS} ${GOBUILD_OPTIONS} -ldflags "${GOBUILD_LDFLAGS} -X ${VERSION_VAR}=${APP_VER}"
# cross compile defs
CC_BUILD_TARGETS = go-camo url-tool
CC_BUILD_ARCHES = darwin/arm64 freebsd/amd64 linux/amd64 linux/arm64 windows/amd64
CC_OUTPUT_TPL := ${BUILDDIR}/bin/{{.Dir}}.{{.OS}}-{{.Arch}}
# some exported vars (pre-configure go build behavior)
export GO111MODULE=on
export CGO_ENABLED=0
# unsure toolchains are not automatically downloaded/upgraded
export GOTOOLCHAIN=local
## enable go 1.21 loopvar "experiment"
export GOEXPERIMENT=loopvar
export GOBIN
export PATH := ${GOBIN}:${PATH}
define HELP_OUTPUT
Available targets:
* help this help (default target)
clean clean up
check run checks and validators
test run tests
cover run tests with cover output
bench run benchmarks
build build all binaries
man build all man pages
all build binaries and man pages
tar build release tarball for host platform only
cross-tar cross compile and build release tarballs for all platforms
release-sign sign release tarballs with minisign
release build and sign release
update-go-deps updates go.mod and go.sum files
endef
export HELP_OUTPUT
.PHONY: help clean build test cover bench man man-copy all tar cross-tar setup-check
help:
@echo "$$HELP_OUTPUT"
clean:
@rm -rf "${BUILDDIR}"
## begin tools
# bench tools
${GOBIN}/benchstat:
go install golang.org/x/perf/cmd/benchstat@latest
BENCH_TOOLS := ${GOBIN}/benchstat
# check tools
${GOBIN}/betteralign:
go install github.com/dkorunic/betteralign/cmd/betteralign@latest
${GOBIN}/ineffassign:
go install github.com/gordonklaus/ineffassign@latest
${GOBIN}/errcheck:
go install github.com/kisielk/errcheck@latest
${GOBIN}/go-errorlint:
go install github.com/polyfloyd/go-errorlint@latest
${GOBIN}/gosec:
go install github.com/securego/gosec/v2/cmd/gosec@latest
${GOBIN}/nilaway:
go install go.uber.org/nilaway/cmd/nilaway@latest
${GOBIN}/deadcode:
go install golang.org/x/tools/cmd/deadcode@latest
${GOBIN}/govulncheck:
go install golang.org/x/vuln/cmd/govulncheck@latest
${GOBIN}/staticcheck:
go install honnef.co/go/tools/cmd/staticcheck@latest
${GOBIN}/nilness:
go install golang.org/x/tools/go/analysis/passes/nilness/cmd/nilness@latest
CHECK_TOOLS := ${GOBIN}/staticcheck ${GOBIN}/gosec ${GOBIN}/govulncheck
CHECK_TOOLS += ${GOBIN}/errcheck ${GOBIN}/ineffassign ${GOBIN}/nilaway
CHECK_TOOLS += ${GOBIN}/go-errorlint ${GOBIN}/deadcode ${GOBIN}/betteralign
CHECK_TOOLS += ${GOBIN}/nilness
## end tools
.PHONY: setup-check
setup-check: ${CHECK_TOOLS}
.PHONY: setup-bench
setup-bench: ${BENCH_TOOLS}
.PHONY: setup
setup:
build: setup
@[ -d "${BUILDDIR}/bin" ] || mkdir -p "${BUILDDIR}/bin"
@echo "Building..."
@echo "...go-camo..."
@go build ${GOBUILD_FLAGS} -o "${BUILDDIR}/bin/go-camo" ./cmd/go-camo
@echo "...url-tool..."
@go build ${GOBUILD_FLAGS} -o "${BUILDDIR}/bin/url-tool" ./cmd/url-tool
@echo "done!"
test: setup
@echo "Running tests..."
@go test -count=1 -cpu=4 -vet=off ${GOTEST_FLAGS} ./...
bench: setup setup-bench
@echo "Running benchmarks..."
@go test -bench="." -run="^$$" -test.benchmem=true ${GOTEST_BENCHFLAGS} ./...
cover: setup
@echo "Running tests with coverage..."
@go test -vet=off -cover ${GOTEST_FLAGS} ./...
check: setup setup-check
@echo "Running checks and validators..."
@echo "... staticcheck ..."
@${GOBIN}/staticcheck ./...
@echo "... errcheck ..."
@${GOBIN}/errcheck -ignoretests -exclude .errcheck-excludes.txt ./...
@echo "... go-vet ..."
@go vet ./...
@echo "... nilness ..."
@${GOBIN}/nilness ./...
@echo "... ineffassign ..."
@${GOBIN}/ineffassign ./...
@echo "... govulncheck ..."
@${GOBIN}/govulncheck ./...
@echo "... betteralign ..."
@${GOBIN}/betteralign ./...
@echo "... gosec ..."
@${GOBIN}/gosec -quiet -exclude-generated -exclude-dir=cmd/refidgen -exclude-dir=tools ./...
.PHONY: update-go-deps
update-go-deps:
@echo ">> updating Go dependencies"
@go get -u all
@go mod tidy
${BUILDDIR}/man/%: man/%.adoc
@[ -d "${BUILDDIR}/man" ] || mkdir -p "${BUILDDIR}/man"
@asciidoctor -b manpage -a release-version="${APP_VER}" -o $@ $<
man: $(patsubst man/%.adoc,${BUILDDIR}/man/%,$(wildcard man/*.adoc))
tar: all
@echo "Building tar..."
@mkdir -p ${TARBUILDDIR}/${APP_NAME}-${APP_VER}/bin
@mkdir -p ${TARBUILDDIR}/${APP_NAME}-${APP_VER}/man
@cp ${BUILDDIR}/bin/${APP_NAME} ${TARBUILDDIR}/${APP_NAME}-${APP_VER}/bin/${APP_NAME}
@cp ${BUILDDIR}/bin/url-tool ${TARBUILDDIR}/${APP_NAME}-${APP_VER}/bin/url-tool
@cp ${BUILDDIR}/man/*.[1-9] ${TARBUILDDIR}/${APP_NAME}-${APP_VER}/man/
@tar -C ${TARBUILDDIR} -czf ${TARBUILDDIR}/${APP_NAME}-${APP_VER}.${GOVER}.${OS}-${ARCH}.tar.gz ${APP_NAME}-${APP_VER}
@rm -rf "${TARBUILDDIR}/${APP_NAME}-${APP_VER}"
cross-tar: man setup
@echo "Building (cross-compile: ${CC_BUILD_ARCHES})..."
@(for x in ${CC_BUILD_TARGETS}; do \
for y in $(subst /,-,${CC_BUILD_ARCHES}); do \
printf -- "--> %15s: %s\n" "$${y}" "$${x}"; \
GOOS="$${y%%-*}"; \
GOARCH="$${y##*-}"; \
EXT=""; \
if echo "$${y}" | grep -q 'windows-'; then EXT=".exe"; fi; \
env GOOS=$${GOOS} GOARCH=$${GOARCH} go build ${GOBUILD_FLAGS} -o "${BUILDDIR}/bin/$${x}.$${GOOS}-$${GOARCH}$${EXT}" ./cmd/$${x}; \
done; \
done)
@echo "Creating tar archives..."
@(for x in $(subst /,-,${CC_BUILD_ARCHES}); do \
printf -- "--> %15s\n" "$${x}"; \
EXT=""; \
if echo "$${x}" | grep -q 'windows-'; then EXT=".exe"; fi; \
XDIR="${GOVER}.$${x}"; \
ODIR="${TARBUILDDIR}/$${XDIR}/${APP_NAME}-${APP_VER}"; \
mkdir -p "$${ODIR}/bin"; \
mkdir -p "$${ODIR}/man"; \
for t in ${CC_BUILD_TARGETS}; do \
cp ${BUILDDIR}/bin/$${t}.$${x}$${EXT} $${ODIR}/bin/$${t}$${EXT}; \
done; \
cp ${BUILDDIR}/man/*.[1-9] $${ODIR}/man/; \
tar -C ${TARBUILDDIR}/$${XDIR} -czf ${TARBUILDDIR}/${APP_NAME}-${APP_VER}.$${XDIR}.tar.gz ${APP_NAME}-${APP_VER}; \
rm -rf "${TARBUILDDIR}/$${XDIR}/"; \
done)
@echo "done!"
release-sign:
@echo "signing release tarballs"
@(cd build/tar; shasum -a 256 go-camo-*.tar.gz > SHA256; \
minisign -S -s ${SIGN_KEY} \
-c "go-camo-${APP_VER} SHA256" \
-t "go-camo-${APP_VER} SHA256" \
-x SHA256.sig -m SHA256; \
)
release: cross-tar release-sign
all: build man