blob: c14bbbf05643c170128ff34147b952c73ba977d1 [file] [log] [blame]
khenaidoocfee5f42018-07-19 22:47:38 -04001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040017# set default shell
18SHELL = bash -e -o pipefail
Kent Hagerman074d0e02019-04-24 17:58:16 -040019
Zack Williams27f59a42019-05-10 09:12:07 -070020# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040021VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070022
Maninder9a1bc0d2020-10-26 11:34:02 +053023# Packages
24PACKAGES = $(shell go list ./...)
25
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040026DOCKER_LABEL_VCS_DIRTY = false
27ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
28 DOCKER_LABEL_VCS_DIRTY = true
29endif
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040030## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070031DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040032DOCKER_REGISTRY ?=
33DOCKER_REPOSITORY ?=
34DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge41835142021-04-01 17:26:12 +000035DOCKER_TARGET ?= prod
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040036RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
Andrea Campanella7a6cce22019-12-17 14:10:27 -080037TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040038
39## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040040DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
41DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
42DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
43DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040044
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040045DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070046 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040047 --build-arg org_label_schema_version="${VERSION}" \
48 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
49 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
50 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
51 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
52 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
53
54DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040055 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040056
Kent Hagerman812b2572020-01-28 11:57:51 -050057# tool containers
David K. Bainbridgefc8e1812021-04-09 16:09:49 +000058VOLTHA_TOOLS_VERSION ?= 2.4.0
Kent Hagerman812b2572020-01-28 11:57:51 -050059
Kent Hagerman1b820752020-02-14 14:45:51 -050060GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
Kent Hagerman812b2572020-01-28 11:57:51 -050061GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
Matteo Scandoloa4c81452020-11-23 14:37:11 -080062GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Kent Hagerman1b820752020-02-14 14:45:51 -050063GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
64HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
Kent Hagerman812b2572020-01-28 11:57:51 -050065
David K. Bainbridge41835142021-04-01 17:26:12 +000066.PHONY: docker-build local-protos local-lib-go help
67.DEFAULT_GOAL := help
khenaidoocfee5f42018-07-19 22:47:38 -040068
khenaidoocfee5f42018-07-19 22:47:38 -040069
David K. Bainbridge1678e192019-05-17 11:48:29 -070070## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000071local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
David K. Bainbridge1678e192019-05-17 11:48:29 -070072ifdef LOCAL_PROTOS
David K. Bainbridge41835142021-04-01 17:26:12 +000073 rm -rf vendor/github.com/opencord/voltha-protos/v4/go
Maninderdfadc982020-10-28 14:04:33 +053074 mkdir -p vendor/github.com/opencord/voltha-protos/v4/go
75 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070076endif
77
Scott Bakercb7c88a2019-10-16 18:32:48 -070078## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000079local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Bakercb7c88a2019-10-16 18:32:48 -070080ifdef LOCAL_LIB_GO
David K. Bainbridge41835142021-04-01 17:26:12 +000081 rm -rf vendor/github.com/opencord/voltha-lib-go/v4/pkg
Maninderdfadc982020-10-28 14:04:33 +053082 mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg
83 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070084endif
85
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040086## Docker targets
David K. Bainbridge41835142021-04-01 17:26:12 +000087build: docker-build ## Alias for 'docker-build'
khenaidoocfee5f42018-07-19 22:47:38 -040088
David K. Bainbridge41835142021-04-01 17:26:12 +000089docker-build: local-protos local-lib-go ## Build core docker image (set BUILD_PROFILED=true to also build the profiled image)
90 docker build $(DOCKER_BUILD_ARGS) -t ${RWCORE_IMAGENAME}:${DOCKER_TAG} --target ${DOCKER_TARGET} -f docker/Dockerfile.rw_core .
Matteo Scandolo450933a2020-04-30 07:47:08 -070091ifdef BUILD_PROFILED
Andrea Campanella8d4f3e32021-04-13 10:02:03 +020092# Force target to dev as profile build must be built with dynamic linking
93 docker build $(DOCKER_BUILD_ARGS) --target dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" --build-arg CGO_PARAMETER="CGO_ENABLED=1" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.rw_core .
Matteo Scandolo450933a2020-04-30 07:47:08 -070094endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -070095ifdef BUILD_RACE
Andrea Campanella8d4f3e32021-04-13 10:02:03 +020096# Force target to dev as race detection build must be built with dynamic linking
97 docker build $(DOCKER_BUILD_ARGS) --target dev --build-arg GOLANG_IMAGE=golang:1.13.8-buster --build-arg CGO_PARAMETER="CGO_ENABLED=1" --build-arg DEPLOY_IMAGE=debian:buster-slim --build-arg EXTRA_GO_BUILD_TAGS="--race" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd -f docker/Dockerfile.rw_core .
David K. Bainbridge3768fde2020-07-29 21:15:35 -070098endif
Don Newton8eca4622020-02-10 16:44:48 -050099
David K. Bainbridge41835142021-04-01 17:26:12 +0000100docker-push: ## Push the docker images to an external repository
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400101 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700102ifdef BUILD_PROFILED
103 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
104endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700105ifdef BUILD_RACE
106 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
107endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000108docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800109 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
110 kind load docker-image ${RWCORE_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev)
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800111
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400112## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500113
David K. Bainbridge41835142021-04-01 17:26:12 +0000114lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500115 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500116 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000117 @echo "Dockerfile lint check OK"
118
David K. Bainbridge41835142021-04-01 17:26:12 +0000119lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400120 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500121 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800122 @echo "Dependency check OK. Running vendor check..."
123 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500124 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1)
125 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1)
Kent Hagerman812b2572020-01-28 11:57:51 -0500126 ${GO} mod tidy
127 ${GO} mod vendor
Scott Bakere4c2a982019-11-21 16:32:03 -0800128 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500129 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
130 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
Scott Bakere4c2a982019-11-21 16:32:03 -0800131 @echo "Vendor check OK."
132
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400133
David K. Bainbridge41835142021-04-01 17:26:12 +0000134lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400135
David K. Bainbridge41835142021-04-01 17:26:12 +0000136sca: ## Runs static code analysis with the golangci-lint tool
Kent Hagerman812b2572020-01-28 11:57:51 -0500137 @rm -rf ./sca-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700138 @mkdir -p ./sca-report
Kent Hagerman812b2572020-01-28 11:57:51 -0500139 @echo "Running static code analysis..."
Kent Hagermand9cc2e92019-11-04 13:28:15 -0500140 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Kent Hagerman1b820752020-02-14 14:45:51 -0500141 @echo ""
Kent Hagerman812b2572020-01-28 11:57:51 -0500142 @echo "Static code analysis OK"
Scott Baker18ea5a42019-10-15 16:08:42 -0700143
David K. Bainbridge41835142021-04-01 17:26:12 +0000144test: local-lib-go ## Run unit tests
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400145 @mkdir -p ./tests/results
Kent Hagerman812b2572020-01-28 11:57:51 -0500146 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700147 RETURN=$$? ;\
Kent Hagerman812b2572020-01-28 11:57:51 -0500148 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
149 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700150 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400151
David K. Bainbridge41835142021-04-01 17:26:12 +0000152clean: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700153
David K. Bainbridge41835142021-04-01 17:26:12 +0000154distclean: ## Removes any local filesystem artifacts generated by a build or test run
Kent Hagerman812b2572020-01-28 11:57:51 -0500155 rm -rf ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700156
David K. Bainbridge41835142021-04-01 17:26:12 +0000157mod-update: ## Update go mod files
Kent Hagerman812b2572020-01-28 11:57:51 -0500158 ${GO} mod tidy
159 ${GO} mod vendor
Maninder9a1bc0d2020-10-26 11:34:02 +0530160
David K. Bainbridge41835142021-04-01 17:26:12 +0000161fmt: ## Formats the soure code to go best practice style
Maninder9a1bc0d2020-10-26 11:34:02 +0530162 @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000163
164# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
165help: ## Print help for each Makefile target
166 @echo "Usage: make [<target>]"
167 @echo "where available targets are:"
168 @echo
169 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
170 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'