-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
98 lines (84 loc) · 3.29 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
MODULE = $(shell env GO111MODULE=on $(GO) list -m)
DATE ?= $(shell date +%FT%T%z)
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || \
cat $(CURDIR)/.version 2> /dev/null || echo v0)
COMMIT=$(shell git rev-parse HEAD)
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
GOVERSION=$(shell go version | awk -F\go '{print $$3}' | awk '{print $$1}')
PKGS = $(or $(PKG),$(shell env GO111MODULE=on $(GO) list ./...))
TESTPKGS = $(shell env GO111MODULE=on $(GO) list -f \
'{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' \
$(PKGS))
BUILDPATH ?= $(BIN)/$(shell basename $(MODULE))
SRC_FILES=find . -name "*.go" -type f -not -path "./vendor/*" -not -path "./.git/*" -not -path "./.cache/*" -print0 | xargs -0
BIN = $(CURDIR)/bin
TBIN = $(CURDIR)/test/bin
INTDIR = $(CURDIR)/test/int-test
GO = go
TIMEOUT = 15
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m➜\033[0m")
export GO111MODULE=on
export CGO_ENABLED=0
# Build
.PHONY: all
all: | $(BIN) ; $(info $(M) building executable to $(BUILDPATH)) @ ## Build program binary
$Q $(GO) build \
-tags release \
-ldflags '-X ${MODULE}/cmd/version.VERSION=${VERSION} -X ${MODULE}/cmd/version.COMMIT=${COMMIT} -X ${MODULE}/cmd/version.BRANCH=${BRANCH} -X ${MODULE}/cmd/version.GOVERSION=${GOVERSION} -X ${MODULE}/cmd/version.DATE=${DATE}' \
-o $(BUILDPATH) main.go
# Tools
$(BIN):
@mkdir -p $(BIN)
$(TBIN):
@mkdir -p $@
$(INTDIR):
@mkdir -p $@
$(TBIN)/%: | $(TBIN) ; $(info $(M) building $(PACKAGE))
$Q tmp=$$(mktemp -d); \
env GOBIN=$(TBIN) $(GO) install $(PACKAGE) \
|| ret=$$?; \
#rm -rf $$tmp ; exit $$ret
GOCILINT = $(TBIN)/golangci-lint
$(TBIN)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.1
# Tests
.PHONY: lint
lint: | $(GOCILINT) ; $(info $(M) running golangci-lint) @ ## Runs static code analysis using golangci-lint
$Q $(GOCILINT) run --timeout=5m
.PHONY: test
test: ; $(info $(M) running go test) @ ## Runs unit tests
$Q $(GO) test -v ${PKGS}
.PHONY: fmt
fmt: ; $(info $(M) running gofmt) @ ## Formats Go code
$Q $(GO) fmt $(PKGS)
.PHONY: vet
vet: ; $(info $(M) running go vet) @ ## Examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
$Q $(GO) vet $(PKGS)
.PHONY: race
race: ; $(info $(M) running go race) @ ## Runs tests with data race detection
$Q CGO_ENABLED=1 $(GO) test -race -short $(PKGS)
.PHONY: benchmark
benchmark: ; $(info $(M) running go benchmark test) @ ## Benchmark tests to examine performance
$Q $(GO) test -run=__absolutelynothing__ -bench=. $(PKGS)
.PHONY: coverage
coverage: ; $(info $(M) running go coverage) @ ## Runs tests and generates code coverage report at ./test/coverage.out
$Q mkdir -p $(CURDIR)/test/
$Q $(GO) test -coverprofile="$(CURDIR)/test/coverage.out" $(PKGS)
# Misc
.PHONY: help
help:
@grep -hE '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m∙ %s:\033[0m %s\n", $$1, $$2}'
.PHONY: version
version: ## Print version information
@echo App: $(VERSION)
@echo Go: $(GOVERSION)
@echo Commit: $(COMMIT)
@echo Branch: $(BRANCH)
.PHONY: clean
clean: ; $(info $(M) cleaning) @ ## Cleanup everything
@rm -rfv $(BIN)
@rm -rfv $(TBIN)
@rm -rfv $(CURDIR)/test
@rm -rfv $(CURDIR)/src