# make build     builds dibs for this machine
# make test      runs the tests; GOTESTFLAGS=-short skips those that start Chrome
# make lint      runs golangci-lint, built from source at a pinned version
# make release   cross-compiles TARGETS into dist/, with SHA256SUMS
#
# VERSION defaults to `git describe`, such as v0.3.0 or v0.3.0-4-gabc1234-dirty.

VERSION     ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
TARGETS     ?= linux/amd64 windows/amd64 darwin/arm64
GOTESTFLAGS ?=

GOLANGCI_LINT ?= go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.14.0

# File names carry the version without its "v", like dibs-0.3.0-linux-amd64.
FILE_VERSION := $(patsubst v%,%,$(VERSION))
LDFLAGS      := -s -w -X main.version=$(VERSION)

.PHONY: build test lint release clean

build:
	CGO_ENABLED=0 go build -trimpath -ldflags '$(LDFLAGS)' -o dibs$(shell go env GOEXE) ./cmd/dibs

test:
	go test $(GOTESTFLAGS) ./...

lint:
	$(GOLANGCI_LINT) run ./...

release:
	rm -rf dist
	mkdir -p dist
	set -e; for target in $(TARGETS); do \
		os=$${target%/*}; arch=$${target#*/}; ext=; \
		if [ "$$os" = windows ]; then ext=.exe; fi; \
		out=dist/dibs-$(FILE_VERSION)-$$os-$$arch$$ext; \
		echo "$$out"; \
		CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags '$(LDFLAGS)' -o "$$out" ./cmd/dibs; \
	done
	cd dist && sha256sum dibs-* > SHA256SUMS

clean:
	rm -rf dist dibs dibs.exe
