Makefile.common 8.7 KB

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