# Developer and CI entry points. CI (.github/workflows/*.yml) calls these
# targets so local runs and CI stay identical.

GO       ?= go
GOFLAGS  ?= -mod=vendor
export GOFLAGS

# Go sources we own (vendor/ is third-party and not reformatted).
GOFILES   = $(shell git ls-files '*.go' | grep -v '^vendor/')

# Targets mutate vendor/ (vendor-check) and then build from it: never run them
# concurrently, even under `make -j`.
.NOTPARALLEL:

.PHONY: all build test race lint fmt-check vet vendor-check ci fmt

all: build

build:
	$(GO) build ./...

test:
	$(GO) test -count=1 ./...

race:
	$(GO) test -race -count=1 ./...

fmt:
	gofmt -w $(GOFILES)

fmt-check:
	@out="$$(gofmt -l $(GOFILES) $$(git ls-files --others --exclude-standard '*.go' | grep -v '^vendor/'))"; \
	if [ -n "$$out" ]; then echo "gofmt needed for:"; echo "$$out"; exit 1; fi

vet:
	$(GO) vet ./...

# go mod vendor must be a no-op: go.mod, go.sum and vendor/ are committed.
vendor-check:
	GOFLAGS= $(GO) mod vendor
	git diff --exit-code -- vendor go.mod go.sum
	@untracked="$$(git ls-files --others --exclude-standard vendor)"; \
	if [ -n "$$untracked" ]; then echo "untracked files in vendor/:"; echo "$$untracked"; exit 1; fi

lint: fmt-check vet vendor-check

ci: lint build race
