summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: d3c470d7691d432c658cce70140665707d4e91c7 (plain)
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
# Development tasks.
#
#   make            list the targets
#   make test       run the suite
#   make lint       luacheck if installed, otherwise luac -p; touches nothing
#   make build      byte-compile every module into build/
#
# LUA pins the interpreter. It defaults to `lua`; override it to run the suite
# on another one, e.g. `make test LUA=lua5.4` or `make test LUA=luajit`.
LUA ?= lua
LUAC ?= luac

SOURCES := cepis.lua $(wildcard cepis/*.lua)
ALL_LUA := $(SOURCES) $(wildcard test/*.lua)

.DEFAULT_GOAL := help
.PHONY: help test lint format build check clean

help:
	@echo 'Targets:'
	@echo '  test    run the test suite (LUA=$(LUA))'
	@echo '  lint    luacheck when installed, else $(LUAC) -p (read-only)'
	@echo '  format  apply stylua when installed (writes)'
	@echo '  build   byte-compile every module into build/'
	@echo '  check   lint + test + build, in that order'
	@echo '  clean   remove build artefacts'
	@echo
	@echo 'Override the interpreter with e.g. make test LUA=luajit'

test:
	@$(LUA) test/run.lua

# Read-only by design, so it is safe in CI and in a pre-commit hook. luacheck
# is the real linter; luac -p is the fallback that needs nothing installed and
# still catches a syntax error before the suite does.
lint:
	@if command -v luacheck >/dev/null 2>&1; then \
		luacheck $(ALL_LUA); \
	else \
		echo '==> luacheck not installed, checking syntax with $(LUAC) -p'; \
		$(LUAC) -p $(ALL_LUA) && echo 'syntax ok'; \
	fi

format:
	@if command -v stylua >/dev/null 2>&1; then \
		stylua $(ALL_LUA); \
	else \
		echo '==> stylua not installed, nothing was formatted'; \
	fi

build:
	@mkdir -p build
	@for source in $(SOURCES); do \
		$(LUAC) -o "build/$$(echo $$source | tr / _)c" "$$source" || exit 1; \
	done
	@echo 'byte-compiled into build/'

# The order matters: lint is the fastest to fail, so a broken run reports the
# cheapest problem first.
check: lint test build

clean:
	rm -rf build