kesavand | 2cde658 | 2020-06-22 04:56:23 -0400 | [diff] [blame^] | 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 | pkgs = ./... |
| 64 | |
| 65 | ifeq (arm, $(GOHOSTARCH)) |
| 66 | GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM) |
| 67 | GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM) |
| 68 | else |
| 69 | GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH) |
| 70 | endif |
| 71 | |
| 72 | PROMU_VERSION ?= 0.4.0 |
| 73 | PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz |
| 74 | |
| 75 | GOLANGCI_LINT := |
| 76 | GOLANGCI_LINT_OPTS ?= |
| 77 | GOLANGCI_LINT_VERSION ?= v1.16.0 |
| 78 | # golangci-lint only supports linux, darwin and windows platforms on i386/amd64. |
| 79 | # windows isn't included here because of the path separator being different. |
| 80 | ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin)) |
| 81 | ifeq ($(GOHOSTARCH),$(filter $(GOHOSTARCH),amd64 i386)) |
| 82 | GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint |
| 83 | endif |
| 84 | endif |
| 85 | |
| 86 | PREFIX ?= $(shell pwd) |
| 87 | BIN_DIR ?= $(shell pwd) |
| 88 | DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) |
| 89 | DOCKERFILE_PATH ?= ./ |
| 90 | DOCKER_REPO ?= prom |
| 91 | |
| 92 | DOCKER_ARCHS ?= amd64 |
| 93 | |
| 94 | BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS)) |
| 95 | PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS)) |
| 96 | TAG_DOCKER_ARCHS = $(addprefix common-docker-tag-latest-,$(DOCKER_ARCHS)) |
| 97 | |
| 98 | ifeq ($(GOHOSTARCH),amd64) |
| 99 | ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows)) |
| 100 | # Only supported on amd64 |
| 101 | test-flags := -race |
| 102 | endif |
| 103 | endif |
| 104 | |
| 105 | # This rule is used to forward a target like "build" to "common-build". This |
| 106 | # allows a new "build" target to be defined in a Makefile which includes this |
| 107 | # one and override "common-build" without override warnings. |
| 108 | %: common-% ; |
| 109 | |
| 110 | .PHONY: common-all |
| 111 | common-all: precheck style check_license lint unused build test |
| 112 | |
| 113 | .PHONY: common-style |
| 114 | common-style: |
| 115 | @echo ">> checking code style" |
| 116 | @fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \ |
| 117 | if [ -n "$${fmtRes}" ]; then \ |
| 118 | echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \ |
| 119 | echo "Please ensure you are using $$($(GO) version) for formatting code."; \ |
| 120 | exit 1; \ |
| 121 | fi |
| 122 | |
| 123 | .PHONY: common-check_license |
| 124 | common-check_license: |
| 125 | @echo ">> checking license header" |
| 126 | @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \ |
| 127 | awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \ |
| 128 | done); \ |
| 129 | if [ -n "$${licRes}" ]; then \ |
| 130 | echo "license header checking failed:"; echo "$${licRes}"; \ |
| 131 | exit 1; \ |
| 132 | fi |
| 133 | |
| 134 | .PHONY: common-deps |
| 135 | common-deps: |
| 136 | @echo ">> getting dependencies" |
| 137 | ifdef GO111MODULE |
| 138 | GO111MODULE=$(GO111MODULE) $(GO) mod download |
| 139 | else |
| 140 | $(GO) get $(GOOPTS) -t ./... |
| 141 | endif |
| 142 | |
| 143 | .PHONY: common-test-short |
| 144 | common-test-short: |
| 145 | @echo ">> running short tests" |
| 146 | GO111MODULE=$(GO111MODULE) $(GO) test -short $(GOOPTS) $(pkgs) |
| 147 | |
| 148 | .PHONY: common-test |
| 149 | common-test: |
| 150 | @echo ">> running all tests" |
| 151 | GO111MODULE=$(GO111MODULE) $(GO) test $(test-flags) $(GOOPTS) $(pkgs) |
| 152 | |
| 153 | .PHONY: common-format |
| 154 | common-format: |
| 155 | @echo ">> formatting code" |
| 156 | GO111MODULE=$(GO111MODULE) $(GO) fmt $(pkgs) |
| 157 | |
| 158 | .PHONY: common-vet |
| 159 | common-vet: |
| 160 | @echo ">> vetting code" |
| 161 | GO111MODULE=$(GO111MODULE) $(GO) vet $(GOOPTS) $(pkgs) |
| 162 | |
| 163 | .PHONY: common-lint |
| 164 | common-lint: $(GOLANGCI_LINT) |
| 165 | ifdef GOLANGCI_LINT |
| 166 | @echo ">> running golangci-lint" |
| 167 | ifdef GO111MODULE |
| 168 | # 'go list' needs to be executed before staticcheck to prepopulate the modules cache. |
| 169 | # Otherwise staticcheck might fail randomly for some reason not yet explained. |
| 170 | GO111MODULE=$(GO111MODULE) $(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null |
| 171 | GO111MODULE=$(GO111MODULE) $(GOLANGCI_LINT) run $(GOLANGCI_LINT_OPTS) $(pkgs) |
| 172 | else |
| 173 | $(GOLANGCI_LINT) run $(pkgs) |
| 174 | endif |
| 175 | endif |
| 176 | |
| 177 | # For backward-compatibility. |
| 178 | .PHONY: common-staticcheck |
| 179 | common-staticcheck: lint |
| 180 | |
| 181 | .PHONY: common-unused |
| 182 | common-unused: $(GOVENDOR) |
| 183 | ifdef GOVENDOR |
| 184 | @echo ">> running check for unused packages" |
| 185 | @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages' |
| 186 | else |
| 187 | ifdef GO111MODULE |
| 188 | @echo ">> running check for unused/missing packages in go.mod" |
| 189 | GO111MODULE=$(GO111MODULE) $(GO) mod tidy |
| 190 | ifeq (,$(wildcard vendor)) |
| 191 | @git diff --exit-code -- go.sum go.mod |
| 192 | else |
| 193 | @echo ">> running check for unused packages in vendor/" |
| 194 | GO111MODULE=$(GO111MODULE) $(GO) mod vendor |
| 195 | @git diff --exit-code -- go.sum go.mod vendor/ |
| 196 | endif |
| 197 | endif |
| 198 | endif |
| 199 | |
| 200 | .PHONY: common-build |
| 201 | common-build: promu |
| 202 | @echo ">> building binaries" |
| 203 | GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX) |
| 204 | |
| 205 | .PHONY: common-tarball |
| 206 | common-tarball: promu |
| 207 | @echo ">> building release tarball" |
| 208 | $(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) |
| 209 | |
| 210 | .PHONY: common-docker $(BUILD_DOCKER_ARCHS) |
| 211 | common-docker: $(BUILD_DOCKER_ARCHS) |
| 212 | $(BUILD_DOCKER_ARCHS): common-docker-%: |
| 213 | docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" \ |
| 214 | --build-arg ARCH="$*" \ |
| 215 | --build-arg OS="linux" \ |
| 216 | $(DOCKERFILE_PATH) |
| 217 | |
| 218 | .PHONY: common-docker-publish $(PUBLISH_DOCKER_ARCHS) |
| 219 | common-docker-publish: $(PUBLISH_DOCKER_ARCHS) |
| 220 | $(PUBLISH_DOCKER_ARCHS): common-docker-publish-%: |
| 221 | docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" |
| 222 | |
| 223 | .PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS) |
| 224 | common-docker-tag-latest: $(TAG_DOCKER_ARCHS) |
| 225 | $(TAG_DOCKER_ARCHS): common-docker-tag-latest-%: |
| 226 | docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest" |
| 227 | |
| 228 | .PHONY: common-docker-manifest |
| 229 | common-docker-manifest: |
| 230 | DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create -a "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" $(foreach ARCH,$(DOCKER_ARCHS),$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$(ARCH):$(DOCKER_IMAGE_TAG)) |
| 231 | DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" |
| 232 | |
| 233 | .PHONY: promu |
| 234 | promu: $(PROMU) |
| 235 | |
| 236 | $(PROMU): |
| 237 | $(eval PROMU_TMP := $(shell mktemp -d)) |
| 238 | curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP) |
| 239 | mkdir -p $(FIRST_GOPATH)/bin |
| 240 | cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu |
| 241 | rm -r $(PROMU_TMP) |
| 242 | |
| 243 | .PHONY: proto |
| 244 | proto: |
| 245 | @echo ">> generating code from proto files" |
| 246 | @./scripts/genproto.sh |
| 247 | |
| 248 | ifdef GOLANGCI_LINT |
| 249 | $(GOLANGCI_LINT): |
| 250 | mkdir -p $(FIRST_GOPATH)/bin |
| 251 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_LINT_VERSION)/install.sh \ |
| 252 | | sed -e '/install -d/d' \ |
| 253 | | sh -s -- -b $(FIRST_GOPATH)/bin $(GOLANGCI_LINT_VERSION) |
| 254 | endif |
| 255 | |
| 256 | ifdef GOVENDOR |
| 257 | .PHONY: $(GOVENDOR) |
| 258 | $(GOVENDOR): |
| 259 | GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor |
| 260 | endif |
| 261 | |
| 262 | .PHONY: precheck |
| 263 | precheck:: |
| 264 | |
| 265 | define PRECHECK_COMMAND_template = |
| 266 | precheck:: $(1)_precheck |
| 267 | |
| 268 | PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1))) |
| 269 | .PHONY: $(1)_precheck |
| 270 | $(1)_precheck: |
| 271 | @if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \ |
| 272 | echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \ |
| 273 | exit 1; \ |
| 274 | fi |
| 275 | endef |