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