本站源代码
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.

276 lines
8.9KB

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