blob: 41418981dfb7e135b246ad917b51989d8e2ec2aa [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
Joey Armstrong811e9542022-12-25 21:45:27 -050020$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
21
22##--------------------##
23##---] INCLUDES [---##
24##--------------------##
Joey Armstrong367d76b2023-06-08 17:16:46 -040025-include config.mk
26include makefiles/include.mk
Kent Hagerman074d0e02019-04-24 17:58:16 -040027
Zack Williams27f59a42019-05-10 09:12:07 -070028# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040029VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070030
Maninder9a1bc0d2020-10-26 11:34:02 +053031# Packages
32PACKAGES = $(shell go list ./...)
33
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040034DOCKER_LABEL_VCS_DIRTY = false
35ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
36 DOCKER_LABEL_VCS_DIRTY = true
37endif
Joey Armstrong811e9542022-12-25 21:45:27 -050038
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040039## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070040DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040041DOCKER_REGISTRY ?=
42DOCKER_REPOSITORY ?=
43DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge41835142021-04-01 17:26:12 +000044DOCKER_TARGET ?= prod
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040045RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
Andrea Campanella7a6cce22019-12-17 14:10:27 -080046TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040047
48## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040049DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
50DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
51DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
52DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040053
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040054DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070055 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040056 --build-arg org_label_schema_version="${VERSION}" \
57 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
58 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
59 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
60 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
61 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
62
63DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040064 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040065
Kent Hagerman812b2572020-01-28 11:57:51 -050066# tool containers
David K. Bainbridgefc8e1812021-04-09 16:09:49 +000067VOLTHA_TOOLS_VERSION ?= 2.4.0
Kent Hagerman812b2572020-01-28 11:57:51 -050068
Kent Hagerman1b820752020-02-14 14:45:51 -050069GO = 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 -050070GO_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 -080071GOCOVER_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 -050072GOLANGCI_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
73HADOLINT = 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 -050074
David K. Bainbridge41835142021-04-01 17:26:12 +000075.PHONY: docker-build local-protos local-lib-go help
76.DEFAULT_GOAL := help
khenaidoocfee5f42018-07-19 22:47:38 -040077
khenaidoocfee5f42018-07-19 22:47:38 -040078
David K. Bainbridge1678e192019-05-17 11:48:29 -070079## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000080local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
David K. Bainbridge1678e192019-05-17 11:48:29 -070081ifdef LOCAL_PROTOS
khenaidood948f772021-08-11 17:49:24 -040082 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
83 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
84 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070085endif
86
Scott Bakercb7c88a2019-10-16 18:32:48 -070087## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000088local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Bakercb7c88a2019-10-16 18:32:48 -070089ifdef LOCAL_LIB_GO
khenaidood948f772021-08-11 17:49:24 -040090 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
91 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
92 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070093endif
94
Joey Armstrong811e9542022-12-25 21:45:27 -050095## -----------------------------------------------------------------------
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040096## Docker targets
Joey Armstrong811e9542022-12-25 21:45:27 -050097## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +000098build: docker-build ## Alias for 'docker-build'
khenaidoocfee5f42018-07-19 22:47:38 -040099
Joey Armstrong811e9542022-12-25 21:45:27 -0500100docker-build-args += --debug
101docker-build-args += --log-level 'debug'
102docker-build-args := $(null)# comment line for debug mode
103
David K. Bainbridge41835142021-04-01 17:26:12 +0000104docker-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 -0500105 @echo "======================================================================="
106 @echo " ** TARGET: $@"
107 @echo "======================================================================="
108 $(MAKE) --no-print-directory init-test-results
109
110 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 -0700111ifdef BUILD_PROFILED
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200112# Force target to dev as profile build must be built with dynamic linking
113 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 -0700114endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700115ifdef BUILD_RACE
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200116# Force target to dev as race detection build must be built with dynamic linking
117 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 -0700118endif
Don Newton8eca4622020-02-10 16:44:48 -0500119
Joey Armstrong811e9542022-12-25 21:45:27 -0500120## -----------------------------------------------------------------------
Joey Armstrong5f51f2e2023-01-17 17:06:26 -0500121## Intent:
Joey Armstrong811e9542022-12-25 21:45:27 -0500122## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000123docker-push: ## Push the docker images to an external repository
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400124 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700125ifdef BUILD_PROFILED
126 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
127endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700128ifdef BUILD_RACE
129 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
130endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000131docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800132 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
133 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 -0800134
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400135## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500136
David K. Bainbridge41835142021-04-01 17:26:12 +0000137lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500138 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500139 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000140 @echo "Dockerfile lint check OK"
141
David K. Bainbridge41835142021-04-01 17:26:12 +0000142lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400143 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500144 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800145 @echo "Dependency check OK. Running vendor check..."
146 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500147 @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)
148 @[[ `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 -0500149 ${GO} mod tidy
Joey Armstrong811e9542022-12-25 21:45:27 -0500150
151 # This command is destructive, vendor/ removed
Kent Hagerman812b2572020-01-28 11:57:51 -0500152 ${GO} mod vendor
Joey Armstrong811e9542022-12-25 21:45:27 -0500153
Scott Bakere4c2a982019-11-21 16:32:03 -0800154 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500155 @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)
156 @[[ `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 -0800157 @echo "Vendor check OK."
158
Joey Armstrong811e9542022-12-25 21:45:27 -0500159## -----------------------------------------------------------------------
160## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000161lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400162
David K. Bainbridge41835142021-04-01 17:26:12 +0000163sca: ## Runs static code analysis with the golangci-lint tool
Joey Armstrong811e9542022-12-25 21:45:27 -0500164 @$(RM) -r ./sca-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700165 @mkdir -p ./sca-report
Kent Hagerman812b2572020-01-28 11:57:51 -0500166 @echo "Running static code analysis..."
Joey Armstrong811e9542022-12-25 21:45:27 -0500167 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... \
168 | tee ./sca-report/sca-report.xml
Kent Hagerman1b820752020-02-14 14:45:51 -0500169 @echo ""
Kent Hagerman812b2572020-01-28 11:57:51 -0500170 @echo "Static code analysis OK"
Scott Baker18ea5a42019-10-15 16:08:42 -0700171
Joey Armstrong811e9542022-12-25 21:45:27 -0500172## -----------------------------------------------------------------------
173## -----------------------------------------------------------------------
174tests-dir := ./tests/results
175tests-coverage := $(tests-dir)/go-test-coverage
176tests-results := $(tests-dir)/go-test-results
David K. Bainbridge41835142021-04-01 17:26:12 +0000177test: local-lib-go ## Run unit tests
Joey Armstrong811e9542022-12-25 21:45:27 -0500178 $(MAKE) --no-print-directory init-test-results
179 $(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 -0700180 RETURN=$$? ;\
Joey Armstrong811e9542022-12-25 21:45:27 -0500181 ${GO_JUNIT_REPORT} < $(tests-results).out > $(tests-results).xml ;\
182 ${GOCOVER_COBERTURA} < $(tests-coverage).out > $(tests-coverage).xml ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700183 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400184
Joey Armstrong367d76b2023-06-08 17:16:46 -0400185clean :: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700186
Joey Armstrong367d76b2023-06-08 17:16:46 -0400187distclean sterile :: ## Removes any local filesystem artifacts generated by a build or test run
Joey Armstrong811e9542022-12-25 21:45:27 -0500188 $(RM) -r ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700189
David K. Bainbridge41835142021-04-01 17:26:12 +0000190mod-update: ## Update go mod files
Kent Hagerman812b2572020-01-28 11:57:51 -0500191 ${GO} mod tidy
192 ${GO} mod vendor
Maninder9a1bc0d2020-10-26 11:34:02 +0530193
David K. Bainbridge41835142021-04-01 17:26:12 +0000194fmt: ## Formats the soure code to go best practice style
Maninder9a1bc0d2020-10-26 11:34:02 +0530195 @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000196
Joey Armstrong811e9542022-12-25 21:45:27 -0500197## ---------------------------------------------------------------------------
198## Intent: Prep work, test -coverprofile fails w/o a file on disk ?!?
199## ---------------------------------------------------------------------------
200init-test-results:
201 @$(RM) -r tests/results
202 @mkdir -p tests/results
203 @touch $(tests-coverage).out
204
205## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000206# 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 -0500207## ---------------------------------------------------------------------------
Joey Armstrong367d76b2023-06-08 17:16:46 -0400208help :: ## Print help for each Makefile target
David K. Bainbridge41835142021-04-01 17:26:12 +0000209 @echo "Usage: make [<target>]"
Joey Armstrong811e9542022-12-25 21:45:27 -0500210 @echo " help This message"
211 @echo " todo Future enhancements"
212 @echo " versions Display version-by-tool used while building"
213 ifdef VERBOSE
David K. Bainbridge41835142021-04-01 17:26:12 +0000214 @echo
Joey Armstrong811e9542022-12-25 21:45:27 -0500215 @echo " init-test-results Massage tests/results to fix coverage reporting"
216 endif
217 @echo
218 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
219 | sort \
220 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
221
222# [EOF]