diff options
-rw-r--r-- | Makefile | 48 | ||||
-rw-r--r-- | Makefile.common | 100 |
2 files changed, 106 insertions, 42 deletions
@@ -11,20 +11,15 @@ | |||
11 | # See the License for the specific language governing permissions and | 11 | # See the License for the specific language governing permissions and |
12 | # limitations under the License. | 12 | # limitations under the License. |
13 | 13 | ||
14 | include Makefile.common | ||
15 | |||
14 | GO ?= GO15VENDOREXPERIMENT=1 go | 16 | GO ?= GO15VENDOREXPERIMENT=1 go |
15 | GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) | ||
16 | GOARCH := $(shell $(GO) env GOARCH) | 17 | GOARCH := $(shell $(GO) env GOARCH) |
17 | GOHOSTARCH := $(shell $(GO) env GOHOSTARCH) | 18 | GOHOSTARCH := $(shell $(GO) env GOHOSTARCH) |
18 | 19 | ||
19 | PROMTOOL ?= $(GOPATH)/bin/promtool | 20 | PROMTOOL ?= $(FIRST_GOPATH)/bin/promtool |
20 | PROMU ?= $(GOPATH)/bin/promu | ||
21 | STATICCHECK ?= $(GOPATH)/bin/staticcheck | ||
22 | pkgs = $(shell $(GO) list ./... | grep -v /vendor/) | ||
23 | 21 | ||
24 | PREFIX ?= $(shell pwd) | ||
25 | BIN_DIR ?= $(shell pwd) | ||
26 | DOCKER_IMAGE_NAME ?= node-exporter | 22 | DOCKER_IMAGE_NAME ?= node-exporter |
27 | DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) | ||
28 | MACH ?= $(shell uname -m) | 23 | MACH ?= $(shell uname -m) |
29 | DOCKERFILE ?= Dockerfile | 24 | DOCKERFILE ?= Dockerfile |
30 | 25 | ||
@@ -75,10 +70,6 @@ $(eval $(call goarch_pair,mips64el,mipsel)) | |||
75 | 70 | ||
76 | all: style vet staticcheck checkmetrics build test $(cross-test) $(test-e2e) | 71 | all: style vet staticcheck checkmetrics build test $(cross-test) $(test-e2e) |
77 | 72 | ||
78 | style: | ||
79 | @echo ">> checking code style" | ||
80 | @! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^' | ||
81 | |||
82 | test: collector/fixtures/sys/.unpacked | 73 | test: collector/fixtures/sys/.unpacked |
83 | @echo ">> running tests" | 74 | @echo ">> running tests" |
84 | $(GO) test -short $(test-flags) $(pkgs) | 75 | $(GO) test -short $(test-flags) $(pkgs) |
@@ -107,26 +98,6 @@ checkmetrics: $(PROMTOOL) | |||
107 | @echo ">> checking metrics for correctness" | 98 | @echo ">> checking metrics for correctness" |
108 | ./checkmetrics.sh $(PROMTOOL) $(e2e-out) | 99 | ./checkmetrics.sh $(PROMTOOL) $(e2e-out) |
109 | 100 | ||
110 | format: | ||
111 | @echo ">> formatting code" | ||
112 | @$(GO) fmt $(pkgs) | ||
113 | |||
114 | vet: | ||
115 | @echo ">> vetting code" | ||
116 | @$(GO) vet $(pkgs) | ||
117 | |||
118 | staticcheck: $(STATICCHECK) | ||
119 | @echo ">> running staticcheck" | ||
120 | @$(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) | ||
121 | |||
122 | build: $(PROMU) | ||
123 | @echo ">> building binaries" | ||
124 | @$(PROMU) build --prefix $(PREFIX) | ||
125 | |||
126 | tarball: $(PROMU) | ||
127 | @echo ">> building release tarball" | ||
128 | @$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) | ||
129 | |||
130 | docker: | 101 | docker: |
131 | ifeq ($(MACH), ppc64le) | 102 | ifeq ($(MACH), ppc64le) |
132 | $(eval DOCKERFILE=Dockerfile.ppc64le) | 103 | $(eval DOCKERFILE=Dockerfile.ppc64le) |
@@ -138,20 +109,13 @@ test-docker: | |||
138 | @echo ">> testing docker image" | 109 | @echo ">> testing docker image" |
139 | ./test_image.sh "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" 9100 | 110 | ./test_image.sh "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" 9100 |
140 | 111 | ||
141 | $(GOPATH)/bin/promtool promtool: | 112 | $(FIRST_GOPATH)/bin/promtool promtool: |
142 | @GOOS= GOARCH= $(GO) get -u github.com/prometheus/prometheus/cmd/promtool | 113 | @GOOS= GOARCH= $(GO) get -u github.com/prometheus/prometheus/cmd/promtool |
143 | 114 | ||
144 | $(GOPATH)/bin/promu promu: | 115 | .PHONY: test-e2e promtool checkmetrics |
145 | @GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu | ||
146 | |||
147 | $(GOPATH)/bin/staticcheck: | ||
148 | @GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck | ||
149 | |||
150 | |||
151 | .PHONY: all style format build test test-e2e vet tarball docker promtool promu staticcheck checkmetrics | ||
152 | 116 | ||
153 | # Declaring the binaries at their default locations as PHONY targets is a hack | 117 | # Declaring the binaries at their default locations as PHONY targets is a hack |
154 | # to ensure the latest version is downloaded on every make execution. | 118 | # to ensure the latest version is downloaded on every make execution. |
155 | # If this is not desired, copy/symlink these binaries to a different path and | 119 | # If this is not desired, copy/symlink these binaries to a different path and |
156 | # set the respective environment variables. | 120 | # set the respective environment variables. |
157 | .PHONY: $(GOPATH)/bin/promtool $(GOPATH)/bin/promu $(GOPATH)/bin/staticcheck | 121 | .PHONY: $(FIRST_GOPATH)/bin/promtool |
diff --git a/Makefile.common b/Makefile.common new file mode 100644 index 0000000..353494c --- /dev/null +++ b/Makefile.common | |||
@@ -0,0 +1,100 @@ | |||
1 | # Copyright 2018 The Prometheus Authors | ||
2 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
3 | # you may not use this file except in compliance with the License. | ||
4 | # You may obtain a copy of the License at | ||
5 | # | ||
6 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
7 | # | ||
8 | # Unless required by applicable law or agreed to in writing, software | ||
9 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
11 | # See the License for the specific language governing permissions and | ||
12 | # limitations under the License. | ||
13 | |||
14 | |||
15 | # A common Makefile that includes rules to be reused in different prometheus projects. | ||
16 | # !!! Open PRs only against the prometheus/prometheus/Makefile.common repository! | ||
17 | |||
18 | # Example usage : | ||
19 | # Create the main Makefile in the root project directory. | ||
20 | # include Makefile.common | ||
21 | # customTarget: | ||
22 | # @echo ">> Running customTarget" | ||
23 | # | ||
24 | |||
25 | # Ensure GOBIN is not set during build so that promu is installed to the correct path | ||
26 | unexport GOBIN | ||
27 | |||
28 | GO ?= go | ||
29 | GOFMT ?= $(GO)fmt | ||
30 | FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) | ||
31 | PROMU := $(FIRST_GOPATH)/bin/promu | ||
32 | STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck | ||
33 | GOVENDOR := $(FIRST_GOPATH)/bin/govendor | ||
34 | pkgs = ./... | ||
35 | |||
36 | PREFIX ?= $(shell pwd) | ||
37 | BIN_DIR ?= $(shell pwd) | ||
38 | DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) | ||
39 | |||
40 | all: style staticcheck unused build test | ||
41 | |||
42 | style: | ||
43 | @echo ">> checking code style" | ||
44 | ! $(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print) | grep '^' | ||
45 | |||
46 | check_license: | ||
47 | @echo ">> checking license header" | ||
48 | @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \ | ||
49 | awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \ | ||
50 | done); \ | ||
51 | if [ -n "$${licRes}" ]; then \ | ||
52 | echo "license header checking failed:"; echo "$${licRes}"; \ | ||
53 | exit 1; \ | ||
54 | fi | ||
55 | |||
56 | test-short: | ||
57 | @echo ">> running short tests" | ||
58 | $(GO) test -short $(pkgs) | ||
59 | |||
60 | test: | ||
61 | @echo ">> running all tests" | ||
62 | $(GO) test -race $(pkgs) | ||
63 | |||
64 | format: | ||
65 | @echo ">> formatting code" | ||
66 | $(GO) fmt $(pkgs) | ||
67 | |||
68 | vet: | ||
69 | @echo ">> vetting code" | ||
70 | $(GO) vet $(pkgs) | ||
71 | |||
72 | staticcheck: $(STATICCHECK) | ||
73 | @echo ">> running staticcheck" | ||
74 | $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) | ||
75 | |||
76 | unused: $(GOVENDOR) | ||
77 | @echo ">> running check for unused packages" | ||
78 | @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages' | ||
79 | |||
80 | build: promu | ||
81 | @echo ">> building binaries" | ||
82 | $(PROMU) build --prefix $(PREFIX) | ||
83 | |||
84 | tarball: promu | ||
85 | @echo ">> building release tarball" | ||
86 | $(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) | ||
87 | |||
88 | docker: | ||
89 | docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" . | ||
90 | |||
91 | promu: | ||
92 | GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu | ||
93 | |||
94 | $(FIRST_GOPATH)/bin/staticcheck: | ||
95 | GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck | ||
96 | |||
97 | $(FIRST_GOPATH)/bin/govendor: | ||
98 | GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor | ||
99 | |||
100 | .PHONY: all style check_license format build test vet assets tarball docker promu staticcheck $(FIRST_GOPATH)/bin/staticcheck govendor $(FIRST_GOPATH)/bin/govendor \ No newline at end of file | ||