@@ -29,6 +29,8 @@ GO ?= go
29
29
GOFMT ?= $(GO)fmt
30
30
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
31
31
GOOPTS ?=
32
+ GOHOSTOS ?= $(shell $(GO) env GOHOSTOS)
33
+ GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH)
32
34
33
35
GO_VERSION ?= $(shell $(GO) version)
34
36
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
@@ -62,17 +64,30 @@ PROMU := $(FIRST_GOPATH)/bin/promu
62
64
STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
63
65
pkgs = ./...
64
66
65
- GO_VERSION ?= $(shell $(GO) version)
66
- GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION)))
67
+ ifeq (arm, $(GOHOSTARCH))
68
+ GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM)
69
+ GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM)
70
+ else
71
+ GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)
72
+ endif
67
73
68
74
PROMU_VERSION ?= 0.2.0
69
75
PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
76
+ STATICCHECK_VERSION ?= 2019.1
77
+ STATICCHECK_URL := https://github.com/dominikh/go-tools/releases/download/$(STATICCHECK_VERSION)/staticcheck_$(GOHOSTOS)_$(GOHOSTARCH)
70
78
71
79
PREFIX ?= $(shell pwd)
72
80
BIN_DIR ?= $(shell pwd)
73
81
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
74
82
DOCKER_REPO ?= prom
75
83
84
+ ifeq ($(GOHOSTARCH),amd64)
85
+ ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows))
86
+ # Only supported on amd64
87
+ test-flags := -race
88
+ endif
89
+ endif
90
+
76
91
.PHONY: all
77
92
all: precheck style staticcheck unused build test
78
93
@@ -110,12 +125,12 @@ common-test-short:
110
125
.PHONY: common-test
111
126
common-test:
112
127
@echo ">> running all tests"
113
- GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs)
128
+ GO111MODULE=$(GO111MODULE) $(GO) test $(test-flags) $(GOOPTS) $(pkgs)
114
129
115
130
.PHONY: common-format
116
131
common-format:
117
132
@echo ">> formatting code"
118
- GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $( pkgs)
133
+ GO111MODULE=$(GO111MODULE) $(GO) fmt $(pkgs)
119
134
120
135
.PHONY: common-vet
121
136
common-vet:
@@ -125,8 +140,12 @@ common-vet:
125
140
.PHONY: common-staticcheck
126
141
common-staticcheck: $(STATICCHECK)
127
142
@echo ">> running staticcheck"
143
+ chmod +x $(STATICCHECK)
128
144
ifdef GO111MODULE
129
- GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs)
145
+ # 'go list' needs to be executed before staticcheck to prepopulate the modules cache.
146
+ # Otherwise staticcheck might fail randomly for some reason not yet explained.
147
+ GO111MODULE=$(GO111MODULE) $(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null
148
+ GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
130
149
else
131
150
$(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
132
151
endif
140
159
ifdef GO111MODULE
141
160
@echo ">> running check for unused/missing packages in go.mod"
142
161
GO111MODULE=$(GO111MODULE) $(GO) mod tidy
162
+ ifeq (,$(wildcard vendor))
143
163
@git diff --exit-code -- go.sum go.mod
144
- ifneq (,$(wildcard vendor))
164
+ else
145
165
@echo ">> running check for unused packages in vendor/"
146
166
GO111MODULE=$(GO111MODULE) $(GO) mod vendor
147
167
@git diff --exit-code -- go.sum go.mod vendor/
@@ -175,30 +195,20 @@ common-docker-tag-latest:
175
195
promu: $(PROMU)
176
196
177
197
$(PROMU):
178
- curl -s -L $(PROMU_URL) | tar -xvz -C /tmp
179
- mkdir -v -p $(FIRST_GOPATH)/bin
180
- cp -v /tmp/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(PROMU)
198
+ $(eval PROMU_TMP := $(shell mktemp -d))
199
+ curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
200
+ mkdir -p $(FIRST_GOPATH)/bin
201
+ cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
202
+ rm -r $(PROMU_TMP)
181
203
182
204
.PHONY: proto
183
205
proto:
184
206
@echo ">> generating code from proto files"
185
207
@./scripts/genproto.sh
186
208
187
- .PHONY: $(STATICCHECK)
188
209
$(STATICCHECK):
189
- ifdef GO111MODULE
190
- # Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}.
191
- # See https://github.com/golang/go/issues/27643.
192
- # For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules.
193
- tmpModule=$$(mktemp -d 2>&1) && \
194
- mkdir -p $${tmpModule}/staticcheck && \
195
- cd "$${tmpModule}"/staticcheck && \
196
- GO111MODULE=on $(GO) mod init example.com/staticcheck && \
197
- GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \
198
- rm -rf $${tmpModule};
199
- else
200
- GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck
201
- endif
210
+ mkdir -p $(FIRST_GOPATH)/bin
211
+ curl -s -L $(STATICCHECK_URL) > $(STATICCHECK)
202
212
203
213
ifdef GOVENDOR
204
214
.PHONY: $(GOVENDOR)
@@ -214,7 +224,7 @@ precheck:: $(1)_precheck
214
224
215
225
216
226
PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
217
- .PHONE : $(1)_precheck
227
+ .PHONY : $(1)_precheck
218
228
$(1)_precheck:
219
229
@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
220
230
echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
0 commit comments