forked from cactus/go-camo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
170 lines (143 loc) · 5.55 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
# 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)
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
## enable go 1.21 loopvar "experiment"
export GOEXPERIMENT=loopvar
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}"
setup:
setup-check: ${GOPATH}/bin/staticcheck ${GOPATH}/bin/gosec ${GOPATH}/bin/govulncheck
${GOPATH}/bin/staticcheck:
go install honnef.co/go/tools/cmd/staticcheck@latest
${GOPATH}/bin/gosec:
go install github.com/securego/gosec/v2/cmd/gosec@latest
${GOPATH}/bin/govulncheck:
go install golang.org/x/vuln/cmd/govulncheck@latest
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
@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 ..."
@${GOPATH}/bin/staticcheck ./...
@echo "... go-vet ..."
@go vet ./...
@echo "... gosec ..."
@${GOPATH}/bin/gosec -quiet ./...
@echo "... govulncheck ..."
@${GOPATH}/bin/govulncheck ./...
.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