/
Makefile
92 lines (78 loc) · 2.9 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
.DEFAULT_GOAL := help
SHELL := bash
# based in part on: https://povilasv.me/exposing-go-modules-to-prometheus/
PROGRAM := of
PACKAGE := github.com/cisco-cx/$(PROGRAM)
INFO_PACKAGE := $(PACKAGE)/info
LICENSE := Apache-2.0
URL := https://$(PACKAGE)
DATE := $(shell date +%FT%T%z)
USER := $(shell whoami)
GIT_HASH := $(shell git --no-pager describe --tags --always)
BRANCH := $(shell git branch | grep '*' | cut -d ' ' -f2)
GOFLAGS := GOFLAGS="-mod=vendor"
GOFILES := $(shell $(GOFLAGS) go list ./... | grep -oP "/of/\K.*") # ref: https://stackoverflow.com/a/39530381
LDFLAGS := -s
LDFLAGS += -X "$(INFO_PACKAGE).Program=$(PROGRAM)"
LDFLAGS += -X "$(INFO_PACKAGE).License=$(LICENSE)"
LDFLAGS += -X "$(INFO_PACKAGE).URL=$(URL)"
LDFLAGS += -X "$(INFO_PACKAGE).BuildUser=$(USER)"
LDFLAGS += -X "$(INFO_PACKAGE).BuildDate=$(DATE)"
LDFLAGS += -X "$(INFO_PACKAGE).Version=$(GIT_HASH)"
LDFLAGS += -X "$(INFO_PACKAGE).Revision=$(GIT_HASH)"
LDFLAGS += -X "$(INFO_PACKAGE).Branch=$(BRANCH)"
.PHONY: all
all: | refine test build ## Run all generally applicable targets.
.PHONY: build
build: ## Build the program for Linux.
@echo "==> Building the program for Linux."
$(GOFLAGS) CGO_ENABLED=0 GOOS=linux go build -v -a -ldflags '$(LDFLAGS)' .
.PHONY: clean
clean: ## Clean temporary files.
@echo "==> Cleaning temporary files."
rm -f ./*.html
rm -f ./*.pdf
rm -f ./*.pprof
rm -f ./cp.out
rm -f ./of
.PHONY: docker
docker: ## Build a docker image for local dev.
docker build . -t of:local
.PHONY: refine
refine: ## Run all formatters and static analysis.
@echo "==> Running all formatters and static analysis."
gofmt -w $(GOFILES) # ref: https://github.com/golang/go/issues/27841
.PHONY: report
report: ## Generate all reports.
@echo "==> Generating profiler reports."
for mode in cpu mem mutex block; do \
if [ -e $$mode.pprof ]; then go tool pprof --pdf $(PROGRAM) $$mode.pprof > $$mode.pprof.pdf; fi; done
@if [ -d ~/x/tmp ] && compgen -G "*.pdf"; then cp -v *.pdf ~/x/tmp; fi
@echo "==> Generating coverage reports."
$(GOFLAGS) go tool cover -html=cp.out -o=coverage.html
.PHONY: test
test: ## Run all tests and generate all reports.
@echo "==> Running all tests."
$(GOFLAGS) go test ./... -coverprofile=cp.out
$(GOFLAGS) go tool cover -func=cp.out
$(MAKE) vet
$(MAKE) report
.PHONY: tidy
tidy: ## Run go mod tidy (depends on access to github.com)
@echo "==> Tidying dependency list."
go mod tidy -v
.PHONY: vet
vet: ## Run Go vet.
@echo "==> Running Go vet."
$(GOFLAGS) go vet ./...
.PHONY: vendor
vendor: ## Re-vendor dependencies. (depends on access to github.com)
@echo "==> Re-vendoring dependencies."
go mod vendor -v
go mod verify
.PHONY: help
help: ## Print list of Makefile targets.
@# Taken from https://github.com/spf13/hugo/blob/master/Makefile
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
cut -d ":" -f1- | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'