aboutsummaryrefslogtreecommitdiff
path: root/Makefile
diff options
context:
space:
mode:
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile89
1 files changed, 62 insertions, 27 deletions
diff --git a/Makefile b/Makefile
index 40c3ad2..2acc62f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,31 +1,45 @@
.POSIX:
-DESTDIR=$(HOME)
-PREFIX=/.local
-INSTALL_PATH=$(DESTDIR)$(PREFIX)/bin
-BIN=prognosis
-DIST=dist
-.PHONY: help build install uninstall test vet fmt ci cross clean
+# Standard GNU-ish install variables, so a packager can redirect everything:
+# make install PREFIX=/usr DESTDIR=/tmp/pkg
+DESTDIR ?=
+PREFIX ?= $(HOME)/.local
+BINDIR := $(DESTDIR)$(PREFIX)/bin
+MANDIR := $(DESTDIR)$(PREFIX)/share/man/man1
-help: ## show this help
- @grep -E '^[a-z-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[1m%-16s\033[0m %s\n", $$1, $$2}'
+BIN := prognosis
+DIST := dist
+MODULE := github.com/lukaszkasprzak/prognosis
+
+# Version comes from git when there is a tag, "dev" otherwise, and is stamped
+# into the binary so -version reports something meaningful.
+VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
+GOFLAGS := -trimpath
+LDFLAGS := -s -w -X main.version=$(VERSION)
+
+PLATFORMS := linux/amd64 linux/arm64 android/arm64 darwin/amd64 darwin/arm64 freebsd/amd64
-build: ## build the Go binary into ./$(BIN)
- go build -trimpath -ldflags "-s -w" -o $(BIN) ./cmd/prognosis
+.PHONY: help build install uninstall test vet fmt lint ci cross release install-hooks clean
-install: build ## build and install the Go binary
- mkdir -p $(INSTALL_PATH)
- # rm first: the target may be a symlink into this repo (see `link`), and cp
- # follows symlinks -- it would write the binary over bin/prognosis itself.
- rm -f $(INSTALL_PATH)/$(BIN)
- cp $(BIN) $(INSTALL_PATH)/$(BIN)
- chmod 755 $(INSTALL_PATH)/$(BIN)
- @echo "Installed. Config: $$($(INSTALL_PATH)/$(BIN) -config)"
+help: ## show this help
+ @grep -E '^[a-z-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
+ awk 'BEGIN {FS = ":.*?## "}; {printf " \033[1m%-14s\033[0m %s\n", $$1, $$2}'
+ @echo
+ @echo " version: $(VERSION) prefix: $(PREFIX)"
+build: ## build ./$(BIN)
+ go build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BIN) ./cmd/prognosis
+install: build ## install the binary and man page
+ mkdir -p $(BINDIR) $(MANDIR)
+ # rm first: the target may be a symlink, and cp follows symlinks.
+ rm -f $(BINDIR)/$(BIN)
+ install -m 755 $(BIN) $(BINDIR)/$(BIN)
+ install -m 644 man/prognosis.1 $(MANDIR)/prognosis.1
+ @echo "installed $(BINDIR)/$(BIN) and $(MANDIR)/prognosis.1"
-uninstall: ## remove the installed binary
- rm -f $(INSTALL_PATH)/$(BIN)
+uninstall: ## remove the installed binary and man page
+ rm -f $(BINDIR)/$(BIN) $(MANDIR)/prognosis.1
test: ## run the tests
go test ./...
@@ -36,21 +50,42 @@ vet: ## go vet
fmt: ## gofmt the tree
gofmt -w .
-ci: ## pre-push gate: gofmt clean, vet, tests, no third-party deps
+lint: ## check the man page renders without warnings
+ @out=$$(man --warnings -l man/prognosis.1 2>&1 >/dev/null); \
+ test -z "$$out" || { echo "man page warnings:"; echo "$$out"; exit 1; }
+ @echo "man page ok"
+
+ci: ## the gate: gofmt, vet, tests, man page, and no third-party deps
@test -z "$$(gofmt -l .)" || { echo "gofmt needed:"; gofmt -l .; exit 1; }
go vet ./...
go test ./...
- @deps=$$(go list -deps ./... | grep -E '^[a-z0-9-]+\.[a-z]+/' | grep -v '^github.com/lukaszkasprzak/prognosis' || true); \
+ @$(MAKE) --no-print-directory lint
+ @deps=$$(go list -deps ./... | grep -E '^[a-z0-9-]+\.[a-z]+/' | grep -v '^$(MODULE)' || true); \
test -z "$$deps" || { echo "third-party dependencies crept in:"; echo "$$deps"; exit 1; }
@echo "ci ok"
-cross: ## cross-compile into $(DIST)/ -- android/arm64 is the phone
- mkdir -p $(DIST)
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "-s -w" -o $(DIST)/$(BIN)-linux-amd64 ./cmd/prognosis
- CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -trimpath -ldflags "-s -w" -o $(DIST)/$(BIN)-linux-arm64 ./cmd/prognosis
- CGO_ENABLED=0 GOOS=android GOARCH=arm64 go build -trimpath -ldflags "-s -w" -o $(DIST)/$(BIN)-android-arm64 ./cmd/prognosis
+cross: ## cross-compile every platform into $(DIST)/
+ @mkdir -p $(DIST)
+ @for p in $(PLATFORMS); do \
+ os=$${p%/*}; arch=$${p#*/}; \
+ echo " $$os/$$arch"; \
+ CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch \
+ go build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(DIST)/$(BIN)-$$os-$$arch ./cmd/prognosis || exit 1; \
+ done
@ls -la $(DIST)
+release: ## tag a release: make release VERSION=0.2.0 (add its CHANGELOG entry first)
+ @test "$(VERSION)" != "dev" || { echo "give a version: make release VERSION=0.2.0"; exit 1; }
+ @grep -q "^## $(VERSION)" CHANGELOG.md || \
+ { echo "CHANGELOG.md has no '## $(VERSION)' entry; write it first"; exit 1; }
+ @test -z "$$(git status --porcelain)" || { echo "working tree is dirty"; exit 1; }
+ @$(MAKE) --no-print-directory ci
+ git tag -a v$(VERSION) -m "prognosis v$(VERSION)"
+ @echo "tagged v$(VERSION); push it with: git push --tags"
+
+install-hooks: ## install the pre-push hook that runs make ci
+ install -m 755 scripts/hooks/pre-push .git/hooks/pre-push
+ @echo "installed .git/hooks/pre-push"
clean: ## remove build artifacts
rm -rf $(BIN) $(DIST)