forked from evylang/evy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
260 lines (197 loc) · 7.57 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Run `make help` to display help
.DEFAULT_GOAL := $(or $(EVY_DEFAULT_GOAL),help)
# --- Global -------------------------------------------------------------------
O = out
COVERAGE = 80
VERSION ?= $(shell git describe --tags --dirty --always)
GOFILES = $(shell find . -name '*.go')
## Build, test, check coverage and lint
all: build test lint tiny test-tiny check-coverage sh-lint check-prettier check-evy-fmt frontend
@if [ -e .git/rebase-merge ]; then git --no-pager log -1 --pretty='%h %s'; fi
@echo '$(COLOUR_GREEN)Success$(COLOUR_NORMAL)'
## Full clean build and up-to-date checks as run on CI
ci: clean check-uptodate all
check-uptodate: tidy fmt doc
test -z "$$(git status --porcelain)" || { git status; false; }
## Remove generated files
clean::
-rm -rf $(O)
.PHONY: all check-uptodate ci clean
# --- Build --------------------------------------------------------------------
GO_LDFLAGS = -X main.version=$(VERSION)
CMDS = .
## Build evy binaries
build: | $(O)
go build -o $(O) -ldflags='$(GO_LDFLAGS)' $(CMDS)
## Build and install binaries in $GOBIN
install:
go install -ldflags='$(GO_LDFLAGS)' $(CMDS)
# Use `go version` to ensure the right go version is installed when using tinygo.
go-version:
go version
## Build with tinygo targeting wasm
# optimise for size, see https://www.fermyon.com/blog/optimizing-tinygo-wasm
tiny: go-version | $(O)
GOOS=wasip1 GOARCH=wasm tinygo build -o $(O)/evy-unopt.wasm -no-debug -ldflags='$(GO_LDFLAGS)' -stack-size=512kb ./pkg/wasm
wasm-opt -O3 $(O)/evy-unopt.wasm -o frontend/evy.wasm
cp -f $$(tinygo env TINYGOROOT)/targets/wasm_exec.js frontend/
echo '{ "version": "$(VERSION)" }' | jq > frontend/version.json
## Tidy go modules with "go mod tidy"
tidy:
go mod tidy
## Format all go files with gofumpt, a stricter gofmt
fmt:
gofumpt -w $(GOFILES)
clean::
-rm -f frontend/evy.wasm
-rm -f frontend/wasm_exec.js
-rm -f frontend/version.json
.PHONY: build go-version install tidy tiny
# --- Test ---------------------------------------------------------------------
COVERFILE = $(O)/coverage.txt
## Run non-tinygo tests and generate a coverage file
test: | $(O)
go test -coverprofile=$(COVERFILE) ./...
## Run tinygo tests
test-tiny: go-version | $(O)
tinygo test ./...
## Check that test coverage meets the required level
check-coverage: test
@go tool cover -func=$(COVERFILE) | $(CHECK_COVERAGE) || $(FAIL_COVERAGE)
## Show test coverage in your browser
cover: test
go tool cover -html=$(COVERFILE)
CHECK_COVERAGE = awk -F '[ \t%]+' '/^total:/ {print; if ($$3 < $(COVERAGE)) exit 1}'
FAIL_COVERAGE = { echo '$(COLOUR_RED)FAIL - Coverage below $(COVERAGE)%$(COLOUR_NORMAL)'; exit 1; }
.PHONY: check-coverage cover test test-tiny
# --- Lint ---------------------------------------------------------------------
EVY_FILES = $(shell find frontend/samples -name '*.evy')
## Lint go source code
lint:
golangci-lint run
## Format evy sample code
evy-fmt:
go run . fmt --write $(EVY_FILES)
check-evy-fmt:
go run . fmt --check $(EVY_FILES)
.PHONY: check-evy-fmt evy-fmt lint
# --- Docs ---------------------------------------------------------------------
doc: doctest godoc toc usage
DOCTEST_CMD = ./scripts/doctest.awk $(md) > $(O)/out.md && mv $(O)/out.md $(md)
DOCTESTS = docs/builtins.md docs/spec.md
doctest: install
$(foreach md,$(DOCTESTS),$(DOCTEST_CMD)$(nl))
TOC_CMD = ./scripts/toc.awk $(md) > $(O)/out.md && mv $(O)/out.md $(md)
TOCFILES = docs/builtins.md docs/spec.md
toc:
$(foreach md,$(TOCFILES),$(TOC_CMD)$(nl))
USAGE_CMD = ./scripts/gencmd.awk $(md) > $(O)/out.md && mv $(O)/out.md $(md)
USAGEFILES = docs/usage.md
usage: install
$(foreach md,$(USAGEFILES),$(USAGE_CMD)$(nl))
GODOC_CMD = ./scripts/gengodoc.awk $(filename) > $(O)/out.go && mv $(O)/out.go $(filename)
GODOCFILES = main.go
godoc: install
$(foreach filename,$(GODOCFILES),$(GODOC_CMD)$(nl))
.PHONY: doc doctest godoc toc usage
# --- frontend -----------------------------------------------------------------
NODEPREFIX = .hermit/node
NODELIB = $(NODEPREFIX)/lib
# BASEURL needs to be in the environment so that `e2e/playwright.config.js`
# can see it when the `e2e` target is called.
# The firebase-deploy script sets BASEURL to the deployment URL on GitHub CI.
export SERVEDIR_PORT ?= 8080
export BASEURL ?= http://localhost:$(SERVEDIR_PORT)
## Serve frontend on port 8080 by default to work with e2e target
serve:
servedir frontend
## Format code with prettier
prettier: | $(NODELIB)
npx --prefix $(NODEPREFIX) -y prettier --write .
## Ensure code is formatted with prettier
check-prettier: | $(NODELIB)
npx --prefix $(NODEPREFIX) -y prettier --check .
e2e:
@echo "testing $(BASEURL)"
npm --prefix e2e ci
npx --prefix e2e playwright install --with-deps chromium
npx --prefix e2e playwright test --config e2e
$(NODELIB):
@mkdir -p $@
.PHONY: check-prettier e2e prettier serve
# --- deploy -----------------------------------------------------------------
## Deploy to live channel on firebase prod, use with care!
## `firebase login` for first time local usage
deploy-prod: tiny
./scripts/firebase-deploy prod live
## Deploy to live channel on firebase stage.
## `firebase login` for first time local usage
deploy-stage: tiny
./scripts/firebase-deploy stage live
## Deploy to dev (or other) channel on firebase stage.
## `firebase login` for first time local usage
deploy: tiny
./scripts/firebase-deploy stage
.PHONY: deploy deploy-prod deploy-stage
# --- scripts ------------------------------------------------------------------
SCRIPTS = scripts/firebase-deploy .github/scripts/app_token
## Lint script files with shellcheck and shfmt
sh-lint:
shellcheck $(SCRIPTS)
shfmt --diff $(SCRIPTS)
## Format script files
sh-fmt:
shfmt --write $(SCRIPTS)
.PHONY: sh-fmt sh-lint
# --- Release -------------------------------------------------------------------
## Tag and release binaries for different OS on GitHub release
release: nexttag
git tag $(NEXTTAG)
git push origin $(NEXTTAG)
[ -z "$(CI)" ] || GITHUB_TOKEN=$$(.github/scripts/app_token) || exit 1; \
goreleaser release --clean $(if $(RELNOTES),--release-header=$(RELNOTES))
nexttag:
$(eval NEXTTAG := $(shell $(NEXTTAG_CMD)))
$(eval RELNOTES := $(wildcard docs/release-notes/$(NEXTTAG).md))
.PHONY: nexttag release
define NEXTTAG_CMD
{
{ git tag --list --merged HEAD --sort=-v:refname; echo v0.0.0; }
| grep -E "^v?[0-9]+\.[0-9]+\.[0-9]+$$"
| head -n 1
| awk -F . '{ print $$1 "." $$2 "." $$3 + 1 }';
git diff --name-only @^ | sed -E -n 's|^docs/release-notes/(v[0-9]+\.[0-9]+\.[0-9]+)\.md$$|\1|p';
} | sort --reverse --version-sort | head -n 1
endef
# --- Utilities ----------------------------------------------------------------
COLOUR_NORMAL = $(shell tput sgr0 2>/dev/null)
COLOUR_RED = $(shell tput setaf 1 2>/dev/null)
COLOUR_GREEN = $(shell tput setaf 2 2>/dev/null)
COLOUR_WHITE = $(shell tput setaf 7 2>/dev/null)
help:
$(eval export HELP_AWK)
@awk "$${HELP_AWK}" $(MAKEFILE_LIST) | sort | column -s "$$(printf \\t)" -t
$(O):
@mkdir -p $@
.PHONY: help
# Awk script to extract and print target descriptions for `make help`.
define HELP_AWK
/^## / { desc = desc substr($$0, 3) }
/^[A-Za-z0-9%_-]+:/ && desc {
sub(/::?$$/, "", $$1)
printf "$(COLOUR_WHITE)%s$(COLOUR_NORMAL)\t%s\n", $$1, desc
desc = ""
}
endef
define nl
endef
ifndef ACTIVE_HERMIT
$(eval $(subst \n,$(nl),$(shell bin/hermit env -r | sed 's/^\(.*\)$$/export \1\\n/')))
endif
# Ensure make version is gnu make 3.82 or higher
ifeq ($(filter undefine,$(value .FEATURES)),)
$(error Unsupported Make version. \
$(nl)Use GNU Make 3.82 or higher (current: $(MAKE_VERSION)). \
$(nl)Activate 🐚 hermit with `. bin/activate-hermit` and run again \
$(nl)or use `bin/make`)
endif