-
Notifications
You must be signed in to change notification settings - Fork 11
/
Makefile
167 lines (129 loc) · 5.16 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# TOOLCHAIN
GO := CGO_ENABLED=0 GOBIN=$(CURDIR)/bin go
GO_BIN_IN_PATH := CGO_ENABLED=0 go
GOFMT := $(GO)fmt
# ENVIRONMENT
VERBOSE =
GOPATH := $(GOPATH)
# APPLICATION INFORMATION
BUILD_DATE := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
REVISION := $(shell git rev-parse --short HEAD)
RELEASE := $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)-dev
USER := $(shell whoami)
# TOOLS
GEN_CLI_DOCS := bin/gen-cli-docs
GOLANGCI_LINT := bin/golangci-lint
GORELEASER := bin/goreleaser
GOTESTSUM := bin/gotestsum
STRINGER := bin/stringer
# MISC
COVERPROFILE := coverage.out
DIST_DIR := dist
MANPAGES_DIR := man
# GO TAGS
GO_TAGS := osusergo netgo static_build
# GO LD FLAGS
GO_LD_FLAGS := -s -w -extldflags "-fno-PIC -static -Wl -z now -z relro"
GO_LD_FLAGS += -X github.com/axiomhq/pkg/version.release=$(RELEASE)
GO_LD_FLAGS += -X github.com/axiomhq/pkg/version.revision=$(REVISION)
GO_LD_FLAGS += -X github.com/axiomhq/pkg/version.buildDate=$(BUILD_DATE)
GO_LD_FLAGS += -X github.com/axiomhq/pkg/version.buildUser=$(USER)
# FLAGS
GO_FLAGS := -buildvcs=false -buildmode=pie -installsuffix=cgo -trimpath -tags='$(GO_TAGS)' -ldflags='$(GO_LD_FLAGS)'
GO_TEST_FLAGS := -race -coverprofile=$(COVERPROFILE)
GORELEASER_FLAGS := --snapshot --clean
# DEPENDENCIES
GOMODDEPS = go.mod go.sum
# Enable verbose test output if explicitly set.
GOTESTSUM_FLAGS =
ifdef VERBOSE
GOTESTSUM_FLAGS += --format=standard-verbose
endif
# FUNCTIONS
# func go-list-pkg-sources(package)
go-list-pkg-sources = $(GO) list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $(1)
# func go-pkg-sourcefiles(package)
go-pkg-sourcefiles = $(shell $(call go-list-pkg-sources,$(strip $1)))
.PHONY: all
all: dep generate fmt lint test build man ## Run dep, generate, fmt, lint, test, build and man
.PHONY: build
build: $(GORELEASER) dep.stamp $(call go-pkg-sourcefiles, ./...) ## Build the binaries
@echo ">> building binaries"
@$(GORELEASER) build $(GORELEASER_FLAGS)
.PHONY: clean
clean: ## Remove build and test artifacts
@echo ">> cleaning up artifacts"
@rm -rf bin $(DIST_DIR) $(MANPAGES_DIR) $(COVERPROFILE)
.PHONY: cover
cover: $(COVERPROFILE) ## Calculate the code coverage score
@echo ">> calculating code coverage"
@$(GO) tool cover -func=$(COVERPROFILE) | tail -n1
.PHONY: dep-clean
dep-clean: ## Remove obsolete dependencies
@echo ">> cleaning dependencies"
@$(GO) mod tidy
.PHONY: dep-upgrade
dep-upgrade: ## Upgrade all direct dependencies to their latest version
@echo ">> upgrading dependencies"
@$(GO) get $(shell $(GO) list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
@make dep
.PHONY: dep
dep: dep-clean dep.stamp ## Install and verify dependencies and remove obsolete ones
dep.stamp: $(GOMODDEPS)
@echo ">> installing dependencies"
@$(GO) mod download
@$(GO) mod verify
@touch $@
.PHONY: fmt
fmt: ## Format and simplify the source code using `gofmt`
@echo ">> formatting code"
@! $(GOFMT) -s -w $(shell find . -path -prune -o -name '*.go' -print) | grep '^'
.PHONY: generate
generate: $(STRINGER) pkg/iofmt/format_string.go ## Generate code using `go generate`
.PHONY: install
install: $(GOPATH)/bin/axiom ## Install the binary into the $GOPATH/bin directory
.PHONY: lint
lint: $(GOLANGCI_LINT) ## Lint the source code
@echo ">> linting code"
@$(GOLANGCI_LINT) run
.PHONY: man
man: $(GEN_CLI_DOCS) ## Generate man pages
@echo ">> generate man pages"
@rm -rf $(MANPAGES_DIR)
@$(GEN_CLI_DOCS) -d=$(MANPAGES_DIR) -t=$(RELEASE)
.PHONY: test
test: $(GOTESTSUM) ## Run all tests. Run with VERBOSE=1 to get verbose test output (`-v` flag)
@echo ">> running tests"
@$(GOTESTSUM) $(GOTESTSUM_FLAGS) -- $(GO_TEST_FLAGS) ./...
.PHONY: tools
tools: $(GEN_CLI_DOCS) $(GOLANGCI_LINT) $(GORELEASER) $(GOTESTSUM) $(STRINGER) ## Install all tools into the projects local $GOBIN directory
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
# GO GENERATE TARGETS
pkg/iofmt/%_string.go: pkg/iofmt/%.go
@echo ">> generating $@ from $<"
@$(GO) generate $<
# MISC TARGETS
$(COVERPROFILE):
@make test
# INSTALL TARGETS
$(GOPATH)/bin/axiom: dep.stamp $(call go-pkg-sourcefiles, ./cmd/axiom)
@echo ">> installing axiom binary"
@$(GO_BIN_IN_PATH) install $(GO_FLAGS) ./cmd/axiom
# TOOLS
$(GEN_CLI_DOCS): dep.stamp $(call go-pkg-sourcefiles, github.com/axiomhq/cli/tools/gen-cli-docs) $(call go-pkg-sourcefiles, ./cmd/axiom)
@echo ">> installing gen-cli-docs"
@$(GO) install github.com/axiomhq/cli/tools/gen-cli-docs
$(GOLANGCI_LINT): dep.stamp $(call go-pkg-sourcefiles, github.com/golangci/golangci-lint/cmd/golangci-lint)
@echo ">> installing golangci-lint"
@$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint
$(GORELEASER): dep.stamp $(call go-pkg-sourcefiles, github.com/goreleaser/goreleaser/v2)
@echo ">> installing goreleaser"
@$(GO) install github.com/goreleaser/goreleaser/v2
$(GOTESTSUM): dep.stamp $(call go-pkg-sourcefiles, gotest.tools/gotestsum)
@echo ">> installing gotestsum"
@$(GO) install gotest.tools/gotestsum
$(STRINGER): dep.stamp $(call go-pkg-sourcefiles, golang.org/x/tools/cmd/stringer)
@echo ">> installing stringer"
@$(GO) install golang.org/x/tools/cmd/stringer