You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

268 lines
8.8 KiB

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