2016-11-05 17:09:05 -07:00
# Copyright 2016 The Rook Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2016-09-29 15:48:15 -07:00
# ====================================================================================
# Makefile helper functions for golang
#
ifeq ( $( GO_PROJECT ) ,)
2017-06-26 20:04:53 -07:00
$( error the variable GO_PROJECT must be set prior to including golang .mk )
2016-09-29 15:48:15 -07:00
endif
2017-06-02 13:46:15 -07:00
# These targets will be statically linked.
2016-09-29 15:48:15 -07:00
GO_STATIC_PACKAGES ?=
2017-06-15 12:09:09 -07:00
ifeq ( $( GO_STATIC_PACKAGES ) ,)
$( error please set GO_STATIC_PACKAGES prior to including golang .mk )
2016-09-29 15:48:15 -07:00
endif
2017-07-21 12:20:37 -07:00
# These are the static test packages
GO_STATIC_PACKAGES ?=
2017-06-26 21:52:06 -07:00
# Optional. These are subdirs that we look for all go files to test, vet, and fmt
2016-09-29 15:48:15 -07:00
GO_SUBDIRS ?= cmd pkg
2017-06-26 21:52:06 -07:00
# Optional. Additional subdirs used for integration or e2e testings
GO_INTEGRATION_TESTS_SUBDIRS ?= tests
2016-09-29 15:48:15 -07:00
# Optional directories (relative to CURDIR)
2017-06-26 20:04:53 -07:00
GO_PKG_DIR ?= $( WORK_DIR) /pkg
2016-09-29 15:48:15 -07:00
# Optional build flags passed to go tools
2016-11-14 11:31:35 -08:00
GO_BUILDFLAGS ?=
GO_LDFLAGS ?=
GO_TAGS ?=
2017-06-26 21:52:06 -07:00
GO_TEST_FLAGS ?=
2016-09-29 15:48:15 -07:00
# ====================================================================================
# Setup go environment
2024-02-09 17:04:32 +05:30
GO_SUPPORTED_VERSIONS ?= 1.21| 1.22
2016-09-29 15:48:15 -07:00
2017-06-26 21:52:06 -07:00
GO_PACKAGES := $( foreach t,$( GO_SUBDIRS) ,$( GO_PROJECT) /$( t) /...)
2017-09-19 01:10:50 -07:00
GO_INTEGRATION_TEST_PACKAGES := $( foreach t,$( GO_INTEGRATION_TESTS_SUBDIRS) ,$( GO_PROJECT) /$( t) /integration)
2017-06-26 21:52:06 -07:00
ifneq ( $( GO_TEST_SUITE ) ,)
GO_TEST_FLAGS += -run '$(GO_TEST_SUITE)'
endif
2016-09-29 15:48:15 -07:00
2017-09-19 01:10:50 -07:00
ifneq ( $( GO_TEST_FILTER ) ,)
TEST_FILTER_PARAM := -testify.m '$(GO_TEST_FILTER)'
endif
2017-06-02 13:46:15 -07:00
GOPATH := $( shell go env GOPATH)
2016-09-29 15:48:15 -07:00
2017-03-27 12:10:02 -07:00
# setup tools used during the build
2017-06-26 20:04:53 -07:00
GOLINT := $( TOOLS_HOST_DIR) /golint
2022-07-26 15:48:44 -06:00
GOJUNIT := $( TOOLS_DIR) /go-junit-report
2016-09-29 15:48:15 -07:00
GO := go
GOHOST := GOOS = $( GOHOSTOS) GOARCH = $( GOHOSTARCH) go
2018-09-10 09:02:23 -07:00
GO_VERSION := $( shell $( GO) version | sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,4\}[0-9][^.]\).*/\1/p' )
2021-01-19 11:19:53 -07:00
GO_FULL_VERSION := $( shell $( GO) version)
2018-09-10 09:02:23 -07:00
2021-08-04 12:25:40 +05:30
GOFMT_VERSION := $( GO_VERSION)
2018-09-10 09:02:23 -07:00
ifneq ( $( findstring $( GOFMT_VERSION ) ,$( GO_VERSION )) ,)
GOFMT := $( shell which gofmt)
else
GOFMT := $( TOOLS_HOST_DIR) /gofmt$( GOFMT_VERSION)
endif
2016-09-29 15:48:15 -07:00
2017-07-19 10:36:37 -07:00
GO_OUT_DIR := $( abspath $( OUTPUT_DIR) /bin/$( PLATFORM))
GO_TEST_OUTPUT := $( abspath $( OUTPUT_DIR) /tests/$( PLATFORM))
2016-09-29 15:48:15 -07:00
2016-10-18 15:56:52 -07:00
ifeq ( $( GOOS ) ,windows)
2016-11-16 11:37:24 -08:00
GO_OUT_EXT := .exe
2016-10-18 15:56:52 -07:00
endif
2017-06-02 13:46:15 -07:00
# NOTE: the install suffixes are matched with the build container to speed up the
# the build. Please keep them in sync.
2016-09-29 15:48:15 -07:00
ifneq ( $( GO_PKG_DIR ) ,)
2017-06-26 20:04:53 -07:00
GO_PKG_BASE_DIR := $( abspath $( GO_PKG_DIR) /$( PLATFORM))
2017-06-02 13:46:15 -07:00
GO_PKG_STATIC_FLAGS := -pkgdir $( GO_PKG_BASE_DIR) _static
2016-09-29 15:48:15 -07:00
endif
2017-08-24 22:16:09 -07:00
GO_COMMON_FLAGS = $( GO_BUILDFLAGS) -tags '$(GO_TAGS)'
GO_STATIC_FLAGS = $( GO_COMMON_FLAGS) $( GO_PKG_STATIC_FLAGS) -installsuffix static -ldflags '$(GO_LDFLAGS)'
2016-11-14 11:31:35 -08:00
2016-09-29 15:48:15 -07:00
# ====================================================================================
# Targets
2017-06-26 20:04:53 -07:00
ifeq ( $( filter help clean distclean prune go .clean , $( MAKECMDGOALS )) ,)
2016-09-29 15:48:15 -07:00
.PHONY : go .check
go.check :
ifneq ( $( shell $( GO ) version | grep -q -E '\bgo ($( GO_SUPPORTED_VERSIONS )) \b ' && echo 0 || echo 1) , 0)
2021-01-19 11:19:53 -07:00
$(error unsupported : $( GO_FULL_VERSION ) . Please make install one of the following supported version : '$( GO_SUPPORTED_VERSIONS ) ')
2016-09-29 15:48:15 -07:00
endif
-include go.check
2017-03-30 15:42:03 +02:00
endif
2016-09-29 15:48:15 -07:00
.PHONY : go .init
2020-03-08 15:12:57 +09:00
go.init :
2017-03-30 15:42:03 +02:00
@:
2016-09-29 15:48:15 -07:00
.PHONY : go .build
2017-10-24 12:44:18 -07:00
go.build :
@echo === go build $( PLATFORM)
2021-08-16 12:16:07 -06:00
$( info Go version: $( shell $( GO) version))
2021-11-03 15:00:09 +01:00
$( foreach p,$( GO_STATIC_PACKAGES) ,@CGO_ENABLED= $( CGO_ENABLED_VALUE) $( GO) build -v -o $( GO_OUT_DIR) /$( lastword $( subst /, ,$( p)))$( GO_OUT_EXT) $( GO_STATIC_FLAGS) $( p) ${ \n } )
$( foreach p,$( GO_TEST_PACKAGES) ,@CGO_ENABLED= $( CGO_ENABLED_VALUE) $( GO) test -v -c -o $( GO_TEST_OUTPUT) /$( lastword $( subst /, ,$( p)))$( GO_OUT_EXT) $( GO_STATIC_FLAGS) $( p) ${ \n } )
2016-09-29 15:48:15 -07:00
.PHONY : go .install
2017-10-24 12:44:18 -07:00
go.install :
@echo === go install $( PLATFORM)
2021-11-03 15:00:09 +01:00
$( foreach p,$( GO_STATIC_PACKAGES) ,@CGO_ENABLED= $( CGO_ENABLED_VALUE) $( GO) install -v $( GO_STATIC_FLAGS) $( p) ${ \n } )
2016-09-29 15:48:15 -07:00
2021-04-20 02:53:36 +00:00
# GOJUNIT need to happen in order and NOT in parallel, so call them explicitly
2017-07-17 17:35:25 -07:00
.PHONY : go .test .unit
2021-04-20 08:42:15 +02:00
go.test.unit :
@$( MAKE) $( GOJUNIT)
2017-06-26 21:52:06 -07:00
@echo === go test unit-tests
@mkdir -p $( GO_TEST_OUTPUT)
2021-11-03 15:00:09 +01:00
CGO_ENABLED = $( CGO_ENABLED_VALUE) $( GOHOST) test -v -cover $( GO_STATIC_FLAGS) $( GO_PACKAGES)
CGO_ENABLED = $( CGO_ENABLED_VALUE) $( GOHOST) test -v -cover $( GO_TEST_FLAGS) $( GO_STATIC_FLAGS) $( GO_PACKAGES) 2>& 1 | tee $( GO_TEST_OUTPUT) /unit-tests.log
2017-06-26 21:52:06 -07:00
@cat $( GO_TEST_OUTPUT) /unit-tests.log | $( GOJUNIT) -set-exit-code > $( GO_TEST_OUTPUT) /unit-tests.xml
.PHONY :
go.test.integration : $( GOJUNIT )
@echo === go test integration-tests
@mkdir -p $( GO_TEST_OUTPUT)
2021-11-03 15:00:09 +01:00
CGO_ENABLED = $( CGO_ENABLED_VALUE) $( GOHOST) test -v -i $( GO_STATIC_FLAGS) $( GO_INTEGRATION_TEST_PACKAGES)
CGO_ENABLED = $( CGO_ENABLED_VALUE) $( GOHOST) test -v -timeout 7200s $( GO_TEST_FLAGS) $( GO_STATIC_FLAGS) $( GO_INTEGRATION_TEST_PACKAGES) $( TEST_FILTER_PARAM) 2>& 1 | tee $( GO_TEST_OUTPUT) /integration-tests.log
2017-06-26 21:52:06 -07:00
@cat $( GO_TEST_OUTPUT) /integration-tests.log | $( GOJUNIT) -set-exit-code > $( GO_TEST_OUTPUT) /integration-tests.xml
2016-09-29 15:48:15 -07:00
2017-03-02 11:30:33 -08:00
.PHONY : go .lint
2017-03-27 12:10:02 -07:00
go.lint : $( GOLINT )
2017-06-02 13:46:15 -07:00
@echo === go lint
2017-06-26 21:52:06 -07:00
@$( GOLINT) -set_exit_status= true $( GO_PACKAGES) $( GO_INTEGRATION_TEST_PACKAGES)
2017-03-02 11:30:33 -08:00
2016-09-29 15:48:15 -07:00
.PHONY : go .vet
go.vet :
2017-06-02 13:46:15 -07:00
@echo === go vet
2021-11-02 18:54:58 +01:00
CGO_ENABLED = $( CGO_ENABLED_VALUE) $( GOHOST) vet $( GO_COMMON_FLAGS) $( GO_PACKAGES) $( GO_INTEGRATION_TEST_PACKAGES)
2016-09-29 15:48:15 -07:00
.PHONY : go .fmt
2021-08-24 18:52:39 +02:00
# ignore deepcopy generated files since the tool hardcoded the header with a "// +build" which in Golang 1.17 makes it fail gofmt since "////go:build" is preferred
# see: https://github.com/kubernetes/gengo/blob/master/examples/deepcopy-gen/generators/deepcopy.go#L136
# https://github.com/kubernetes/gengo/pull/210
2018-09-10 09:02:23 -07:00
go.fmt : $( GOFMT )
2021-08-24 18:52:39 +02:00
@gofmt_out= $$ ( find $( GO_SUBDIRS) -name "*.go" -not -name "*.deepcopy.go" | xargs $( GOFMT) -s -d -e 2>& 1) && [ -z " $$ {gofmt_out}" ] || ( echo " $$ {gofmt_out}" 1>& 2; exit 1)
2016-09-29 15:48:15 -07:00
2017-03-30 15:42:03 +02:00
go.validate : go .vet go .fmt
2020-03-10 14:42:15 +09:00
.PHONY : go .mod .update
go.mod.update :
@echo === updating modules
@$( GOHOST) get -u ./...
.PHONY : go .mod .check
go.mod.check :
2023-10-26 15:20:13 -06:00
@echo === syncing root modules with APIs modules
@cp -a go.sum pkg/apis/go.sum
@cat go.mod | sed -e 's|^module github.com/rook/rook|module github.com/rook/rook/pkg/apis|' \
-e '\:^replace github.com/rook/rook/pkg/apis => ./pkg/apis:d' > pkg/apis/go.mod
@echo === ensuring APIs modules are tidied
@( cd pkg/apis/; $( GOHOST) mod tidy -compat= $( GO_VERSION) )
2023-05-01 18:40:45 +03:00
@echo === ensuring root modules are tidied
2023-08-24 17:28:12 +05:30
@$( GOHOST) mod tidy -compat= $( GO_VERSION)
2017-11-10 08:47:22 -08:00
2020-03-10 14:42:15 +09:00
.PHONY : go .mod .clean
go.mod.clean :
@echo === cleaning modules cache
@sudo rm -fr $( WORK_DIR) /cross_pkg
@$( GOHOST) clean -modcache
2016-09-29 15:48:15 -07:00
2017-03-27 12:10:02 -07:00
$(GOLINT) :
2017-06-02 13:46:15 -07:00
@echo === installing golint
2017-06-26 20:04:53 -07:00
@mkdir -p $( TOOLS_HOST_DIR) /tmp
@GOPATH= $( TOOLS_HOST_DIR) /tmp GOBIN = $( TOOLS_HOST_DIR) $( GOHOST) get github.com/golang/lint/golint
@rm -fr $( TOOLS_HOST_DIR) /tmp
2016-09-29 15:48:15 -07:00
2018-09-10 09:02:23 -07:00
$(GOFMT) :
@echo === installing gofmt$( GOFMT_VERSION)
@mkdir -p $( TOOLS_HOST_DIR) /tmp
2021-11-02 18:54:58 +01:00
@curl -sL https://dl.google.com/go/go$( GOFMT_VERSION) .$( shell go env GOHOSTOS) -$( GOHOSTARCH) .tar.gz | tar -xz -C $( TOOLS_HOST_DIR) /tmp
2018-09-10 09:02:23 -07:00
@mv $( TOOLS_HOST_DIR) /tmp/go/bin/gofmt $( GOFMT)
@rm -fr $( TOOLS_HOST_DIR) /tmp
2017-06-26 21:52:06 -07:00
$(GOJUNIT) :
@echo === installing go-junit-report
2022-07-26 15:48:44 -06:00
@mkdir -p $( TOOLS_DIR) /tmp
@curl -sL https://github.com/jstemmer/go-junit-report/releases/download/v2.0.0/go-junit-report-v2.0.0-$( GOOS) -$( GOHOSTARCH) .tar.gz | tar -xz -C $( TOOLS_DIR) /tmp
@mv $( TOOLS_DIR) /tmp/go-junit-report $( TOOLS_DIR)
@rm -fr $( TOOLS_DIR) /tmp
2021-04-08 13:19:14 -06:00
export CONTROLLER_GEN = $( TOOLS_HOST_DIR) /controller-gen-$( CONTROLLER_GEN_VERSION)
2021-11-22 17:31:19 -07:00
$(CONTROLLER_GEN) :
2021-04-08 13:19:14 -06:00
{ \
2021-04-15 12:27:52 -06:00
set -e ; \
2021-04-08 13:19:14 -06:00
mkdir -p $( TOOLS_HOST_DIR) ; \
CONTROLLER_GEN_TMP_DIR = $$ ( mktemp -d) ; \
cd $$ CONTROLLER_GEN_TMP_DIR ; \
go mod init tmp; \
2021-04-20 08:42:15 +02:00
unset GOOS GOARCH ; \
2021-11-03 15:00:09 +01:00
export CGO_ENABLED = $( CGO_ENABLED_VALUE) ; \
2021-04-20 08:42:15 +02:00
export GOBIN = $$ CONTROLLER_GEN_TMP_DIR ; \
2021-04-08 13:19:14 -06:00
echo === installing controller-gen ; \
2022-04-21 19:48:52 +05:30
go install sigs.k8s.io/controller-tools/cmd/controller-gen@$( CONTROLLER_GEN_VERSION) ; \
2021-04-08 13:19:14 -06:00
mv $$ CONTROLLER_GEN_TMP_DIR/controller-gen $( CONTROLLER_GEN) ; \
rm -rf $$ CONTROLLER_GEN_TMP_DIR ; \
2021-04-20 02:53:36 +00:00
}
2021-11-22 17:31:19 -07:00
YQ_VERSION = v4.14.2
YQ := $( TOOLS_HOST_DIR) /yq-$( YQ_VERSION)
export YQ
$(YQ) :
@echo === installing yq $( YQ_VERSION) $( REAL_HOST_PLATFORM)
@mkdir -p $( TOOLS_HOST_DIR)
@curl -JL https://github.com/mikefarah/yq/releases/download/$( YQ_VERSION) /yq_$( REAL_HOST_PLATFORM) -o $( YQ)
@chmod +x $( YQ)
2021-04-20 02:53:36 +00:00
export CODE_GENERATOR_VERSION = 0.20.0
export CODE_GENERATOR = $( TOOLS_HOST_DIR) /code-generator-$( CODE_GENERATOR_VERSION)
$(CODE_GENERATOR) :
mkdir -p $( TOOLS_HOST_DIR)
curl -sL https://github.com/kubernetes/code-generator/archive/refs/tags/v${ CODE_GENERATOR_VERSION } .tar.gz | tar -xz -C $( TOOLS_HOST_DIR)