| 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 |
| 17 |
GO ?= go |
| 18 |
INSTALL ?= install |
| 19 |
|
| 20 |
# Root directory of the project (absolute path). |
| 21 |
ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 22 |
|
| 23 |
WHALE = "🇩" |
| 24 |
ONI = "👹" |
| 25 |
|
| 26 |
# Project binaries. |
| 27 |
COMMANDS=protoc-gen-go-ttrpc protoc-gen-gogottrpc |
| 28 |
|
| 29 |
ifdef BUILDTAGS |
| 30 |
GO_BUILDTAGS = ${BUILDTAGS} |
| 31 |
endif |
| 32 |
GO_BUILDTAGS ?= |
| 33 |
GO_TAGS=$(if $(GO_BUILDTAGS),-tags "$(strip $(GO_BUILDTAGS))",) |
| 34 |
|
| 35 |
# Project packages. |
| 36 |
PACKAGES=$(shell $(GO) list ${GO_TAGS} ./... | grep -v /example) |
| 37 |
TESTPACKAGES=$(shell $(GO) list ${GO_TAGS} ./... | grep -v /cmd | grep -v /integration | grep -v /example) |
| 38 |
BINPACKAGES=$(addprefix ./cmd/,$(COMMANDS)) |
| 39 |
|
| 40 |
#Replaces ":" (*nix), ";" (windows) with newline for easy parsing |
| 41 |
GOPATHS=$(shell echo ${GOPATH} | tr ":" "\n" | tr ";" "\n") |
| 42 |
|
| 43 |
TESTFLAGS_RACE= |
| 44 |
GO_BUILD_FLAGS= |
| 45 |
# See Golang issue re: '-trimpath': https://github.com/golang/go/issues/13809 |
| 46 |
GO_GCFLAGS=$(shell \ |
| 47 |
set -- ${GOPATHS}; \ |
| 48 |
echo "-gcflags=-trimpath=$${1}/src"; \ |
| 49 |
) |
| 50 |
|
| 51 |
BINARIES=$(addprefix bin/,$(COMMANDS)) |
| 52 |
|
| 53 |
# Flags passed to `go test` |
| 54 |
TESTFLAGS ?= $(TESTFLAGS_RACE) $(EXTRA_TESTFLAGS) |
| 55 |
TESTFLAGS_PARALLEL ?= 8 |
| 56 |
|
| 57 |
# Use this to replace `go test` with, for instance, `gotestsum` |
| 58 |
GOTEST ?= $(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 |
|
| 66 |
all: binaries |
| 67 |
|
| 68 |
check: proto-fmt ## run all linters |
| 69 |
@echo "$(WHALE) $@" |
| 70 |
GOGC=75 golangci-lint run |
| 71 |
|
| 72 |
ci: check binaries check-protos coverage # coverage-integration ## to be used by the CI |
| 73 |
|
| 74 |
AUTHORS: .mailmap .git/HEAD |
| 75 |
git log --format='%aN <%aE>' | sort -fu > $@ |
| 76 |
|
| 77 |
generate: protos |
| 78 |
@echo "$(WHALE) $@" |
| 79 |
@PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES} |
| 80 |
|
| 81 |
protos: bin/protoc-gen-gogottrpc bin/protoc-gen-go-ttrpc ## generate protobuf |
| 82 |
@echo "$(WHALE) $@" |
| 83 |
@(PATH="${ROOTDIR}/bin:${PATH}" protobuild --quiet ${PACKAGES}) |
| 84 |
|
| 85 |
check-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 |
|
| 91 |
check-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 |
|
| 97 |
proto-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 |
|
| 104 |
build: ## build the go packages |
| 105 |
@echo "$(WHALE) $@" |
| 106 |
@$(GO) build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} ${EXTRA_FLAGS} ${PACKAGES} |
| 107 |
|
| 108 |
test: ## run tests, except integration tests and tests that require root |
| 109 |
@echo "$(WHALE) $@" |
| 110 |
@$(GOTEST) ${TESTFLAGS} ${TESTPACKAGES} |
| 111 |
|
| 112 |
integration: ## run integration tests |
| 113 |
@echo "$(WHALE) $@" |
| 114 |
@cd "${ROOTDIR}/integration" && $(GOTEST) -v ${TESTFLAGS} -parallel ${TESTFLAGS_PARALLEL} . |
| 115 |
|
| 116 |
benchmark: ## run benchmarks tests |
| 117 |
@echo "$(WHALE) $@" |
| 118 |
@$(GO) test ${TESTFLAGS} -bench . -run Benchmark |
| 119 |
|
| 120 |
FORCE: |
| 121 |
|
| 122 |
define BUILD_BINARY |
| 123 |
@echo "$(WHALE) $@" |
| 124 |
@$(GO) build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} -o $@ ${GO_TAGS} ./$< |
| 125 |
endef |
| 126 |
|
| 127 |
# Build a binary from a cmd. |
| 128 |
bin/%: cmd/% FORCE |
| 129 |
$(call BUILD_BINARY) |
| 130 |
|
| 131 |
binaries: $(BINARIES) ## build binaries |
| 132 |
@echo "$(WHALE) $@" |
| 133 |
|
| 134 |
clean: ## clean up binaries |
| 135 |
@echo "$(WHALE) $@" |
| 136 |
@rm -f $(BINARIES) |
| 137 |
|
| 138 |
install: ## install binaries |
| 139 |
@echo "$(WHALE) $@ $(BINPACKAGES)" |
| 140 |
@$(GO) install $(BINPACKAGES) |
| 141 |
|
| 142 |
install-protobuf: |
| 143 |
@echo "$(WHALE) $@" |
| 144 |
@script/install-protobuf |
| 145 |
|
| 146 |
install-protobuild: |
| 147 |
@echo "$(WHALE) $@" |
| 148 |
@$(GO) install google.golang.org/protobuf/cmd/[email protected] |
| 149 |
@$(GO) install github.com/containerd/protobuild@14832ccc41429f5c4f81028e5af08aa233a219cf |
| 150 |
|
| 151 |
coverage: ## 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 |
|
| 166 |
vendor: ## ensure all the go.mod/go.sum files are up-to-date |
| 167 |
@echo "$(WHALE) $@" |
| 168 |
@$(GO) mod tidy |
| 169 |
@$(GO) mod verify |
| 170 |
|
| 171 |
verify-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 |
|
| 179 |
help: ## this help |
| 180 |
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort |