Makefile.common 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. GO_VERSION ?= $(shell $(GO) version)
  28. GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
  29. PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')
  30. unexport GOVENDOR
  31. ifeq (, $(PRE_GO_111))
  32. ifneq (,$(wildcard go.mod))
  33. # Enforce Go modules support just in case the directory is inside GOPATH (and for Travis CI).
  34. GO111MODULE := on
  35. ifneq (,$(wildcard vendor))
  36. # Always use the local vendor/ directory to satisfy the dependencies.
  37. GOOPTS := $(GOOPTS) -mod=vendor
  38. endif
  39. endif
  40. else
  41. ifneq (,$(wildcard go.mod))
  42. ifneq (,$(wildcard vendor))
  43. $(warning This repository requires Go >= 1.11 because of Go modules)
  44. $(warning Some recipes may not work as expected as the current Go runtime is '$(GO_VERSION_NUMBER)')
  45. endif
  46. else
  47. # This repository isn't using Go modules (yet).
  48. GOVENDOR := $(FIRST_GOPATH)/bin/govendor
  49. endif
  50. unexport GO111MODULE
  51. endif
  52. PROMU := $(FIRST_GOPATH)/bin/promu
  53. STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
  54. pkgs = ./...
  55. GO_VERSION ?= $(shell $(GO) version)
  56. GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION)))
  57. PROMU_VERSION ?= 0.2.0
  58. PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
  59. PREFIX ?= $(shell pwd)
  60. BIN_DIR ?= $(shell pwd)
  61. DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
  62. DOCKER_REPO ?= prom
  63. .PHONY: all
  64. all: precheck style staticcheck unused build test
  65. # This rule is used to forward a target like "build" to "common-build". This
  66. # allows a new "build" target to be defined in a Makefile which includes this
  67. # one and override "common-build" without override warnings.
  68. %: common-% ;
  69. .PHONY: common-style
  70. common-style:
  71. @echo ">> checking code style"
  72. @fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
  73. if [ -n "$${fmtRes}" ]; then \
  74. echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
  75. echo "Please ensure you are using $$($(GO) version) for formatting code."; \
  76. exit 1; \
  77. fi
  78. .PHONY: common-check_license
  79. common-check_license:
  80. @echo ">> checking license header"
  81. @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \
  82. awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \
  83. done); \
  84. if [ -n "$${licRes}" ]; then \
  85. echo "license header checking failed:"; echo "$${licRes}"; \
  86. exit 1; \
  87. fi
  88. .PHONY: common-test-short
  89. common-test-short:
  90. @echo ">> running short tests"
  91. GO111MODULE=$(GO111MODULE) $(GO) test -short $(GOOPTS) $(pkgs)
  92. .PHONY: common-test
  93. common-test:
  94. @echo ">> running all tests"
  95. GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs)
  96. .PHONY: common-format
  97. common-format:
  98. @echo ">> formatting code"
  99. GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs)
  100. .PHONY: common-vet
  101. common-vet:
  102. @echo ">> vetting code"
  103. GO111MODULE=$(GO111MODULE) $(GO) vet $(GOOPTS) $(pkgs)
  104. .PHONY: common-staticcheck
  105. common-staticcheck: $(STATICCHECK)
  106. @echo ">> running staticcheck"
  107. ifdef GO111MODULE
  108. GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs)
  109. else
  110. $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
  111. endif
  112. .PHONY: common-unused
  113. common-unused: $(GOVENDOR)
  114. ifdef GOVENDOR
  115. @echo ">> running check for unused packages"
  116. @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages'
  117. else
  118. ifdef GO111MODULE
  119. @echo ">> running check for unused/missing packages in go.mod"
  120. GO111MODULE=$(GO111MODULE) $(GO) mod tidy
  121. @git diff --exit-code -- go.sum go.mod
  122. ifneq (,$(wildcard vendor))
  123. @echo ">> running check for unused packages in vendor/"
  124. GO111MODULE=$(GO111MODULE) $(GO) mod vendor
  125. @git diff --exit-code -- go.sum go.mod vendor/
  126. endif
  127. endif
  128. endif
  129. .PHONY: common-build
  130. common-build: promu
  131. @echo ">> building binaries"
  132. GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX)
  133. .PHONY: common-tarball
  134. common-tarball: promu
  135. @echo ">> building release tarball"
  136. $(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)
  137. .PHONY: common-docker
  138. common-docker:
  139. docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .
  140. .PHONY: common-docker-publish
  141. common-docker-publish:
  142. docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)"
  143. .PHONY: common-docker-tag-latest
  144. common-docker-tag-latest:
  145. docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest"
  146. .PHONY: promu
  147. promu: $(PROMU)
  148. $(PROMU):
  149. curl -s -L $(PROMU_URL) | tar -xvz -C /tmp
  150. mkdir -v -p $(FIRST_GOPATH)/bin
  151. cp -v /tmp/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(PROMU)
  152. .PHONY: proto
  153. proto:
  154. @echo ">> generating code from proto files"
  155. @./scripts/genproto.sh
  156. .PHONY: $(STATICCHECK)
  157. $(STATICCHECK):
  158. ifdef GO111MODULE
  159. # Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}.
  160. # See https://github.com/golang/go/issues/27643.
  161. # For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules.
  162. tmpModule=$$(mktemp -d 2>&1) && \
  163. mkdir -p $${tmpModule}/staticcheck && \
  164. cd "$${tmpModule}"/staticcheck && \
  165. GO111MODULE=on $(GO) mod init example.com/staticcheck && \
  166. GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \
  167. rm -rf $${tmpModule};
  168. else
  169. GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck
  170. endif
  171. ifdef GOVENDOR
  172. .PHONY: $(GOVENDOR)
  173. $(GOVENDOR):
  174. GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor
  175. endif
  176. .PHONY: precheck
  177. precheck::
  178. define PRECHECK_COMMAND_template =
  179. precheck:: $(1)_precheck
  180. PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
  181. .PHONY: $(1)_precheck
  182. $(1)_precheck:
  183. @if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
  184. echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
  185. exit 1; \
  186. fi
  187. endef