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