-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
166 lines (136 loc) · 4.84 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
# This Makefile intended to be POSIX-compliant (2018 edition with .PHONY target).
#
# .PHONY targets are used by:
# - task definintions
# - compilation of Go code (force usage of `go build` to changes detection).
#
# More info:
# - docs: <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html>
# - .PHONY: <https://www.austingroupbugs.net/view.php?id=523>
#
.POSIX:
.SUFFIXES:
#
# PUBLIC MACROS
#
CLI = opinions
CLIDIR = ./cmd/opinions
DESTDIR = ./dist
GO = go
GOFLAGS =
LDFLAGS = -ldflags "-s -w -X main.appVersion=$(VERSION)"
BETTERALIGN = $$($(GO) env GOPATH)/bin/betteralign
ERRCHECK = $$($(GO) env GOPATH)/bin/errcheck
STATICCHECK = $$($(GO) env GOPATH)/bin/staticcheck
#
# INTERNAL MACROS
#
CLI_CURRENT_VER_TAG = $$(git tag --points-at HEAD | grep "^cli" | sed 's/^cli\/v//' | sort -t. -k 1,1n -k 2,2n -k 3,3n | tail -1)
CLI_LATEST_VERSION = $$(git tag | grep "^cli" | sed 's/^cli\/v//' | sort -t. -k 1,1n -k 2,2n -k 3,3n | tail -1)
CLI_PSEUDOVERSION = $$(VER="$(CLI_LATEST_VERSION)"; echo "$${VER:-0001.01}")-$$(TZ=UTC git --no-pager show --quiet --abbrev=12 --date='format-local:%Y%m%d%H%M%S' --format='%cd-%h')
CLI_VERSION = $$(VER="$(CLI_CURRENT_VER_TAG)"; echo "$${VER:-$(CLI_PSEUDOVERSION)}")
MODULE_LATEST_VERSION = $$(git tag | grep "^v" | sed 's/^v//' | sort -t. -k 1,1n -k 2,2n -k 3,3n | tail -1)
#
# DEVELOPMENT TASKS
#
.PHONY: all
all: install-dependencies
.PHONY: clean
clean:
@echo '# Delete build directory' >&2
rm -rf $(DESTDIR)
.PHONY: info
info:
@printf '# OS info: '
@uname -rsv;
@echo '# Development dependencies:'
@$(GO) version || true
@$(BETTERALIGN) -V=full || true
# @$(ERRCHECK) --version || true # not supported, see: https://github.com/kisielk/errcheck/issues/254
@$(STATICCHECK) --version || true
@echo '# Go environment variables:'
@$(GO) env || true
.PHONY: check
check:
@echo '# Static analysis' >&2
$(GO) vet ./...
$(STATICCHECK) ./...
$(ERRCHECK) ./...
$(BETTERALIGN) ./...
$(GO) mod verify
@echo '# Formatting' >&2
$(GO) fmt ./...
$(GO) mod tidy
.PHONY: test
test:
@echo '# Unit tests' >&2
$(GO) test ./...
.PHONY: e2e
e2e:
@echo '# E2E tests of $(DESTDIR)/$(CLI)' >&2
@printf 'Hacker News\nLemmy\nLobsters\nReddit\n' >test_case.grugbrain
@if [ -n "$${GITHUB_ACTIONS}" ]; then sed '/Reddit/d' test_case.grugbrain >filtered; mv filtered test_case.grugbrain; fi
@printf '' >test_case.unknown
$(DESTDIR)/$(CLI) --version
$(DESTDIR)/$(CLI) --timeout 10s 'https://grugbrain.dev' | cut -d' ' -f1 | sort -u | diff test_case.grugbrain -
$(DESTDIR)/$(CLI) --timeout 8500ms 'zażółćjaźńgęślą' | diff test_case.unknown -
.PHONY: build
build:
@echo '# Build CLI executable: $(DESTDIR)/$(CLI)' >&2
$(GO) build -C $(CLIDIR) $(GOFLAGS) $(LDFLAGS) -o '../../$(DESTDIR)/$(CLI)'
@echo '# Add executable checksum to: $(DESTDIR)/sha256sum.txt' >&2
cd $(DESTDIR); sha256sum $(CLI) >> sha256sum.txt
.PHONY: unsafe
unsafe:
@$(MAKE) GOFLAGS='-tags=unsafe' build
.PHONY: dist
dist: opinions-freebsd_amd64 \
opinions-linux_amd64-hardened opinions-linux_armv7 opinions-linux_arm64 \
opinions-openbsd_amd64-hardened \
opinions-windows_amd64.exe
.PHONY: install-dependencies
install-dependencies:
@echo '# Install development dependencies:' >&2
$(GO) install github.com/dkorunic/betteralign/cmd/betteralign@latest
$(GO) install github.com/kisielk/errcheck@latest
$(GO) install honnef.co/go/tools/cmd/staticcheck@latest
@echo '# Install CLI dependencies' >&2
@GOFLAGS='-v -x' $(GO) get -C $(CLIDIR) $(GOFLAGS) .
.PHONY: cli-release
cli-release: check test
@echo '# Update local branch' >&2
@git pull --rebase
@echo '# Create new CLI release tag' >&2
@VER="$(CLI_LATEST_VERSION)"; printf 'Choose new version number for CLI (calver; >%s): ' "$${VER:-2024.01}"
@read -r NEW_VERSION; \
git tag "cli/v$$NEW_VERSION"; \
git push --tags
.PHONY: module-release
module-release: check test
@echo '# Update local branch' >&2
@git pull --rebase
@echo '# Create new Go module release tag' >&2
@VER="$(MODULE_LATEST_VERSION)"; printf 'Choose new version number for module (semver; >%s): ' "$${VER:-2.0.0}"
@read -r NEW_VERSION; \
git tag "v$$NEW_VERSION"; \
git push --tags
#
# SUPPORTED EXECUTABLES
#
# this force using `go build` to changes detection in Go project (instead of `make`)
.PHONY: opinions-freebsd_amd64 \
opinions-linux_amd64-hardened opinions-linux_armv7 opinions-linux_arm64 \
opinions-openbsd_amd64-hardened \
opinions-windows_amd64.exe
opinions-freebsd_amd64:
GOOS=freebsd GOARCH=amd64 $(MAKE) CLI=$@ unsafe
opinions-linux_amd64-hardened:
GOOS=linux GOARCH=amd64 $(MAKE) CLI=$@ build
opinions-linux_armv7:
GOOS=linux GOARCH=arm GOARM=7 $(MAKE) CLI=$@ unsafe
opinions-linux_arm64:
GOOS=linux GOARCH=arm64 $(MAKE) CLI=$@ unsafe
opinions-openbsd_amd64-hardened:
GOOS=openbsd GOARCH=amd64 $(MAKE) CLI=$@ build
opinions-windows_amd64.exe:
GOOS=windows GOARCH=amd64 $(MAKE) CLI=$@ unsafe