blob: 0bd7c9f30f2706fcd21d7519ae3a8cc4fff26a91 [file] [log] [blame]
Joey Armstrongf9bffdf2022-12-27 07:05:28 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2016-2023 Open Networking Foundation (ONF) and the ONF Contributors
Phaneendra Manda4c62c802019-03-06 21:37:49 +05304#
5# 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 Armstrongf9bffdf2022-12-27 07:05:28 -050016# -----------------------------------------------------------------------
Phaneendra Manda4c62c802019-03-06 21:37:49 +053017
Joey Armstrongf9bffdf2022-12-27 07:05:28 -050018$(if $(DEBUG),$(warning ENTER))
19
20.DEFAULT_GOAL := help
21
22TOP ?= .
23MAKEDIR ?= $(TOP)/makefiles
24
25$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
26
27##--------------------##
28##---] INCLUDES [---##
29##--------------------##
30include $(MAKEDIR)/include.mk
31ifdef LOCAL_LINT
32 include $(MAKEDIR)/lint/golang/sca.mk
33endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040034
35# Variables
36VERSION ?= $(shell cat ./VERSION)
37
Matt Jeanneretf880eb62019-07-16 20:08:03 -040038DOCKER_LABEL_VCS_DIRTY = false
39ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
40 DOCKER_LABEL_VCS_DIRTY = true
41endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040042## Docker related
Matt Jeanneretf880eb62019-07-16 20:08:03 -040043DOCKER_EXTRA_ARGS ?=
Matt Jeanneret8b823f62019-05-11 11:01:28 -040044DOCKER_REGISTRY ?=
45DOCKER_REPOSITORY ?=
David K. Bainbridge96b06512021-04-01 18:53:51 +000046DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040047ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG}
David K. Bainbridge96b06512021-04-01 18:53:51 +000048DOCKER_TARGET ?= prod
Andrea Campanellacbf583e2019-12-17 15:26:00 -080049TYPE ?= minimal
Matt Jeanneret8b823f62019-05-11 11:01:28 -040050
51## Docker labels. Only set ref and commit date if committed
Matt Jeanneretf880eb62019-07-16 20:08:03 -040052DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
53DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
54DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
55DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret8b823f62019-05-11 11:01:28 -040056
Matt Jeanneretf880eb62019-07-16 20:08:03 -040057DOCKER_BUILD_ARGS ?= \
58 ${DOCKER_EXTRA_ARGS} \
59 --build-arg org_label_schema_version="${VERSION}" \
60 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
61 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
62 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
63 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
64 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
65
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050066# tool containers
David K. Bainbridge2f2658d2021-04-09 16:13:57 +000067VOLTHA_TOOLS_VERSION ?= 2.4.0
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050068
Kent Hagerman52830082020-02-14 14:53:52 -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 Hagerman2cd4b7b2020-02-04 15:15:14 -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 Scandolod5c22402020-11-23 14:28:37 -080071GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openolt-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Kent Hagerman52830082020-02-14 14:53:52 -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 Hagerman2cd4b7b2020-02-04 15:15:14 -050074
Zack Williams75662252020-04-06 18:16:54 -070075.PHONY: docker-build local-protos local-lib-go help
Phaneendra Manda4c62c802019-03-06 21:37:49 +053076
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040077## Local Development Helpers
Zack Williams75662252020-04-06 18:16:54 -070078local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
William Kurkianea869482019-04-09 15:16:11 -040079ifdef LOCAL_PROTOS
Joey Armstronga6af1522023-01-17 16:06:16 -050080 $(RM) -r vendor/github.com/opencord/voltha-protos/v5/go
khenaidoo106c61a2021-08-11 18:05:46 -040081 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
82 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
Joey Armstronga6af1522023-01-17 16:06:16 -050083 $(RM) -r vendor/github.com/opencord/voltha-protos/v5/go/vendor
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040084endif
85
Zack Williams75662252020-04-06 18:16:54 -070086local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Baker355d1742019-10-24 10:57:52 -070087ifdef LOCAL_LIB_GO
khenaidoo106c61a2021-08-11 18:05:46 -040088 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
89 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
William Kurkianea869482019-04-09 15:16:11 -040090endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040091
Joey Armstrongf9bffdf2022-12-27 07:05:28 -050092## -----------------------------------------------------------------------
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040093## Docker targets
Joey Armstrongf9bffdf2022-12-27 07:05:28 -050094## -----------------------------------------------------------------------
Zack Williams75662252020-04-06 18:16:54 -070095build: docker-build ## Alias for 'docker build'
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040096
Joey Armstrongf9bffdf2022-12-27 07:05:28 -050097## -----------------------------------------------------------------------
98## -----------------------------------------------------------------------
Matteo Scandolo6ec3c352020-04-29 15:27:03 -070099docker-build: local-protos local-lib-go ## Build openolt adapter docker image (set BUILD_PROFILED=true to also build the profiled image)
David K. Bainbridgee2d9b332021-04-15 00:11:51 +0000100 docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg CGO_PARAMETER=0 -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt .
Matteo Scandolo6ec3c352020-04-29 15:27:03 -0700101ifdef BUILD_PROFILED
David K. Bainbridgee2d9b332021-04-15 00:11:51 +0000102 docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openolt .
103endif
104ifdef BUILD_RACE
105 docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-race" -t ${ADAPTER_IMAGENAME}-rd -f docker/Dockerfile.openolt .
Matteo Scandolo6ec3c352020-04-29 15:27:03 -0700106endif
Matteo Scandolo8f2b9572020-02-28 15:35:23 -0800107
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500108## -----------------------------------------------------------------------
109## -----------------------------------------------------------------------
Zack Williams75662252020-04-06 18:16:54 -0700110docker-push: ## Push the docker images to an external repository
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400111 docker push ${ADAPTER_IMAGENAME}
Matteo Scandolo24ed9672020-05-04 10:53:08 -0700112ifdef BUILD_PROFILED
113 docker push ${ADAPTER_IMAGENAME}-profile
114endif
David K. Bainbridgee2d9b332021-04-15 00:11:51 +0000115ifdef BUILD_RACE
116 docker push ${ADAPTER_IMAGENAME}-rd
117endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400118
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500119## -----------------------------------------------------------------------
120## -----------------------------------------------------------------------
Zack Williams75662252020-04-06 18:16:54 -0700121docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanellacbf583e2019-12-17 15:26:00 -0800122 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
123 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
124
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500125## -----------------------------------------------------------------------
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400126## lint and unit tests
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500127## -----------------------------------------------------------------------
Zack Williams75662252020-04-06 18:16:54 -0700128lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500129 @echo "Running Dockerfile lint check ..."
130 @${HADOLINT} $$(find . -name "Dockerfile.*")
131 @echo "Dockerfile lint check OK"
132
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500133## -----------------------------------------------------------------------
134## -----------------------------------------------------------------------
Zack Williams75662252020-04-06 18:16:54 -0700135lint-mod: ## Verify the Go dependencies
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400136 @echo "Running dependency check..."
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500137 @${GO} mod verify
Scott Baker22c0e9c2019-11-26 08:11:29 -0800138 @echo "Dependency check OK. Running vendor check..."
139 @git status > /dev/null
Kent Hagermane71b52d2020-02-26 11:13:42 -0500140 @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)
141 @[[ `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 Hagerman2cd4b7b2020-02-04 15:15:14 -0500142 ${GO} mod tidy
143 ${GO} mod vendor
Scott Baker22c0e9c2019-11-26 08:11:29 -0800144 @git status > /dev/null
Kent Hagermane71b52d2020-02-26 11:13:42 -0500145 @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)
146 @[[ `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 Baker22c0e9c2019-11-26 08:11:29 -0800147 @echo "Vendor check OK."
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400148
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500149## -----------------------------------------------------------------------
150## -----------------------------------------------------------------------
Joey Armstronga6af1522023-01-17 16:06:16 -0500151lint: local-lib-go lint-mod lint-dockerfile
David Bainbridge788e5202019-10-21 18:49:40 +0000152
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500153## -----------------------------------------------------------------------
154## -----------------------------------------------------------------------
Zack Williams75662252020-04-06 18:16:54 -0700155test: ## Run unit tests
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400156 @mkdir -p ./tests/results
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500157 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400158 RETURN=$$? ;\
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500159 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
160 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400161 exit $$RETURN
162
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500163## -----------------------------------------------------------------------
164## -----------------------------------------------------------------------
Joey Armstronga6af1522023-01-17 16:06:16 -0500165sca:
166 @$(RM) -r ./sca-report
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 @mkdir -p ./sca-report
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500168 @echo "Running static code analysis..."
Joey Armstronga6af1522023-01-17 16:06:16 -0500169 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... \
170 | tee ./sca-report/sca-report.xml
Kent Hagermane71b52d2020-02-26 11:13:42 -0500171 @echo ""
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500172 @echo "Static code analysis OK"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700173
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500174## -----------------------------------------------------------------------
175## -----------------------------------------------------------------------
Joey Armstronga6af1522023-01-17 16:06:16 -0500176clean: distclean
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400177
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500178## -----------------------------------------------------------------------
179## -----------------------------------------------------------------------
Joey Armstronga6af1522023-01-17 16:06:16 -0500180distclean:
181 $(RM) -r ./sca-report
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400182
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500183## -----------------------------------------------------------------------
184## -----------------------------------------------------------------------
Joey Armstronga6af1522023-01-17 16:06:16 -0500185mod-update:
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500186 ${GO} mod tidy
187 ${GO} mod vendor
Zack Williams75662252020-04-06 18:16:54 -0700188
Joey Armstrongf9bffdf2022-12-27 07:05:28 -0500189# [EOF]