Makefile.common 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. PROMU := $(FIRST_GOPATH)/bin/promu
  27. STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
  28. GOVENDOR := $(FIRST_GOPATH)/bin/govendor
  29. pkgs = ./...
  30. PREFIX ?= $(shell pwd)
  31. BIN_DIR ?= $(shell pwd)
  32. DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
  33. DOCKER_REPO ?= prom
  34. .PHONY: all
  35. all: style staticcheck unused build test
  36. # This rule is used to forward a target like "build" to "common-build". This
  37. # allows a new "build" target to be defined in a Makefile which includes this
  38. # one and override "common-build" without override warnings.
  39. %: common-% ;
  40. .PHONY: common-style
  41. common-style:
  42. @echo ">> checking code style"
  43. @fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
  44. if [ -n "$${fmtRes}" ]; then \
  45. echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
  46. echo "Please ensure you are using $$($(GO) version) for formatting code."; \
  47. exit 1; \
  48. fi
  49. .PHONY: common-check_license
  50. common-check_license:
  51. @echo ">> checking license header"
  52. @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \
  53. awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \
  54. done); \
  55. if [ -n "$${licRes}" ]; then \
  56. echo "license header checking failed:"; echo "$${licRes}"; \
  57. exit 1; \
  58. fi
  59. .PHONY: common-test-short
  60. common-test-short:
  61. @echo ">> running short tests"
  62. $(GO) test -short $(pkgs)
  63. .PHONY: common-test
  64. common-test:
  65. @echo ">> running all tests"
  66. $(GO) test -race $(pkgs)
  67. .PHONY: common-format
  68. common-format:
  69. @echo ">> formatting code"
  70. $(GO) fmt $(pkgs)
  71. .PHONY: common-vet
  72. common-vet:
  73. @echo ">> vetting code"
  74. $(GO) vet $(pkgs)
  75. .PHONY: common-staticcheck
  76. common-staticcheck: $(STATICCHECK)
  77. @echo ">> running staticcheck"
  78. $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
  79. .PHONY: common-unused
  80. common-unused: $(GOVENDOR)
  81. @echo ">> running check for unused packages"
  82. @$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages'
  83. .PHONY: common-build
  84. common-build: promu
  85. @echo ">> building binaries"
  86. $(PROMU) build --prefix $(PREFIX)
  87. .PHONY: common-tarball
  88. common-tarball: promu
  89. @echo ">> building release tarball"
  90. $(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)
  91. .PHONY: common-docker
  92. common-docker:
  93. docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .
  94. .PHONY: common-docker-publish
  95. common-docker-publish:
  96. docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)"
  97. .PHONY: common-docker-tag-latest
  98. common-docker-tag-latest:
  99. docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest"
  100. .PHONY: promu
  101. promu:
  102. GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu
  103. .PHONY: $(STATICCHECK)
  104. $(STATICCHECK):
  105. GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck
  106. .PHONY: $(GOVENDOR)
  107. $(GOVENDOR):
  108. GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor