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.
 
 

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