Skip to content

Commit 2998b13

Browse files
Update Makefile.common (#165)
Signed-off-by: Simon Pasquier <[email protected]>
1 parent b1c43a6 commit 2998b13

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

Makefile.common

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ GO ?= go
2929
GOFMT ?= $(GO)fmt
3030
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
3131
GOOPTS ?=
32+
GOHOSTOS ?= $(shell $(GO) env GOHOSTOS)
33+
GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH)
3234

3335
GO_VERSION ?= $(shell $(GO) version)
3436
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
@@ -62,17 +64,30 @@ PROMU := $(FIRST_GOPATH)/bin/promu
6264
STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
6365
pkgs = ./...
6466

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
6773

6874
PROMU_VERSION ?= 0.2.0
6975
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)
7078

7179
PREFIX ?= $(shell pwd)
7280
BIN_DIR ?= $(shell pwd)
7381
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
7482
DOCKER_REPO ?= prom
7583

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+
7691
.PHONY: all
7792
all: precheck style staticcheck unused build test
7893

@@ -110,12 +125,12 @@ common-test-short:
110125
.PHONY: common-test
111126
common-test:
112127
@echo ">> running all tests"
113-
GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs)
128+
GO111MODULE=$(GO111MODULE) $(GO) test $(test-flags) $(GOOPTS) $(pkgs)
114129

115130
.PHONY: common-format
116131
common-format:
117132
@echo ">> formatting code"
118-
GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs)
133+
GO111MODULE=$(GO111MODULE) $(GO) fmt $(pkgs)
119134

120135
.PHONY: common-vet
121136
common-vet:
@@ -125,8 +140,12 @@ common-vet:
125140
.PHONY: common-staticcheck
126141
common-staticcheck: $(STATICCHECK)
127142
@echo ">> running staticcheck"
143+
chmod +x $(STATICCHECK)
128144
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)
130149
else
131150
$(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
132151
endif
@@ -140,8 +159,9 @@ else
140159
ifdef GO111MODULE
141160
@echo ">> running check for unused/missing packages in go.mod"
142161
GO111MODULE=$(GO111MODULE) $(GO) mod tidy
162+
ifeq (,$(wildcard vendor))
143163
@git diff --exit-code -- go.sum go.mod
144-
ifneq (,$(wildcard vendor))
164+
else
145165
@echo ">> running check for unused packages in vendor/"
146166
GO111MODULE=$(GO111MODULE) $(GO) mod vendor
147167
@git diff --exit-code -- go.sum go.mod vendor/
@@ -175,30 +195,20 @@ common-docker-tag-latest:
175195
promu: $(PROMU)
176196

177197
$(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)
181203

182204
.PHONY: proto
183205
proto:
184206
@echo ">> generating code from proto files"
185207
@./scripts/genproto.sh
186208

187-
.PHONY: $(STATICCHECK)
188209
$(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)
202212

203213
ifdef GOVENDOR
204214
.PHONY: $(GOVENDOR)
@@ -214,7 +224,7 @@ precheck:: $(1)_precheck
214224

215225

216226
PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
217-
.PHONE: $(1)_precheck
227+
.PHONY: $(1)_precheck
218228
$(1)_precheck:
219229
@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
220230
echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \

0 commit comments

Comments
 (0)