diff options
author | Ben Kochie <superq@gmail.com> | 2018-11-30 14:01:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-30 14:01:20 +0100 |
commit | becca1275c77ffab31ec7082b3bd9ba9382a6c1a (patch) | |
tree | 3c201ab4743d461ecdb9ccd39928c3a9dd2a4948 /Makefile.common | |
parent | 1732478361a57dea9f7cd0f319c5a34cebcf047d (diff) | |
download | prometheus_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.common | 124 |
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 | |||
28 | GO ?= go | 28 | GO ?= go |
29 | GOFMT ?= $(GO)fmt | 29 | GOFMT ?= $(GO)fmt |
30 | FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) | 30 | FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) |
31 | GOOPTS ?= | ||
32 | |||
33 | GO_VERSION ?= $(shell $(GO) version) | ||
34 | GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION)) | ||
35 | PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.') | ||
36 | |||
37 | unexport GOVENDOR | ||
38 | ifeq (, $(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 | ||
48 | else | ||
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 | ||
60 | endif | ||
31 | PROMU := $(FIRST_GOPATH)/bin/promu | 61 | PROMU := $(FIRST_GOPATH)/bin/promu |
32 | STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck | 62 | STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck |
33 | GOVENDOR := $(FIRST_GOPATH)/bin/govendor | ||
34 | pkgs = ./... | 63 | pkgs = ./... |
35 | 64 | ||
65 | GO_VERSION ?= $(shell $(GO) version) | ||
66 | GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION))) | ||
67 | |||
68 | PROMU_VERSION ?= 0.2.0 | ||
69 | PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz | ||
70 | |||
36 | PREFIX ?= $(shell pwd) | 71 | PREFIX ?= $(shell pwd) |
37 | BIN_DIR ?= $(shell pwd) | 72 | BIN_DIR ?= $(shell pwd) |
38 | DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) | 73 | DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) |
39 | DOCKER_REPO ?= prom | 74 | DOCKER_REPO ?= prom |
40 | 75 | ||
41 | .PHONY: all | 76 | .PHONY: all |
42 | all: style staticcheck unused build test | 77 | all: 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 |
50 | common-style: | 85 | common-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 |
55 | common-check_license: | 95 | common-check_license: |
@@ -65,37 +105,54 @@ common-check_license: | |||
65 | .PHONY: common-test-short | 105 | .PHONY: common-test-short |
66 | common-test-short: | 106 | common-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 |
71 | common-test: | 111 | common-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 |
76 | common-format: | 116 | common-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 |
81 | common-vet: | 121 | common-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 |
86 | common-staticcheck: $(STATICCHECK) | 126 | common-staticcheck: $(STATICCHECK) |
87 | @echo ">> running staticcheck" | 127 | @echo ">> running staticcheck" |
128 | ifdef GO111MODULE | ||
129 | GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs) | ||
130 | else | ||
88 | $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) | 131 | $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) |
132 | endif | ||
89 | 133 | ||
90 | .PHONY: common-unused | 134 | .PHONY: common-unused |
91 | common-unused: $(GOVENDOR) | 135 | common-unused: $(GOVENDOR) |
136 | ifdef 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' |
139 | else | ||
140 | ifdef 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 | ||
144 | ifneq (,$(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/ | ||
148 | endif | ||
149 | endif | ||
150 | endif | ||
94 | 151 | ||
95 | .PHONY: common-build | 152 | .PHONY: common-build |
96 | common-build: promu | 153 | common-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 |
101 | common-tarball: promu | 158 | common-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 |
118 | promu: | 175 | promu: $(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 | ||
185 | proto: | ||
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 | 191 | ifdef 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}; | ||
201 | else | ||
202 | GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck | ||
203 | endif | ||
204 | |||
205 | ifdef 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 |
209 | endif | ||
210 | |||
211 | .PHONY: precheck | ||
212 | precheck:: | ||
213 | |||
214 | define PRECHECK_COMMAND_template = | ||
215 | precheck:: $(1)_precheck | ||
216 | |||
217 | |||
218 | PRECHECK_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 | ||
225 | endef | ||