|
| 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 | +GOOPTS ?= |
| 32 | +GOHOSTOS ?= $(shell $(GO) env GOHOSTOS) |
| 33 | +GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH) |
| 34 | + |
| 35 | +GO_VERSION ?= $(shell $(GO) version) |
| 36 | +GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION)) |
| 37 | +PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.') |
| 38 | + |
| 39 | +GOVENDOR := |
| 40 | +GO111MODULE := |
| 41 | +ifeq (, $(PRE_GO_111)) |
| 42 | + ifneq (,$(wildcard go.mod)) |
| 43 | + # Enforce Go modules support just in case the directory is inside GOPATH (and for Travis CI). |
| 44 | + GO111MODULE := on |
| 45 | + |
| 46 | + ifneq (,$(wildcard vendor)) |
| 47 | + # Always use the local vendor/ directory to satisfy the dependencies. |
| 48 | + GOOPTS := $(GOOPTS) -mod=vendor |
| 49 | + endif |
| 50 | + endif |
| 51 | +else |
| 52 | + ifneq (,$(wildcard go.mod)) |
| 53 | + ifneq (,$(wildcard vendor)) |
| 54 | +$(warning This repository requires Go >= 1.11 because of Go modules) |
| 55 | +$(warning Some recipes may not work as expected as the current Go runtime is '$(GO_VERSION_NUMBER)') |
| 56 | + endif |
| 57 | + else |
| 58 | + # This repository isn't using Go modules (yet). |
| 59 | + GOVENDOR := $(FIRST_GOPATH)/bin/govendor |
| 60 | + endif |
| 61 | +endif |
| 62 | +PROMU := $(FIRST_GOPATH)/bin/promu |
| 63 | +STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck |
| 64 | +pkgs = ./... |
| 65 | + |
| 66 | +ifeq (arm, $(GOHOSTARCH)) |
| 67 | + GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM) |
| 68 | + GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM) |
| 69 | +else |
| 70 | + GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH) |
| 71 | +endif |
| 72 | + |
| 73 | +PROMU_VERSION ?= 0.2.0 |
| 74 | +PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz |
| 75 | +STATICCHECK_VERSION ?= 2019.1 |
| 76 | +STATICCHECK_URL := https://github.com/dominikh/go-tools/releases/download/$(STATICCHECK_VERSION)/staticcheck_$(GOHOSTOS)_$(GOHOSTARCH) |
| 77 | + |
| 78 | +PREFIX ?= $(shell pwd) |
| 79 | +BIN_DIR ?= $(shell pwd) |
| 80 | +DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) |
| 81 | +DOCKER_REPO ?= prom |
| 82 | + |
| 83 | +ifeq ($(GOHOSTARCH),amd64) |
| 84 | + ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows)) |
| 85 | + # Only supported on amd64 |
| 86 | + test-flags := -race |
| 87 | + endif |
| 88 | +endif |
| 89 | + |
| 90 | +.PHONY: all |
| 91 | +all: precheck style staticcheck unused build test |
| 92 | + |
| 93 | +# This rule is used to forward a target like "build" to "common-build". This |
| 94 | +# allows a new "build" target to be defined in a Makefile which includes this |
| 95 | +# one and override "common-build" without override warnings. |
| 96 | +%: common-% ; |
| 97 | + |
| 98 | +.PHONY: common-style |
| 99 | +common-style: |
| 100 | + @echo ">> checking code style" |
| 101 | + @fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \ |
| 102 | + if [ -n "$${fmtRes}" ]; then \ |
| 103 | + echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \ |
| 104 | + echo "Please ensure you are using $$($(GO) version) for formatting code."; \ |
| 105 | + exit 1; \ |
| 106 | + fi |
| 107 | + |
| 108 | +.PHONY: common-check_license |
| 109 | +common-check_license: |
| 110 | + @echo ">> checking license header" |
| 111 | + @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \ |
| 112 | + awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \ |
| 113 | + done); \ |
| 114 | + if [ -n "$${licRes}" ]; then \ |
| 115 | + echo "license header checking failed:"; echo "$${licRes}"; \ |
| 116 | + exit 1; \ |
| 117 | + fi |
| 118 | + |
| 119 | +.PHONY: common-test-short |
| 120 | +common-test-short: |
| 121 | + @echo ">> running short tests" |
| 122 | + GO111MODULE=$(GO111MODULE) $(GO) test -short $(GOOPTS) $(pkgs) |
| 123 | + |
| 124 | +.PHONY: common-test |
| 125 | +common-test: |
| 126 | + @echo ">> running all tests" |
| 127 | + GO111MODULE=$(GO111MODULE) $(GO) test $(test-flags) $(GOOPTS) $(pkgs) |
| 128 | + |
| 129 | +.PHONY: common-format |
| 130 | +common-format: |
| 131 | + @echo ">> formatting code" |
| 132 | + GO111MODULE=$(GO111MODULE) $(GO) fmt $(pkgs) |
| 133 | + |
| 134 | +.PHONY: common-vet |
| 135 | +common-vet: |
| 136 | + @echo ">> vetting code" |
| 137 | + GO111MODULE=$(GO111MODULE) $(GO) vet $(GOOPTS) $(pkgs) |
| 138 | + |
| 139 | +.PHONY: common-staticcheck |
| 140 | +common-staticcheck: $(STATICCHECK) |
| 141 | + @echo ">> running staticcheck" |
| 142 | + chmod +x $(STATICCHECK) |
| 143 | +ifdef GO111MODULE |
| 144 | +# 'go list' needs to be executed before staticcheck to prepopulate the modules cache. |
| 145 | +# Otherwise staticcheck might fail randomly for some reason not yet explained. |
| 146 | + GO111MODULE=$(GO111MODULE) $(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null |
| 147 | + GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) |
| 148 | +else |
| 149 | + $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) |
| 150 | +endif |
| 151 | + |
| 152 | +.PHONY: common-unused |
| 153 | +common-unused: $(GOVENDOR) |
| 154 | +ifdef GOVENDOR |
| 155 | + @echo ">> running check for unused packages" |
| 156 | + @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages' |
| 157 | +else |
| 158 | +ifdef GO111MODULE |
| 159 | + @echo ">> running check for unused/missing packages in go.mod" |
| 160 | + GO111MODULE=$(GO111MODULE) $(GO) mod tidy |
| 161 | +ifeq (,$(wildcard vendor)) |
| 162 | + @git diff --exit-code -- go.sum go.mod |
| 163 | +else |
| 164 | + @echo ">> running check for unused packages in vendor/" |
| 165 | + GO111MODULE=$(GO111MODULE) $(GO) mod vendor |
| 166 | + @git diff --exit-code -- go.sum go.mod vendor/ |
| 167 | +endif |
| 168 | +endif |
| 169 | +endif |
| 170 | + |
| 171 | +.PHONY: common-build |
| 172 | +common-build: promu |
| 173 | + @echo ">> building binaries" |
| 174 | + GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX) |
| 175 | + |
| 176 | +.PHONY: common-tarball |
| 177 | +common-tarball: promu |
| 178 | + @echo ">> building release tarball" |
| 179 | + $(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) |
| 180 | + |
| 181 | +.PHONY: common-docker |
| 182 | +common-docker: |
| 183 | + docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" . |
| 184 | + |
| 185 | +.PHONY: common-docker-publish |
| 186 | +common-docker-publish: |
| 187 | + docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)" |
| 188 | + |
| 189 | +.PHONY: common-docker-tag-latest |
| 190 | +common-docker-tag-latest: |
| 191 | + docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest" |
| 192 | + |
| 193 | +.PHONY: promu |
| 194 | +promu: $(PROMU) |
| 195 | + |
| 196 | +$(PROMU): |
| 197 | + $(eval PROMU_TMP := $(shell mktemp -d)) |
| 198 | + curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP) |
| 199 | + mkdir -p $(FIRST_GOPATH)/bin |
| 200 | + cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu |
| 201 | + rm -r $(PROMU_TMP) |
| 202 | + |
| 203 | +.PHONY: proto |
| 204 | +proto: |
| 205 | + @echo ">> generating code from proto files" |
| 206 | + @./scripts/genproto.sh |
| 207 | + |
| 208 | +$(STATICCHECK): |
| 209 | + mkdir -p $(FIRST_GOPATH)/bin |
| 210 | + curl -s -L $(STATICCHECK_URL) > $(STATICCHECK) |
| 211 | + |
| 212 | +ifdef GOVENDOR |
| 213 | +.PHONY: $(GOVENDOR) |
| 214 | +$(GOVENDOR): |
| 215 | + GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor |
| 216 | +endif |
| 217 | + |
| 218 | +.PHONY: precheck |
| 219 | +precheck:: |
| 220 | + |
| 221 | +define PRECHECK_COMMAND_template = |
| 222 | +precheck:: $(1)_precheck |
| 223 | + |
| 224 | + |
| 225 | +PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1))) |
| 226 | +.PHONY: $(1)_precheck |
| 227 | +$(1)_precheck: |
| 228 | + @if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \ |
| 229 | + echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \ |
| 230 | + exit 1; \ |
| 231 | + fi |
| 232 | +endef |
0 commit comments