Makefile 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Copyright The containerd 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. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. # Go command to use for build
  12. GO ?= go
  13. INSTALL ?= install
  14. # Root directory of the project (absolute path).
  15. ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
  16. WHALE = "🇩"
  17. ONI = "👹"
  18. # Project binaries.
  19. COMMANDS=protoc-gen-go-ttrpc protoc-gen-gogottrpc
  20. ifdef BUILDTAGS
  21. GO_BUILDTAGS = ${BUILDTAGS}
  22. endif
  23. GO_BUILDTAGS ?=
  24. GO_TAGS=$(if $(GO_BUILDTAGS),-tags "$(strip $(GO_BUILDTAGS))",)
  25. # Project packages.
  26. PACKAGES=$(shell $(GO) list ${GO_TAGS} ./... | grep -v /example)
  27. TESTPACKAGES=$(shell $(GO) list ${GO_TAGS} ./... | grep -v /cmd | grep -v /integration | grep -v /example)
  28. BINPACKAGES=$(addprefix ./cmd/,$(COMMANDS))
  29. #Replaces ":" (*nix), ";" (windows) with newline for easy parsing
  30. GOPATHS=$(shell echo ${GOPATH} | tr ":" "\n" | tr ";" "\n")
  31. TESTFLAGS_RACE=
  32. GO_BUILD_FLAGS=
  33. # See Golang issue re: '-trimpath': https://github.com/golang/go/issues/13809
  34. GO_GCFLAGS=$(shell \
  35. set -- ${GOPATHS}; \
  36. echo "-gcflags=-trimpath=$${1}/src"; \
  37. )
  38. BINARIES=$(addprefix bin/,$(COMMANDS))
  39. # Flags passed to `go test`
  40. TESTFLAGS ?= $(TESTFLAGS_RACE) $(EXTRA_TESTFLAGS)
  41. TESTFLAGS_PARALLEL ?= 8
  42. # Use this to replace `go test` with, for instance, `gotestsum`
  43. GOTEST ?= $(GO) test
  44. .PHONY: clean all AUTHORS build binaries test integration generate protos check-protos coverage ci check help install vendor install-protobuf install-protobuild
  45. .DEFAULT: default
  46. # Forcibly set the default goal to all, in case an include above brought in a rule definition.
  47. .DEFAULT_GOAL := all
  48. all: binaries
  49. check: proto-fmt ## run all linters
  50. @echo "$(WHALE) $@"
  51. GOGC=75 golangci-lint run
  52. ci: check binaries check-protos coverage # coverage-integration ## to be used by the CI
  53. AUTHORS: .mailmap .git/HEAD
  54. git log --format='%aN <%aE>' | sort -fu > $@
  55. generate: protos
  56. @echo "$(WHALE) $@"
  57. @PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES}
  58. protos: bin/protoc-gen-gogottrpc bin/protoc-gen-go-ttrpc ## generate protobuf
  59. @echo "$(WHALE) $@"
  60. @(PATH="${ROOTDIR}/bin:${PATH}" protobuild --quiet ${PACKAGES})
  61. check-protos: protos ## check if protobufs needs to be generated again
  62. @echo "$(WHALE) $@"
  63. @test -z "$$(git status --short | grep ".pb.go" | tee /dev/stderr)" || \
  64. ((git diff | cat) && \
  65. (echo "$(ONI) please run 'make protos' when making changes to proto files" && false))
  66. check-api-descriptors: protos ## check that protobuf changes aren't present.
  67. @echo "$(WHALE) $@"
  68. @test -z "$$(git status --short | grep ".pb.txt" | tee /dev/stderr)" || \
  69. ((git diff $$(find . -name '*.pb.txt') | cat) && \
  70. (echo "$(ONI) please run 'make protos' when making changes to proto files and check-in the generated descriptor file changes" && false))
  71. proto-fmt: ## check format of proto files
  72. @echo "$(WHALE) $@"
  73. @test -z "$$(find . -name '*.proto' -type f -exec grep -Hn -e "^ " {} \; | tee /dev/stderr)" || \
  74. (echo "$(ONI) please indent proto files with tabs only" && false)
  75. @test -z "$$(find . -name '*.proto' -type f -exec grep -Hn "Meta meta = " {} \; | grep -v '(gogoproto.nullable) = false' | tee /dev/stderr)" || \
  76. (echo "$(ONI) meta fields in proto files must have option (gogoproto.nullable) = false" && false)
  77. build: ## build the go packages
  78. @echo "$(WHALE) $@"
  79. @$(GO) build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} ${EXTRA_FLAGS} ${PACKAGES}
  80. test: ## run tests, except integration tests and tests that require root
  81. @echo "$(WHALE) $@"
  82. @$(GOTEST) ${TESTFLAGS} ${TESTPACKAGES}
  83. integration: ## run integration tests
  84. @echo "$(WHALE) $@"
  85. @cd "${ROOTDIR}/integration" && $(GOTEST) -v ${TESTFLAGS} -parallel ${TESTFLAGS_PARALLEL} .
  86. benchmark: ## run benchmarks tests
  87. @echo "$(WHALE) $@"
  88. @$(GO) test ${TESTFLAGS} -bench . -run Benchmark
  89. FORCE:
  90. define BUILD_BINARY
  91. @echo "$(WHALE) $@"
  92. @$(GO) build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} -o $@ ${GO_TAGS} ./$<
  93. endef
  94. # Build a binary from a cmd.
  95. bin/%: cmd/% FORCE
  96. $(call BUILD_BINARY)
  97. binaries: $(BINARIES) ## build binaries
  98. @echo "$(WHALE) $@"
  99. clean: ## clean up binaries
  100. @echo "$(WHALE) $@"
  101. @rm -f $(BINARIES)
  102. install: ## install binaries
  103. @echo "$(WHALE) $@ $(BINPACKAGES)"
  104. @$(GO) install $(BINPACKAGES)
  105. install-protobuf:
  106. @echo "$(WHALE) $@"
  107. @script/install-protobuf
  108. install-protobuild:
  109. @echo "$(WHALE) $@"
  110. @$(GO) install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1
  111. @$(GO) install github.com/containerd/protobuild@14832ccc41429f5c4f81028e5af08aa233a219cf
  112. coverage: ## generate coverprofiles from the unit tests, except tests that require root
  113. @echo "$(WHALE) $@"
  114. @rm -f coverage.txt
  115. @$(GO) test ${TESTFLAGS} ${TESTPACKAGES} 2> /dev/null
  116. @( for pkg in ${PACKAGES}; do \
  117. $(GO) test ${TESTFLAGS} \
  118. -cover \
  119. -coverprofile=profile.out \
  120. -covermode=atomic $$pkg || exit; \
  121. if [ -f profile.out ]; then \
  122. cat profile.out >> coverage.txt; \
  123. rm profile.out; \
  124. fi; \
  125. done )
  126. vendor: ## ensure all the go.mod/go.sum files are up-to-date
  127. @echo "$(WHALE) $@"
  128. @$(GO) mod tidy
  129. @$(GO) mod verify
  130. verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date
  131. @echo "$(WHALE) $@"
  132. @$(GO) mod tidy
  133. @$(GO) mod verify
  134. @test -z "$$(git status --short | grep "go.sum" | tee /dev/stderr)" || \
  135. ((git diff | cat) && \
  136. (echo "$(ONI) make sure to checkin changes after go mod tidy" && false))
  137. help: ## this help
  138. @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort