aboutsummaryrefslogtreecommitdiff
path: root/Makefile.common
diff options
context:
space:
mode:
authorBen Kochie <superq@gmail.com>2018-11-30 14:01:20 +0100
committerGitHub <noreply@github.com>2018-11-30 14:01:20 +0100
commitbecca1275c77ffab31ec7082b3bd9ba9382a6c1a (patch)
tree3c201ab4743d461ecdb9ccd39928c3a9dd2a4948 /Makefile.common
parent1732478361a57dea9f7cd0f319c5a34cebcf047d (diff)
downloadprometheus_node_collector-becca1275c77ffab31ec7082b3bd9ba9382a6c1a.tar.bz2
prometheus_node_collector-becca1275c77ffab31ec7082b3bd9ba9382a6c1a.tar.xz
prometheus_node_collector-becca1275c77ffab31ec7082b3bd9ba9382a6c1a.zip
Convert to Go modules (#1178)
* Convert to Go modules * Update promu config. * Convert to Go modules. * Update vendoring. * Update Makefile.common. * Update circleci config. * Use Prometheus release tar for promtool. * Fixup unpack * Use temp dir for unpacking tools. * Use BSD compatible tar command. * OpenBSD mkdir doesn't support `-v`. Signed-off-by: Ben Kochie <superq@gmail.com>
Diffstat (limited to 'Makefile.common')
-rw-r--r--Makefile.common124
1 files changed, 111 insertions, 13 deletions
diff --git a/Makefile.common b/Makefile.common
index 61ef11d..8e135c5 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -16,7 +16,7 @@
16# !!! Open PRs only against the prometheus/prometheus/Makefile.common repository! 16# !!! Open PRs only against the prometheus/prometheus/Makefile.common repository!
17 17
18# Example usage : 18# Example usage :
19# Create the main Makefile in the root project directory. 19# Create the main Makefile in the root project directory.
20# include Makefile.common 20# include Makefile.common
21# customTarget: 21# customTarget:
22# @echo ">> Running customTarget" 22# @echo ">> Running customTarget"
@@ -28,18 +28,53 @@ unexport GOBIN
28GO ?= go 28GO ?= go
29GOFMT ?= $(GO)fmt 29GOFMT ?= $(GO)fmt
30FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) 30FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
31GOOPTS ?=
32
33GO_VERSION ?= $(shell $(GO) version)
34GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
35PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')
36
37unexport GOVENDOR
38ifeq (, $(PRE_GO_111))
39 ifneq (,$(wildcard go.mod))
40 # Enforce Go modules support just in case the directory is inside GOPATH (and for Travis CI).
41 GO111MODULE := on
42
43 ifneq (,$(wildcard vendor))
44 # Always use the local vendor/ directory to satisfy the dependencies.
45 GOOPTS := $(GOOPTS) -mod=vendor
46 endif
47 endif
48else
49 ifneq (,$(wildcard go.mod))
50 ifneq (,$(wildcard vendor))
51$(warning This repository requires Go >= 1.11 because of Go modules)
52$(warning Some recipes may not work as expected as the current Go runtime is '$(GO_VERSION_NUMBER)')
53 endif
54 else
55 # This repository isn't using Go modules (yet).
56 GOVENDOR := $(FIRST_GOPATH)/bin/govendor
57 endif
58
59 unexport GO111MODULE
60endif
31PROMU := $(FIRST_GOPATH)/bin/promu 61PROMU := $(FIRST_GOPATH)/bin/promu
32STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck 62STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
33GOVENDOR := $(FIRST_GOPATH)/bin/govendor
34pkgs = ./... 63pkgs = ./...
35 64
65GO_VERSION ?= $(shell $(GO) version)
66GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION)))
67
68PROMU_VERSION ?= 0.2.0
69PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
70
36PREFIX ?= $(shell pwd) 71PREFIX ?= $(shell pwd)
37BIN_DIR ?= $(shell pwd) 72BIN_DIR ?= $(shell pwd)
38DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) 73DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
39DOCKER_REPO ?= prom 74DOCKER_REPO ?= prom
40 75
41.PHONY: all 76.PHONY: all
42all: style staticcheck unused build test 77all: precheck style staticcheck unused build test
43 78
44# This rule is used to forward a target like "build" to "common-build". This 79# This rule is used to forward a target like "build" to "common-build". This
45# allows a new "build" target to be defined in a Makefile which includes this 80# allows a new "build" target to be defined in a Makefile which includes this
@@ -49,7 +84,12 @@ all: style staticcheck unused build test
49.PHONY: common-style 84.PHONY: common-style
50common-style: 85common-style:
51 @echo ">> checking code style" 86 @echo ">> checking code style"
52 ! $(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print) | grep '^' 87 @fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
88 if [ -n "$${fmtRes}" ]; then \
89 echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
90 echo "Please ensure you are using $$($(GO) version) for formatting code."; \
91 exit 1; \
92 fi
53 93
54.PHONY: common-check_license 94.PHONY: common-check_license
55common-check_license: 95common-check_license:
@@ -65,37 +105,54 @@ common-check_license:
65.PHONY: common-test-short 105.PHONY: common-test-short
66common-test-short: 106common-test-short:
67 @echo ">> running short tests" 107 @echo ">> running short tests"
68 $(GO) test -short $(pkgs) 108 GO111MODULE=$(GO111MODULE) $(GO) test -short $(GOOPTS) $(pkgs)
69 109
70.PHONY: common-test 110.PHONY: common-test
71common-test: 111common-test:
72 @echo ">> running all tests" 112 @echo ">> running all tests"
73 $(GO) test -race $(pkgs) 113 GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs)
74 114
75.PHONY: common-format 115.PHONY: common-format
76common-format: 116common-format:
77 @echo ">> formatting code" 117 @echo ">> formatting code"
78 $(GO) fmt $(pkgs) 118 GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs)
79 119
80.PHONY: common-vet 120.PHONY: common-vet
81common-vet: 121common-vet:
82 @echo ">> vetting code" 122 @echo ">> vetting code"
83 $(GO) vet $(pkgs) 123 GO111MODULE=$(GO111MODULE) $(GO) vet $(GOOPTS) $(pkgs)
84 124
85.PHONY: common-staticcheck 125.PHONY: common-staticcheck
86common-staticcheck: $(STATICCHECK) 126common-staticcheck: $(STATICCHECK)
87 @echo ">> running staticcheck" 127 @echo ">> running staticcheck"
128ifdef GO111MODULE
129 GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs)
130else
88 $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) 131 $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
132endif
89 133
90.PHONY: common-unused 134.PHONY: common-unused
91common-unused: $(GOVENDOR) 135common-unused: $(GOVENDOR)
136ifdef GOVENDOR
92 @echo ">> running check for unused packages" 137 @echo ">> running check for unused packages"
93 @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages' 138 @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages'
139else
140ifdef GO111MODULE
141 @echo ">> running check for unused/missing packages in go.mod"
142 GO111MODULE=$(GO111MODULE) $(GO) mod tidy
143 @git diff --exit-code -- go.sum go.mod
144ifneq (,$(wildcard vendor))
145 @echo ">> running check for unused packages in vendor/"
146 GO111MODULE=$(GO111MODULE) $(GO) mod vendor
147 @git diff --exit-code -- go.sum go.mod vendor/
148endif
149endif
150endif
94 151
95.PHONY: common-build 152.PHONY: common-build
96common-build: promu 153common-build: promu
97 @echo ">> building binaries" 154 @echo ">> building binaries"
98 $(PROMU) build --prefix $(PREFIX) 155 GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX)
99 156
100.PHONY: common-tarball 157.PHONY: common-tarball
101common-tarball: promu 158common-tarball: promu
@@ -115,13 +172,54 @@ common-docker-tag-latest:
115 docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest" 172 docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest"
116 173
117.PHONY: promu 174.PHONY: promu
118promu: 175promu: $(PROMU)
119 GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu 176
177$(PROMU):
178 $(eval PROMU_TMP := $(shell mktemp -d))
179 curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
180 mkdir -p $(FIRST_GOPATH)/bin
181 cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
182 rm -r $(PROMU_TMP)
183
184.PHONY: proto
185proto:
186 @echo ">> generating code from proto files"
187 @./scripts/genproto.sh
120 188
121.PHONY: $(STATICCHECK) 189.PHONY: $(STATICCHECK)
122$(STATICCHECK): 190$(STATICCHECK):
123 GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck 191ifdef GO111MODULE
124 192# Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}.
193# See https://github.com/golang/go/issues/27643.
194# For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules.
195 tmpModule=$$(mktemp -d 2>&1) && \
196 mkdir -p $${tmpModule}/staticcheck && \
197 cd "$${tmpModule}"/staticcheck && \
198 GO111MODULE=on $(GO) mod init example.com/staticcheck && \
199 GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \
200 rm -rf $${tmpModule};
201else
202 GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck
203endif
204
205ifdef GOVENDOR
125.PHONY: $(GOVENDOR) 206.PHONY: $(GOVENDOR)
126$(GOVENDOR): 207$(GOVENDOR):
127 GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor 208 GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor
209endif
210
211.PHONY: precheck
212precheck::
213
214define PRECHECK_COMMAND_template =
215precheck:: $(1)_precheck
216
217
218PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
219.PHONY: $(1)_precheck
220$(1)_precheck:
221 @if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
222 echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
223 exit 1; \
224 fi
225endef