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.

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