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