|
| 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 | +DOCKER_REPO ?= prom |
| 40 | + |
| 41 | +.PHONY: all |
| 42 | +all: style staticcheck unused build test |
| 43 | + |
| 44 | +# 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 |
| 46 | +# one and override "common-build" without override warnings. |
| 47 | +%: common-% ; |
| 48 | + |
| 49 | +.PHONY: common-style |
| 50 | +common-style: |
| 51 | + @echo ">> checking code style" |
| 52 | + @fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \ |
| 53 | + if [ -n "$${fmtRes}" ]; then \ |
| 54 | + echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \ |
| 55 | + echo "Please ensure you are using $$($(GO) version) for formatting code."; \ |
| 56 | + exit 1; \ |
| 57 | + fi |
| 58 | + |
| 59 | +.PHONY: common-check_license |
| 60 | +common-check_license: |
| 61 | + @echo ">> checking license header" |
| 62 | + @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \ |
| 63 | + awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \ |
| 64 | + done); \ |
| 65 | + if [ -n "$${licRes}" ]; then \ |
| 66 | + echo "license header checking failed:"; echo "$${licRes}"; \ |
| 67 | + exit 1; \ |
| 68 | + fi |
| 69 | + |
| 70 | +.PHONY: common-test-short |
| 71 | +common-test-short: |
| 72 | + @echo ">> running short tests" |
| 73 | + $(GO) test -short $(pkgs) |
| 74 | + |
| 75 | +.PHONY: common-test |
| 76 | +common-test: |
| 77 | + @echo ">> running all tests" |
| 78 | + $(GO) test -race $(pkgs) |
| 79 | + |
| 80 | +.PHONY: common-format |
| 81 | +common-format: |
| 82 | + @echo ">> formatting code" |
| 83 | + $(GO) fmt $(pkgs) |
| 84 | + |
| 85 | +.PHONY: common-vet |
| 86 | +common-vet: |
| 87 | + @echo ">> vetting code" |
| 88 | + $(GO) vet $(pkgs) |
| 89 | + |
| 90 | +.PHONY: common-staticcheck |
| 91 | +common-staticcheck: $(STATICCHECK) |
| 92 | + @echo ">> running staticcheck" |
| 93 | + $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) |
| 94 | + |
| 95 | +.PHONY: common-unused |
| 96 | +common-unused: $(GOVENDOR) |
| 97 | + @echo ">> running check for unused packages" |
| 98 | + @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages' |
| 99 | + |
| 100 | +.PHONY: common-build |
| 101 | +common-build: promu |
| 102 | + @echo ">> building binaries" |
| 103 | + $(PROMU) build --prefix $(PREFIX) |
| 104 | + |
| 105 | +.PHONY: common-tarball |
| 106 | +common-tarball: promu |
| 107 | + @echo ">> building release tarball" |
| 108 | + $(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) |
| 109 | + |
| 110 | +.PHONY: common-docker |
| 111 | +common-docker: |
| 112 | + docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" . |
| 113 | + |
| 114 | +.PHONY: common-docker-publish |
| 115 | +common-docker-publish: |
| 116 | + docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)" |
| 117 | + |
| 118 | +.PHONY: common-docker-tag-latest |
| 119 | +common-docker-tag-latest: |
| 120 | + docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest" |
| 121 | + |
| 122 | +.PHONY: promu |
| 123 | +promu: |
| 124 | + GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu |
| 125 | + |
| 126 | +.PHONY: $(STATICCHECK) |
| 127 | +$(STATICCHECK): |
| 128 | + GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck |
| 129 | + |
| 130 | +.PHONY: $(GOVENDOR) |
| 131 | +$(GOVENDOR): |
| 132 | + GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor |
0 commit comments