Makefile: allow overriding go command by environment

This is required for environments/build systems where a specific
go version / command needs to be used.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2021-05-04 13:22:45 +02:00
parent cbdc094d33
commit 9ea25634bd

View File

@ -13,6 +13,9 @@
# limitations under the License. # limitations under the License.
# Go command to use for build
GO ?= go
# Root directory of the project (absolute path). # Root directory of the project (absolute path).
ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
@ -26,9 +29,9 @@ REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet
PACKAGE=github.com/containerd/containerd PACKAGE=github.com/containerd/containerd
SHIM_CGO_ENABLED ?= 0 SHIM_CGO_ENABLED ?= 0
ifneq "$(strip $(shell command -v go 2>/dev/null))" "" ifneq "$(strip $(shell command -v $(GO) 2>/dev/null))" ""
GOOS ?= $(shell go env GOOS) GOOS ?= $(shell $(GO) env GOOS)
GOARCH ?= $(shell go env GOARCH) GOARCH ?= $(shell $(GO) env GOARCH)
else else
ifeq ($(GOOS),) ifeq ($(GOOS),)
# approximate GOOS for the platform if we don't have Go and GOOS isn't # approximate GOOS for the platform if we don't have Go and GOOS isn't
@ -85,7 +88,7 @@ GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revi
SHIM_GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) -extldflags "-static" $(EXTRA_LDFLAGS)' SHIM_GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) -extldflags "-static" $(EXTRA_LDFLAGS)'
# Project packages. # Project packages.
PACKAGES=$(shell go list ${GO_TAGS} ./... | grep -v /vendor/ | grep -v /integration) PACKAGES=$(shell $(GO) list ${GO_TAGS} ./... | grep -v /vendor/ | grep -v /integration)
TEST_REQUIRES_ROOT_PACKAGES=$(filter \ TEST_REQUIRES_ROOT_PACKAGES=$(filter \
${PACKAGES}, \ ${PACKAGES}, \
$(shell \ $(shell \
@ -122,7 +125,7 @@ TESTFLAGS ?= $(TESTFLAGS_RACE) $(EXTRA_TESTFLAGS)
TESTFLAGS_PARALLEL ?= 8 TESTFLAGS_PARALLEL ?= 8
# Use this to replace `go test` with, for instance, `gotestsum` # Use this to replace `go test` with, for instance, `gotestsum`
GOTEST ?= go test GOTEST ?= $(GO) test
OUTPUTDIR = $(join $(ROOTDIR), _output) OUTPUTDIR = $(join $(ROOTDIR), _output)
CRIDIR=$(OUTPUTDIR)/cri CRIDIR=$(OUTPUTDIR)/cri
@ -143,7 +146,7 @@ AUTHORS: .mailmap .git/HEAD
generate: protos generate: protos
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@PATH="${ROOTDIR}/bin:${PATH}" go generate -x ${PACKAGES} @PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES}
protos: bin/protoc-gen-gogoctrd ## generate protobuf protos: bin/protoc-gen-gogoctrd ## generate protobuf
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@ -170,7 +173,7 @@ proto-fmt: ## check format of proto files
build: ## build the go packages build: ## build the go packages
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@go build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} ${EXTRA_FLAGS} ${GO_LDFLAGS} ${PACKAGES} @$(GO) build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} ${EXTRA_FLAGS} ${GO_LDFLAGS} ${PACKAGES}
test: ## run tests, except integration tests and tests that require root test: ## run tests, except integration tests and tests that require root
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@ -182,12 +185,12 @@ root-test: ## run tests, except integration tests
integration: ## run integration tests integration: ## run integration tests
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@cd "${ROOTDIR}/integration/client" && go mod download && $(GOTEST) -v ${TESTFLAGS} -test.root -parallel ${TESTFLAGS_PARALLEL} . @cd "${ROOTDIR}/integration/client" && $(GO) mod download && $(GOTEST) -v ${TESTFLAGS} -test.root -parallel ${TESTFLAGS_PARALLEL} .
# TODO integrate cri integration bucket with coverage # TODO integrate cri integration bucket with coverage
bin/cri-integration.test: bin/cri-integration.test:
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@go test -c ./integration -o bin/cri-integration.test @$(GO) test -c ./integration -o bin/cri-integration.test
cri-integration: binaries bin/cri-integration.test ## run cri integration tests cri-integration: binaries bin/cri-integration.test ## run cri integration tests
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@ -196,13 +199,13 @@ cri-integration: binaries bin/cri-integration.test ## run cri integration tests
benchmark: ## run benchmarks tests benchmark: ## run benchmarks tests
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@go test ${TESTFLAGS} -bench . -run Benchmark -test.root @$(GO) test ${TESTFLAGS} -bench . -run Benchmark -test.root
FORCE: FORCE:
define BUILD_BINARY define BUILD_BINARY
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@go build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} -o $@ ${GO_LDFLAGS} ${GO_TAGS} ./$< @$(GO) build ${DEBUG_GO_GCFLAGS} ${GO_GCFLAGS} ${GO_BUILD_FLAGS} -o $@ ${GO_LDFLAGS} ${GO_TAGS} ./$<
endef endef
# Build a binary from a cmd. # Build a binary from a cmd.
@ -211,15 +214,15 @@ bin/%: cmd/% FORCE
bin/containerd-shim: cmd/containerd-shim FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220 bin/containerd-shim: cmd/containerd-shim FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220
@echo "$(WHALE) bin/containerd-shim" @echo "$(WHALE) bin/containerd-shim"
@CGO_ENABLED=${SHIM_CGO_ENABLED} go build ${GO_BUILD_FLAGS} -o bin/containerd-shim ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim @CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o bin/containerd-shim ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim
bin/containerd-shim-runc-v1: cmd/containerd-shim-runc-v1 FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220 bin/containerd-shim-runc-v1: cmd/containerd-shim-runc-v1 FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220
@echo "$(WHALE) bin/containerd-shim-runc-v1" @echo "$(WHALE) bin/containerd-shim-runc-v1"
@CGO_ENABLED=${SHIM_CGO_ENABLED} go build ${GO_BUILD_FLAGS} -o bin/containerd-shim-runc-v1 ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim-runc-v1 @CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o bin/containerd-shim-runc-v1 ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim-runc-v1
bin/containerd-shim-runc-v2: cmd/containerd-shim-runc-v2 FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220 bin/containerd-shim-runc-v2: cmd/containerd-shim-runc-v2 FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220
@echo "$(WHALE) bin/containerd-shim-runc-v2" @echo "$(WHALE) bin/containerd-shim-runc-v2"
@CGO_ENABLED=${SHIM_CGO_ENABLED} go build ${GO_BUILD_FLAGS} -o bin/containerd-shim-runc-v2 ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim-runc-v2 @CGO_ENABLED=${SHIM_CGO_ENABLED} $(GO) build ${GO_BUILD_FLAGS} -o bin/containerd-shim-runc-v2 ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim-runc-v2
binaries: $(BINARIES) ## build binaries binaries: $(BINARIES) ## build binaries
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@ -235,11 +238,11 @@ genman: man/containerd.8 man/ctr.8
man/containerd.8: FORCE man/containerd.8: FORCE
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
go run cmd/gen-manpages/main.go $(@F) $(@D) $(GO) run cmd/gen-manpages/main.go $(@F) $(@D)
man/ctr.8: FORCE man/ctr.8: FORCE
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
go run cmd/gen-manpages/main.go $(@F) $(@D) $(GO) run cmd/gen-manpages/main.go $(@F) $(@D)
man/%: docs/man/%.md FORCE man/%: docs/man/%.md FORCE
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@ -365,9 +368,9 @@ endif
coverage: ## generate coverprofiles from the unit tests, except tests that require root coverage: ## generate coverprofiles from the unit tests, except tests that require root
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@rm -f coverage.txt @rm -f coverage.txt
@go test -i ${TESTFLAGS} ${PACKAGES} 2> /dev/null @$(GO) test -i ${TESTFLAGS} ${PACKAGES} 2> /dev/null
@( for pkg in ${PACKAGES}; do \ @( for pkg in ${PACKAGES}; do \
go test ${TESTFLAGS} \ $(GO) test ${TESTFLAGS} \
-cover \ -cover \
-coverprofile=profile.out \ -coverprofile=profile.out \
-covermode=atomic $$pkg || exit; \ -covermode=atomic $$pkg || exit; \
@ -379,9 +382,9 @@ coverage: ## generate coverprofiles from the unit tests, except tests that requi
root-coverage: ## generate coverage profiles for unit tests that require root root-coverage: ## generate coverage profiles for unit tests that require root
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@go test -i ${TESTFLAGS} ${TEST_REQUIRES_ROOT_PACKAGES} 2> /dev/null @$(GO) test -i ${TESTFLAGS} ${TEST_REQUIRES_ROOT_PACKAGES} 2> /dev/null
@( for pkg in ${TEST_REQUIRES_ROOT_PACKAGES}; do \ @( for pkg in ${TEST_REQUIRES_ROOT_PACKAGES}; do \
go test ${TESTFLAGS} \ $(GO) test ${TESTFLAGS} \
-cover \ -cover \
-coverprofile=profile.out \ -coverprofile=profile.out \
-covermode=atomic $$pkg -test.root || exit; \ -covermode=atomic $$pkg -test.root || exit; \
@ -393,8 +396,8 @@ root-coverage: ## generate coverage profiles for unit tests that require root
vendor: ## vendor vendor: ## vendor
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@go mod tidy @$(GO) mod tidy
@go mod vendor @$(GO) mod vendor
help: ## this help help: ## this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort