# The gate and the release run on one named Go, not whichever release this
# machine has. Raise it with each Go security release.
export GOTOOLCHAIN = go1.26.11
export CGO_ENABLED = 0

# The deployment platform target for a release. Override for a one-off:
#   make release RELEASE_GOARCH=amd64
RELEASE_GOOS   ?= linux
RELEASE_GOARCH ?= arm64
RELEASE_VERSION = $(shell git rev-parse --short HEAD)

# One target at a time, even under make -j.
.NOTPARALLEL:

.PHONY: script-test fonts build release test browser vet fmt-check staticcheck govulncheck gitleaks budget ci native-check

RASTRILLO := go run amadan.net/rastrillo/rastrillo/cmd/rastrillo

build:
	CGO_ENABLED=0 go build ./...

# Builds every binary under ./cmd, stamped with the clean commit. A release
# is set in Timeless Sans or not built at all: it fetches the font if it is
# missing and refuses to go on unless the file is the pinned one. (Dev and
# test builds fall back to DM Sans.)
release:
	@test -z "$$(git ls-files --others --exclude-standard)"
	@git diff --quiet && git diff --cached --quiet || \
		{ echo "refusing to build a release from a dirty tree"; exit 1; }
	./hack/fonts.sh
	@./hack/fonts.sh check || { echo "refusing to build a release without Timeless Sans"; exit 1; }
	@mkdir -p releases
	@for dir in cmd/*/; do \
		app=$$(basename $$dir); \
		out=releases/$$app-$(RELEASE_GOOS)-$(RELEASE_GOARCH)-$(RELEASE_VERSION); \
		CGO_ENABLED=0 GOOS=$(RELEASE_GOOS) GOARCH=$(RELEASE_GOARCH) go build -trimpath -buildvcs=false \
			-ldflags "-s -w -X amadan.net/rastrillo/rastrillo.BuildVersion=$(RELEASE_VERSION)" \
			-o $$out ./$$dir || exit 1; \
		git rev-parse HEAD > $$out.COMMIT; \
		echo "built $$out"; \
	done

budget:
	$(RASTRILLO) budget size

test:
	$(RASTRILLO) budget test -count=1 ./...

# The real-browser suite (internal/browsertest): Chromium drives Base and the
# apps it installs. Set RASTRILLO_CHROME to a Chromium, and
# BASE_BROWSER_ARTIFACTS to a directory to keep the screenshots it takes.
browser:
	go test -tags browser -count=1 -timeout 20m ./...

vet:
	go vet ./...

fmt-check:
	@out=$$(gofmt -l .); if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi

STATICCHECK := honnef.co/go/tools/cmd/staticcheck@v0.7.0
staticcheck:
	go run $(STATICCHECK) ./...

GOVULNCHECK := golang.org/x/vuln/cmd/govulncheck@v1.8.0
govulncheck:
	go run $(GOVULNCHECK) ./...

GITLEAKS := github.com/zricethezav/gitleaks/v8@v8.30.1
gitleaks:
	@if [ "$$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ] && [ -z "$$(git rev-parse --show-prefix)" ]; then \
		go run $(GITLEAKS) git --no-banner --redact --log-opts=HEAD .; \
	else \
		echo "gitleaks: not the root of a git repository, nothing committed to scan"; \
	fi

# Tests the operator script against a fake carlos, and the font checks the
# release relies on.
script-test:
	scripts/prepare-apps_test.sh
	hack/fonts_test.sh

# ci is the one gate: what a runner executes and what you run before pushing.
ci: build vet fmt-check staticcheck govulncheck gitleaks budget script-test test

# Fetches Timeless Sans, which the licence keeps out of the repository.
fonts:
	./hack/fonts.sh

# The native app's gate (native/): the string catalog, BaseKit's tests, the
# Mac app and an iPhone Simulator build. Needs a Mac with Xcode.
native-check:
	native/scripts/check-native.sh
