-
Notifications
You must be signed in to change notification settings - Fork 50
/
Makefile
195 lines (155 loc) · 3.59 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
PIP := $(shell command -v pip3 || command -v pip)
SOURCES := $(shell \
find * \
-not -path 'sources/*' \
-not -path 'builds/*' \( \
-name "*.go" -or \
-name "go.mod" -or \
-name "go.sum" -or \
-name "Makefile" -or \
-type f -path 'internal/*' -or \
-type f -path 'cmd/*' -or \
-type f -path 'pkg/*' \
\) | grep -v '.DS_Store' \
)
#
# Environment
#
# Verbose output
ifdef VERBOSE
V = -v
endif
BINDIR := bin
TOOLDIR := $(BINDIR)/tools
# Global environment variables for all targets
SHELL ?= /bin/bash
SHELL := env \
GO111MODULE=on \
GOBIN=$(CURDIR)/$(BINDIR) \
CGO_ENABLED=0 \
PATH='$(CURDIR)/$(BINDIR):$(CURDIR)/$(TOOLDIR):$(PATH)' \
$(SHELL)
#
# Defaults
#
# Default target
.DEFAULT_GOAL := build
#
# Bootstrap
#
bootstrap: bootstrap-brew bootstrap-ruby
bootstrap-ruby:
env BUNDLE_WITHOUT=development bundle install
bootstrap-brew:
ifndef IN_NIX_SHELL
brew bundle --verbose
endif
bootstrap-pip:
$(PIP) install -r requirements-ci.txt
#
# Tools
#
# external tool
define tool # 1: binary-name, 2: go-import-path
TOOLS += $(TOOLDIR)/$(1)
$(TOOLDIR)/$(1): Makefile
GOBIN="$(CURDIR)/$(TOOLDIR)" go install "$(2)"
endef
$(eval $(call tool,gofumpt,mvdan.cc/gofumpt@latest))
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/[email protected]))
$(eval $(call tool,gomod,github.com/Helcaraxan/gomod@latest))
.PHONY: tools
tools: $(TOOLS)
#
# Build
#
LDFLAGS := -w -s
VERSION ?= $(shell git describe --tags --exact 2>/dev/null)
COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null)
DATE ?= $(shell date '+%FT%T%z')
ifeq ($(VERSION),)
VERSION = 0.0.0-dev
endif
CMDDIR := cmd
BINS := $(shell test -d "$(CMDDIR)" && cd "$(CMDDIR)" && \
find * -maxdepth 0 -type d -exec echo $(BINDIR)/{} \;)
.PHONY: build
build: $(BINS)
$(BINS): $(BINDIR)/%: $(SOURCES)
mkdir -p "$(BINDIR)"
cd "$(CMDDIR)/$*" && go build -a $(V) \
-o "$(CURDIR)/$(BINDIR)/$*" \
-ldflags "$(LDFLAGS) \
-X main.version=$(VERSION) \
-X main.commit=$(COMMIT) \
-X main.date=$(DATE)"
#
# Development
#
TEST ?= $$(go list ./... | grep -v 'sources/' | grep -v 'builds/')
.PHONY: clean
clean:
rm -rf $(BINARY) $(TOOLS)
rm -f ./go.mod.tidy-check ./go.sum.tidy-check
.PHONY: test
test:
CGO_ENABLED=1 go test $(V) -count=1 -race $(TESTARGS) $(TEST)
.PHONY: lint
lint: $(TOOLDIR)/golangci-lint
golangci-lint $(V) run
.PHONY: format
format: $(TOOLDIR)/gofumpt
gofumpt -w .
.PHONY: gen
gen:
go generate $$(go list ./... | grep -v 'sources/' | grep -v 'builds/')
.PHONY: nix-flake-update
nix-flake-update:
nix flake update \
&& $(MAKE) flake-package-versions.txt
.SILENT: flake-package-versions
flake-package-versions:
nix develop --command -- bash -c \
'nix derivation show \
$$(echo $$PATH | tr ":" "\n" | grep "/nix/store" | sort -u) \
| jq -r ".[].name" | sort -u'
flake-package-versions.txt: flake.nix flake.lock
$(MAKE) flake-package-versions > flake-package-versions.txt
#
# Dependencies
#
.PHONY: deps
deps:
$(info Downloading dependencies)
go mod download
.PHONY: deps-update
deps-update:
$(info Downloading dependencies)
go get -u -t ./...
.PHONY: deps-analyze
deps-analyze: $(TOOLDIR)/gomod
gomod analyze
.PHONY: tidy
tidy:
go mod tidy $(V)
.PHONY: verify
verify:
go mod verify
.SILENT: check-tidy
.PHONY: check-tidy
check-tidy:
cp go.mod go.mod.tidy-check
cp go.sum go.sum.tidy-check
go mod tidy
( \
diff go.mod go.mod.tidy-check && \
diff go.sum go.sum.tidy-check && \
rm -f go.mod go.sum && \
mv go.mod.tidy-check go.mod && \
mv go.sum.tidy-check go.sum \
) || ( \
rm -f go.mod go.sum && \
mv go.mod.tidy-check go.mod && \
mv go.sum.tidy-check go.sum; \
exit 1 \
)