blob: aa9d833b6a827ef2721b190d52126ad8544f4bd8 [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## -----------------------------------------------------------------------
Joey Armstrong5f51f2e2023-01-17 17:06:26 -0500123## Intent:
Joey Armstrong811e9542022-12-25 21:45:27 -0500124## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000125docker-push: ## Push the docker images to an external repository
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400126 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700127ifdef BUILD_PROFILED
128 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
129endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700130ifdef BUILD_RACE
131 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
132endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000133docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800134 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
135 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 -0800136
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400137## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500138
David K. Bainbridge41835142021-04-01 17:26:12 +0000139lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500140 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500141 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000142 @echo "Dockerfile lint check OK"
143
David K. Bainbridge41835142021-04-01 17:26:12 +0000144lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400145 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500146 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800147 @echo "Dependency check OK. Running vendor check..."
148 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500149 @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)
150 @[[ `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 -0500151 ${GO} mod tidy
Joey Armstrong811e9542022-12-25 21:45:27 -0500152
153 # This command is destructive, vendor/ removed
Kent Hagerman812b2572020-01-28 11:57:51 -0500154 ${GO} mod vendor
Joey Armstrong811e9542022-12-25 21:45:27 -0500155
Scott Bakere4c2a982019-11-21 16:32:03 -0800156 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500157 @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)
158 @[[ `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 -0800159 @echo "Vendor check OK."
160
Joey Armstrong811e9542022-12-25 21:45:27 -0500161## -----------------------------------------------------------------------
162## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000163lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400164
David K. Bainbridge41835142021-04-01 17:26:12 +0000165sca: ## Runs static code analysis with the golangci-lint tool
Joey Armstrong811e9542022-12-25 21:45:27 -0500166 @$(RM) -r ./sca-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700167 @mkdir -p ./sca-report
Kent Hagerman812b2572020-01-28 11:57:51 -0500168 @echo "Running static code analysis..."
Joey Armstrong811e9542022-12-25 21:45:27 -0500169 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... \
170 | tee ./sca-report/sca-report.xml
Kent Hagerman1b820752020-02-14 14:45:51 -0500171 @echo ""
Kent Hagerman812b2572020-01-28 11:57:51 -0500172 @echo "Static code analysis OK"
Scott Baker18ea5a42019-10-15 16:08:42 -0700173
Joey Armstrong811e9542022-12-25 21:45:27 -0500174## -----------------------------------------------------------------------
175## -----------------------------------------------------------------------
176tests-dir := ./tests/results
177tests-coverage := $(tests-dir)/go-test-coverage
178tests-results := $(tests-dir)/go-test-results
David K. Bainbridge41835142021-04-01 17:26:12 +0000179test: local-lib-go ## Run unit tests
Joey Armstrong811e9542022-12-25 21:45:27 -0500180 $(MAKE) --no-print-directory init-test-results
181 $(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 -0700182 RETURN=$$? ;\
Joey Armstrong811e9542022-12-25 21:45:27 -0500183 ${GO_JUNIT_REPORT} < $(tests-results).out > $(tests-results).xml ;\
184 ${GOCOVER_COBERTURA} < $(tests-coverage).out > $(tests-coverage).xml ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700185 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400186
David K. Bainbridge41835142021-04-01 17:26:12 +0000187clean: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700188
Joey Armstrong811e9542022-12-25 21:45:27 -0500189distclean sterile: ## Removes any local filesystem artifacts generated by a build or test run
190 $(RM) -r ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700191
David K. Bainbridge41835142021-04-01 17:26:12 +0000192mod-update: ## Update go mod files
Kent Hagerman812b2572020-01-28 11:57:51 -0500193 ${GO} mod tidy
194 ${GO} mod vendor
Maninder9a1bc0d2020-10-26 11:34:02 +0530195
David K. Bainbridge41835142021-04-01 17:26:12 +0000196fmt: ## Formats the soure code to go best practice style
Maninder9a1bc0d2020-10-26 11:34:02 +0530197 @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000198
Joey Armstrong811e9542022-12-25 21:45:27 -0500199## ---------------------------------------------------------------------------
200## Intent: Prep work, test -coverprofile fails w/o a file on disk ?!?
201## ---------------------------------------------------------------------------
202init-test-results:
203 @$(RM) -r tests/results
204 @mkdir -p tests/results
205 @touch $(tests-coverage).out
206
207## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000208# 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 -0500209## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000210help: ## Print help for each Makefile target
211 @echo "Usage: make [<target>]"
Joey Armstrong811e9542022-12-25 21:45:27 -0500212 @echo " help This message"
213 @echo " todo Future enhancements"
214 @echo " versions Display version-by-tool used while building"
215 ifdef VERBOSE
David K. Bainbridge41835142021-04-01 17:26:12 +0000216 @echo
Joey Armstrong811e9542022-12-25 21:45:27 -0500217 @echo " init-test-results Massage tests/results to fix coverage reporting"
218 endif
219 @echo
220 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
221 | sort \
222 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
223
224# [EOF]