blob: 1b07af8fc445b397fd91445e0881bb637dec665e [file] [log] [blame]
Joey Armstrong811e9542022-12-25 21:45:27 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------#
3# Copyright 2016-2023 Open Networking Foundation (ONF) and the ONF Contributors
4
khenaidoocfee5f42018-07-19 22:47:38 -04005# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joey Armstrong811e9542022-12-25 21:45:27 -050016# -----------------------------------------------------------------------
khenaidoocfee5f42018-07-19 22:47:38 -040017
Joey Armstrong811e9542022-12-25 21:45:27 -050018.DEFAULT_GOAL := help
19
20TOP ?= .
21MAKEDIR ?= $(TOP)/makefiles
22
23$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
24
25##--------------------##
26##---] INCLUDES [---##
27##--------------------##
28include $(MAKEDIR)/include.mk
Kent Hagerman074d0e02019-04-24 17:58:16 -040029
Zack Williams27f59a42019-05-10 09:12:07 -070030# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040031VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070032
Maninder9a1bc0d2020-10-26 11:34:02 +053033# Packages
34PACKAGES = $(shell go list ./...)
35
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040036DOCKER_LABEL_VCS_DIRTY = false
37ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
38 DOCKER_LABEL_VCS_DIRTY = true
39endif
Joey Armstrong811e9542022-12-25 21:45:27 -050040
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040041## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070042DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040043DOCKER_REGISTRY ?=
44DOCKER_REPOSITORY ?=
45DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge41835142021-04-01 17:26:12 +000046DOCKER_TARGET ?= prod
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040047RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
Andrea Campanella7a6cce22019-12-17 14:10:27 -080048TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040049
50## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040051DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
52DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
53DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
54DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040055
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040056DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070057 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040058 --build-arg org_label_schema_version="${VERSION}" \
59 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
60 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
61 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
62 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
63 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
64
65DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040066 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040067
Kent Hagerman812b2572020-01-28 11:57:51 -050068# tool containers
David K. Bainbridgefc8e1812021-04-09 16:09:49 +000069VOLTHA_TOOLS_VERSION ?= 2.4.0
Kent Hagerman812b2572020-01-28 11:57:51 -050070
Kent Hagerman1b820752020-02-14 14:45:51 -050071GO = 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 -050072GO_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 -080073GOCOVER_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 -050074GOLANGCI_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
75HADOLINT = 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 -050076
David K. Bainbridge41835142021-04-01 17:26:12 +000077.PHONY: docker-build local-protos local-lib-go help
78.DEFAULT_GOAL := help
khenaidoocfee5f42018-07-19 22:47:38 -040079
khenaidoocfee5f42018-07-19 22:47:38 -040080
David K. Bainbridge1678e192019-05-17 11:48:29 -070081## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000082local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
David K. Bainbridge1678e192019-05-17 11:48:29 -070083ifdef LOCAL_PROTOS
khenaidood948f772021-08-11 17:49:24 -040084 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
85 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
86 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070087endif
88
Scott Bakercb7c88a2019-10-16 18:32:48 -070089## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000090local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Bakercb7c88a2019-10-16 18:32:48 -070091ifdef LOCAL_LIB_GO
khenaidood948f772021-08-11 17:49:24 -040092 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
93 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
94 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070095endif
96
Joey Armstrong811e9542022-12-25 21:45:27 -050097## -----------------------------------------------------------------------
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040098## Docker targets
Joey Armstrong811e9542022-12-25 21:45:27 -050099## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000100build: docker-build ## Alias for 'docker-build'
khenaidoocfee5f42018-07-19 22:47:38 -0400101
Joey Armstrong811e9542022-12-25 21:45:27 -0500102docker-build-args += --debug
103docker-build-args += --log-level 'debug'
104docker-build-args := $(null)# comment line for debug mode
105
David K. Bainbridge41835142021-04-01 17:26:12 +0000106docker-build: local-protos local-lib-go ## Build core docker image (set BUILD_PROFILED=true to also build the profiled image)
Joey Armstrong811e9542022-12-25 21:45:27 -0500107 @echo "======================================================================="
108 @echo " ** TARGET: $@"
109 @echo "======================================================================="
110 $(MAKE) --no-print-directory init-test-results
111
112 docker $(docker-build-args) build $(DOCKER_BUILD_ARGS) -t ${RWCORE_IMAGENAME}:${DOCKER_TAG} --target ${DOCKER_TARGET} -f docker/Dockerfile.rw_core .
Matteo Scandolo450933a2020-04-30 07:47:08 -0700113ifdef BUILD_PROFILED
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200114# Force target to dev as profile build must be built with dynamic linking
115 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 -0700116endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700117ifdef BUILD_RACE
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200118# Force target to dev as race detection build must be built with dynamic linking
119 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 -0700120endif
Don Newton8eca4622020-02-10 16:44:48 -0500121
Joey Armstrong811e9542022-12-25 21:45:27 -0500122## -----------------------------------------------------------------------
123## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000124docker-push: ## Push the docker images to an external repository
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400125 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700126ifdef BUILD_PROFILED
127 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
128endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700129ifdef BUILD_RACE
130 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
131endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000132docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800133 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
134 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 -0800135
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400136## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500137
David K. Bainbridge41835142021-04-01 17:26:12 +0000138lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500139 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500140 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000141 @echo "Dockerfile lint check OK"
142
David K. Bainbridge41835142021-04-01 17:26:12 +0000143lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400144 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500145 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800146 @echo "Dependency check OK. Running vendor check..."
147 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500148 @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)
149 @[[ `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 -0500150 ${GO} mod tidy
Joey Armstrong811e9542022-12-25 21:45:27 -0500151
152 # This command is destructive, vendor/ removed
Kent Hagerman812b2572020-01-28 11:57:51 -0500153 ${GO} mod vendor
Joey Armstrong811e9542022-12-25 21:45:27 -0500154
Scott Bakere4c2a982019-11-21 16:32:03 -0800155 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500156 @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)
157 @[[ `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 -0800158 @echo "Vendor check OK."
159
Joey Armstrong811e9542022-12-25 21:45:27 -0500160## -----------------------------------------------------------------------
161## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000162lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400163
David K. Bainbridge41835142021-04-01 17:26:12 +0000164sca: ## Runs static code analysis with the golangci-lint tool
Joey Armstrong811e9542022-12-25 21:45:27 -0500165 @$(RM) -r ./sca-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700166 @mkdir -p ./sca-report
Kent Hagerman812b2572020-01-28 11:57:51 -0500167 @echo "Running static code analysis..."
Joey Armstrong811e9542022-12-25 21:45:27 -0500168 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... \
169 | tee ./sca-report/sca-report.xml
Kent Hagerman1b820752020-02-14 14:45:51 -0500170 @echo ""
Kent Hagerman812b2572020-01-28 11:57:51 -0500171 @echo "Static code analysis OK"
Scott Baker18ea5a42019-10-15 16:08:42 -0700172
Joey Armstrong811e9542022-12-25 21:45:27 -0500173## -----------------------------------------------------------------------
174## -----------------------------------------------------------------------
175tests-dir := ./tests/results
176tests-coverage := $(tests-dir)/go-test-coverage
177tests-results := $(tests-dir)/go-test-results
David K. Bainbridge41835142021-04-01 17:26:12 +0000178test: local-lib-go ## Run unit tests
Joey Armstrong811e9542022-12-25 21:45:27 -0500179 $(MAKE) --no-print-directory init-test-results
180 $(HIDE)${GO} test -mod=vendor -v -coverprofile $(tests-coverage).out -covermode count ./... 2>&1 | tee $(tests-results).out ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700181 RETURN=$$? ;\
Joey Armstrong811e9542022-12-25 21:45:27 -0500182 ${GO_JUNIT_REPORT} < $(tests-results).out > $(tests-results).xml ;\
183 ${GOCOVER_COBERTURA} < $(tests-coverage).out > $(tests-coverage).xml ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700184 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400185
David K. Bainbridge41835142021-04-01 17:26:12 +0000186clean: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700187
Joey Armstrong811e9542022-12-25 21:45:27 -0500188distclean sterile: ## Removes any local filesystem artifacts generated by a build or test run
189 $(RM) -r ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700190
David K. Bainbridge41835142021-04-01 17:26:12 +0000191mod-update: ## Update go mod files
Kent Hagerman812b2572020-01-28 11:57:51 -0500192 ${GO} mod tidy
193 ${GO} mod vendor
Maninder9a1bc0d2020-10-26 11:34:02 +0530194
David K. Bainbridge41835142021-04-01 17:26:12 +0000195fmt: ## Formats the soure code to go best practice style
Maninder9a1bc0d2020-10-26 11:34:02 +0530196 @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000197
Joey Armstrong811e9542022-12-25 21:45:27 -0500198## ---------------------------------------------------------------------------
199## Intent: Prep work, test -coverprofile fails w/o a file on disk ?!?
200## ---------------------------------------------------------------------------
201init-test-results:
202 @$(RM) -r tests/results
203 @mkdir -p tests/results
204 @touch $(tests-coverage).out
205
206## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000207# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
Joey Armstrong811e9542022-12-25 21:45:27 -0500208## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000209help: ## Print help for each Makefile target
210 @echo "Usage: make [<target>]"
Joey Armstrong811e9542022-12-25 21:45:27 -0500211 @echo " help This message"
212 @echo " todo Future enhancements"
213 @echo " versions Display version-by-tool used while building"
214 ifdef VERBOSE
David K. Bainbridge41835142021-04-01 17:26:12 +0000215 @echo
Joey Armstrong811e9542022-12-25 21:45:27 -0500216 @echo " init-test-results Massage tests/results to fix coverage reporting"
217 endif
218 @echo
219 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
220 | sort \
221 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
222
223# [EOF]