-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMakefile
125 lines (100 loc) · 3.63 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
GOMOD ?= on
GO ?= GO111MODULE=$(GOMOD) go
#Don't enable mod=vendor when GOMOD is off or else go build/install will fail
GOMODFLAG ?=-mod=vendor
ifeq ($(GOMOD), off)
GOMODFLAG=
endif
#retrieve go version details for version check
GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
GO_VERSION_MAJ := $(shell echo $(GO_VERSION) | cut -f1 -d'.')
GO_VERSION_MIN := $(shell echo $(GO_VERSION) | cut -f2 -d'.')
GOFMT ?= gofmt
GO_MD2MAN ?= go-md2man
LN = ln
RM = rm
BINPATH := $(abspath ./bin)
GOBINPATH := $(shell $(GO) env GOPATH)/bin
COMMIT := $(shell git rev-parse HEAD)
BUILD_DATE := $(shell date +%Y%m%d)
# TAG can be provided as an envvar (provided in the .spec file)
TAG ?= $(shell git describe --tags --exact-match HEAD 2> /dev/null)
# CLOSEST_TAG can be provided as an envvar (provided in the .spec file)
CLOSEST_TAG ?= $(shell git describe --tags)
# VERSION is inferred from CLOSEST_TAG
# It accepts tags of type `vX.Y.Z`, `vX.Y.Z-(alpha|beta|rc|...)` and produces X.Y.Z
VERSION := $(shell echo $(CLOSEST_TAG) | sed -E 's/v(([0-9]\.?)+).*/\1/')
TAGS := development
PROJECT_PATH := github.com/flavio/kuberlr
KUBERLR_LDFLAGS = -ldflags "-X=$(PROJECT_PATH)/pkg/kuberlr.Version=$(VERSION) \
-X=$(PROJECT_PATH)/pkg/kuberlr.BuildDate=$(BUILD_DATE) \
-X=$(PROJECT_PATH)/pkg/kuberlr.Tag=$(TAG) \
-X=$(PROJECT_PATH)/pkg/kuberlr.ClosestTag=$(CLOSEST_TAG)"
KUBERLR_DIRS = cmd pkg internal
# go source files, ignore vendor directory
KUBERLR_SRCS = $(shell find $(KUBERLR_DIRS) -type f -name '*.go')
.PHONY: all
all: install
.PHONY: build
build: go-version-check
$(GO) build $(GOMODFLAG) $(KUBERLR_LDFLAGS) -tags $(TAGS) ./cmd/...
MANPAGES_MD := $(wildcard docs/man/*.md)
MANPAGES := $(MANPAGES_MD:%.md=%)
docs/man/%.1: docs/man/%.1.md
$(GO_MD2MAN) -in $< -out $@
.PHONY: docs
docs: $(MANPAGES)
.PHONY: install
install: go-version-check
$(GO) install $(GOMODFLAG) $(KUBERLR_LDFLAGS) -tags $(TAGS) ./cmd/...
.PHONY: clean
clean:
$(GO) clean -i ./...
$(RM) -f ./kuberlr
$(RM) -rf $(BINPATH)
.PHONY: distclean
distclean: clean
$(GO) clean -i -cache -testcache -modcache ./...
.PHONY: staging
staging:
make TAGS=staging install
.PHONY: release
release:
make TAGS=release install
.PHONY: go-version-check
go-version-check:
@[ $(GO_VERSION_MAJ) -ge 2 ] || \
[ $(GO_VERSION_MAJ) -eq 1 -a $(GO_VERSION_MIN) -ge 12 ] || (echo "FATAL: Go version should be >= 1.12.x" ; exit 1 ; )
.PHONY: lint
lint: deps
# explicitly enable GO111MODULE otherwise go mod will fail
GO111MODULE=on go mod tidy && GO111MODULE=on go mod vendor && GO111MODULE=on go mod verify
# run go vet
$(GO) vet ./...
# run go gmt
test -z `$(GOFMT) -l $(KUBERLR_SRCS)` || { $(GOFMT) -d $(KUBERLR_SRCS) && false; }
# run golint
golint -set_exit_status cmd/... internal/... pkg/...
.PHONY: deps
deps:
go get -u golang.org/x/lint/golint
.PHONY: pre-commit-install
pre-commit-install:
test -f $(BINPATH)/bin/pre-commit || curl -sfL https://pre-commit.com/install-local.py | HOME=$(BINPATH) python -
$(BINPATH)/bin/pre-commit install
.PHONY: pre-commit-uninstall
pre-commit-uninstall:
test -f $(BINPATH)/bin/pre-commit || curl -sfL https://pre-commit.com/install-local.py | HOME=$(BINPATH) python -
$(BINPATH)/bin/pre-commit uninstall
# tests
.PHONY: test
test: test-unit test-bench
.PHONY: test-unit
test-unit:
$(GO) test $(GOMODFLAG) -coverprofile=coverage.out $(PROJECT_PATH)/{cmd,pkg,internal}/...
.PHONY: test-unit-coverage
test-unit-coverage: test-unit
$(GO) tool cover -html=coverage.out
.PHONY: test-bench
test-bench:
$(GO) test $(GOMODFLAG) -bench=. $(PROJECT_PATH)/{cmd,pkg,internal}/...