|
| 1 | +MODULE = $(shell env GO111MODULE=on $(GO) list -m) |
| 2 | +DATE ?= $(shell date +%FT%T%z) |
| 3 | +PKGS = $(or $(PKG),$(shell env GO111MODULE=on $(GO) list ./...)) |
| 4 | +TESTPKGS = $(shell env GO111MODULE=on $(GO) list -f \ |
| 5 | + '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' \ |
| 6 | + $(PKGS)) |
| 7 | +BIN = $(CURDIR)/bin |
| 8 | + |
| 9 | +GO = go |
| 10 | +TIMEOUT = 15 |
| 11 | +V = 0 |
| 12 | +Q = $(if $(filter 1,$V),,@) |
| 13 | +M = $(shell printf "\033[34;1m▶\033[0m") |
| 14 | + |
| 15 | +binext="" |
| 16 | +ifeq ($(GOOS),windows) |
| 17 | + binext=".exe" |
| 18 | +endif |
| 19 | + |
| 20 | +export GO111MODULE=on |
| 21 | + |
| 22 | +.PHONY: all |
| 23 | +all: fmt lint vet build |
| 24 | + |
| 25 | +.PHONY: build |
| 26 | +build: $(BIN) ; $(info $(M) building executable…) @ ## Build program binary |
| 27 | + $Q CGO_ENABLED=0 $(GO) build \ |
| 28 | + -ldflags "-X main.gitVersion=$$(git describe --tags) -X $(MODULE)/cmd.gitVersion=$$(git describe --tags) -X \"main.buildTime=$$(date -u '+%Y-%m-%d %H:%M:%S %Z')\" -X \"$(MODULE)/cmd.buildTime=$$(date -u '+%Y-%m-%d %H:%M:%S %Z')\"" \ |
| 29 | + -tags release \ |
| 30 | + -o $(BIN)/$(notdir $(basename $(MODULE)))$(binext) |
| 31 | +# Tools |
| 32 | + |
| 33 | +$(BIN): |
| 34 | + @mkdir -p $@ |
| 35 | +$(BIN)/%: | $(BIN) ; $(info $(M) building $(PACKAGE)…) |
| 36 | + $Q env GOBIN=$(BIN) $(GO) install $(PACKAGE) \ |
| 37 | + || ret=$$?; \ |
| 38 | + exit $$ret |
| 39 | + |
| 40 | +GOLINT = $(BIN)/golint |
| 41 | +$(BIN)/golint: PACKAGE=golang.org/x/lint/golint@latest |
| 42 | + |
| 43 | +STATICCHECK = $(BIN)/staticcheck |
| 44 | +$(BIN)/staticcheck: PACKAGE=honnef.co/go/tools/cmd/staticcheck@latest |
| 45 | + |
| 46 | +ERRCHECK = $(BIN)/errcheck |
| 47 | +$(BIN)/errcheck: PACKAGE=github.com/kisielk/errcheck@latest |
| 48 | + |
| 49 | +VULNCHECK = $(BIN)/govulncheck |
| 50 | +$(BIN)/govulncheck: PACKAGE=golang.org/x/vuln/cmd/govulncheck@latest |
| 51 | + |
| 52 | +# Tests |
| 53 | + |
| 54 | +TEST_TARGETS := test-default test-bench test-short test-verbose test-race |
| 55 | +.PHONY: $(TEST_TARGETS) check test tests |
| 56 | +test-bench: ARGS=-run=__absolutelynothing__ -bench=. ## Run benchmarks |
| 57 | +test-short: ARGS=-short ## Run only short tests |
| 58 | +test-verbose: ARGS=-v ## Run tests in verbose mode |
| 59 | +test-race: ARGS=-race ## Run tests with race detector |
| 60 | +$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%) |
| 61 | +$(TEST_TARGETS): test |
| 62 | +check test tests: fmt lint vet staticcheck errcheck vulncheck; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests |
| 63 | + $Q $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS) |
| 64 | + |
| 65 | +.PHONY: lint |
| 66 | +lint: | $(GOLINT) ; $(info $(M) running golint…) @ ## Run golint |
| 67 | + $Q $(GOLINT) -set_exit_status $(PKGS) |
| 68 | + |
| 69 | +.PHONY: fmt |
| 70 | +fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files |
| 71 | + $Q $(GO) fmt $(PKGS) |
| 72 | + |
| 73 | +.PHONY: vet |
| 74 | +vet: ; $(info $(M) running go vet…) @ ## Run go vet on all source files |
| 75 | + $Q $(GO) vet $(PKGS) |
| 76 | + |
| 77 | +.PHONY: staticcheck |
| 78 | +staticcheck: | $(STATICCHECK) ; $(info $(M) running staticcheck…) @ |
| 79 | + $Q $(STATICCHECK) $(PKGS) |
| 80 | + |
| 81 | +.PHONY: errcheck |
| 82 | +errcheck: | $(ERRCHECK) ; $(info $(M) running errcheck…) @ |
| 83 | + $Q $(ERRCHECK) $(PKGS) |
| 84 | + |
| 85 | +.PHONY: vulncheck |
| 86 | +vulncheck: | $(VULNCHECK) ; $(info $(M) running vulncheck…) @ |
| 87 | + $Q $(VULNCHECK) $(PKGS) |
| 88 | + |
| 89 | +# Misc |
| 90 | + |
| 91 | +.PHONY: clean |
| 92 | +clean: ; $(info $(M) cleaning…) @ ## Cleanup everything |
| 93 | + @rm -rf $(BIN) |
| 94 | + @rm -rf test/tests.* |
| 95 | + |
| 96 | +.PHONY: help |
| 97 | +help: |
| 98 | + @grep -hE '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ |
| 99 | + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-17s\033[0m %s\n", $$1, $$2}' |
0 commit comments