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