blob: 2a962ddbbaca934b32f9485fdbc622a20c6df4c2 [file] [log] [blame]
Joey Armstrongec895f82024-04-25 14:26:07 -04001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2022-2024 Open Networking Foundation Contributors
4#
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.
16# -----------------------------------------------------------------------
17# SPDX-FileCopyrightText: 2022-2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
20# Intent: Build, test and release voltha-go-controller
21# -----------------------------------------------------------------------
22# Todo: Refactor common makefile logic and targets shared by
23# several VOLTHA repositories.
24# -----------------------------------------------------------------------
25
Naveen Sampath04696f72022-06-13 15:19:14 +053026DOCKER_NAME := voltha-go-controller
abhay83672ba2025-01-06 19:06:13 +053027DOCKER_TAG := master
Naveen Sampath04696f72022-06-13 15:19:14 +053028GOLINT := golint
29GOCMD = 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
30GOBUILD=$(GOCMD) build
31GOCLEAN=$(GOCMD) clean
32GOTEST=$(GOCMD) test
33GOTOOL=$(GOCMD) tool
34
35# Static code analyzers
36GOIMPORTS := goimports
37GO_FILES := `find . -path vendor -prune -o -name '*.go'`
38STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS)
39#STATIC_TOOLS := $(GOFMT)
Matteo Scandolo9061e432022-05-10 13:56:29 -070040
41# set default shell
42SHELL = bash -e -o pipefail
43
44# Variables
Naveen Sampath04696f72022-06-13 15:19:14 +053045VERSION ?= $(shell head -n 1 ./VERSION)
46# DOCKER_TAG ?= ${VERSION}
47IMAGENAME ?= ${DOCKER_NAME}:${DOCKER_TAG}
Matteo Scandolo9061e432022-05-10 13:56:29 -070048
Naveen Sampath04696f72022-06-13 15:19:14 +053049COVERAGE_DIR = ./tests/results
50COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out
51TEST_TARGETS := test-default test-verbose test-short
52test-short: ARGS=-short
53test-verbose: ARGS=-v
Matteo Scandolo9061e432022-05-10 13:56:29 -070054# tool containers
55VOLTHA_TOOLS_VERSION ?= 2.4.0
56
Matteo Scandolo9061e432022-05-10 13:56:29 -070057HADOLINT = 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
Naveen Sampath04696f72022-06-13 15:19:14 +053058GOLANGCI_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
59GO_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
60GOCOVER_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
61
62## Local Development Helpers
63local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
64ifdef LOCAL_PROTOS
65 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
66 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
67 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
68 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
69endif
70
71local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
72ifdef LOCAL_LIB_GO
73 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
74 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
75endif
76
77# This should to be the first and default target in this Makefile
Joey Armstrongec895f82024-04-25 14:26:07 -040078help :: ## Print help for each Makefile target
Naveen Sampath04696f72022-06-13 15:19:14 +053079 @echo "Usage: make [<target>]"
80 @echo "where available targets are:"
81 @echo
82 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
83 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
84
85
86exe:
87 @echo Building voltha-go-controller....
88 @cd voltha-go-controller && go build
89
90vgcctl:
91 @echo Building vgcctl....
92 @cd voltha-go-controller/cli && go build -o vgcctl
93
abhay55a8b262025-01-06 18:22:24 +053094docker-build:
95 @echo Building Docker $(DOCKER_NAME)....
96 sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
abhay41e845e2025-01-06 15:54:18 +053097
abhay83672ba2025-01-06 19:06:13 +053098docker-push: ## Push the docker images to an external repository
99 docker push ${IMAGENAME}
100ifdef BUILD_PROFILED
101 docker push ${IMAGENAME}-profile
102endif
103ifdef BUILD_RACE
104 docker push ${IMAGENAME}-rd
105endif
106
Naveen Sampath04696f72022-06-13 15:19:14 +0530107docker: exe vgcctl
108 @echo Building Docker $(DOCKER_NAME)....
109 sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
110
111## Docker targets
112build: local-protos local-lib-go docker ## Build voltha-go-controller image
113
114build-docker-profile: sca exe-profile vgcctl
115 @echo Building Docker $(DOCKER_NAME)....
116 sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller .
117
118sca: ## Runs static code analysis with the golangci-lint tool
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +0530119 @rm -rf ./sca-report
120 @mkdir -p ./sca-report
Naveen Sampath04696f72022-06-13 15:19:14 +0530121 @echo "Running static code analysis..."
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +0530122 @${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
123 @echo ""
124 @echo "Static code analysis OK"
Naveen Sampath04696f72022-06-13 15:19:14 +0530125
Joey Armstrongec895f82024-04-25 14:26:07 -0400126clean :: ## Removes any local filesystem artifacts generated by a build
Naveen Sampath04696f72022-06-13 15:19:14 +0530127 rm -f voltha-go-controller/voltha-go-controller
128 rm -f voltha-go-controller/cli/vgcctl
129
130mock-gen:
131 mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
Matteo Scandolo9061e432022-05-10 13:56:29 -0700132
133## lint and unit tests
Naveen Sampath04696f72022-06-13 15:19:14 +0530134lint-dockerfile: ## Perform static analysis on Dockerfile
135 @echo "Running Dockerfile lint check ..."
136 @${HADOLINT} $$(find . -name "Dockerfile.*")
137 @echo "Dockerfile lint check OK"
Matteo Scandolo9061e432022-05-10 13:56:29 -0700138
Naveen Sampath04696f72022-06-13 15:19:14 +0530139lint-mod: ## Verify the Go dependencies
140 @echo "Running dependency check..."
141 @${GOCMD} mod verify
142 @echo "Dependency check OK. Running vendor check..."
143 @git status > /dev/null
144 @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)
145 @[[ `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)
146 ${GOCMD} mod tidy
147 ${GOCMD} mod vendor
148 @git status > /dev/null
149 @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)
150 @[[ `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)
151 @echo "Vendor check OK."
Matteo Scandolo9061e432022-05-10 13:56:29 -0700152
Naveen Sampath04696f72022-06-13 15:19:14 +0530153lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
Matteo Scandolo9061e432022-05-10 13:56:29 -0700154
Naveen Sampath04696f72022-06-13 15:19:14 +0530155mod-update: ## Update go mod files
156 ${GOCMD} mod tidy
157 ${GOCMD} mod vendor
158
159test: ## Run unit tests
160 @mkdir -p ./tests/results
161 @${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
162 RETURN=$$? ;\
163 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
164 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
165 exit $$RETURN
Joey Armstrongec895f82024-04-25 14:26:07 -0400166
167## -----------------------------------------------------------------------
168## -----------------------------------------------------------------------
169pre-commit : .venv
170 source .venv/bin/activate && pre-commit
171
172## -----------------------------------------------------------------------
173## [TODO] Replace inlined target with repo:onf-make/makefiles/virtualenv/
174## -----------------------------------------------------------------------
175venv : .venv
176.venv :
177 virtualenv -p python3 $@
178 $@/bin/pip install -r requirements.txt
179
180## -----------------------------------------------------------------------
181## -----------------------------------------------------------------------
182help ::
183 @printf ' %-33.33s %s\n' 'pre-commit' \
184 'Invoke the pre-commit hook linting tool'
185
186clean ::
187 $(RM) -r .venv
188
189# [EOF]