blob: 376b7e07e8d34c0916c2d48f478b6a3dcf3af5ec [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
27DOCKER_TAG := latest
28GOLINT := 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
abhay41e845e2025-01-06 15:54:18 +053094docker-build: exe vgcctl
95
Naveen Sampath04696f72022-06-13 15:19:14 +053096docker: exe vgcctl
97 @echo Building Docker $(DOCKER_NAME)....
98 sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
99
100## Docker targets
101build: local-protos local-lib-go docker ## Build voltha-go-controller image
102
103build-docker-profile: sca exe-profile vgcctl
104 @echo Building Docker $(DOCKER_NAME)....
105 sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller .
106
107sca: ## Runs static code analysis with the golangci-lint tool
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +0530108 @rm -rf ./sca-report
109 @mkdir -p ./sca-report
Naveen Sampath04696f72022-06-13 15:19:14 +0530110 @echo "Running static code analysis..."
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +0530111 @${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
112 @echo ""
113 @echo "Static code analysis OK"
Naveen Sampath04696f72022-06-13 15:19:14 +0530114
Joey Armstrongec895f82024-04-25 14:26:07 -0400115clean :: ## Removes any local filesystem artifacts generated by a build
Naveen Sampath04696f72022-06-13 15:19:14 +0530116 rm -f voltha-go-controller/voltha-go-controller
117 rm -f voltha-go-controller/cli/vgcctl
118
119mock-gen:
120 mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
Matteo Scandolo9061e432022-05-10 13:56:29 -0700121
122## lint and unit tests
Naveen Sampath04696f72022-06-13 15:19:14 +0530123lint-dockerfile: ## Perform static analysis on Dockerfile
124 @echo "Running Dockerfile lint check ..."
125 @${HADOLINT} $$(find . -name "Dockerfile.*")
126 @echo "Dockerfile lint check OK"
Matteo Scandolo9061e432022-05-10 13:56:29 -0700127
Naveen Sampath04696f72022-06-13 15:19:14 +0530128lint-mod: ## Verify the Go dependencies
129 @echo "Running dependency check..."
130 @${GOCMD} mod verify
131 @echo "Dependency check OK. Running vendor check..."
132 @git status > /dev/null
133 @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)
134 @[[ `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)
135 ${GOCMD} mod tidy
136 ${GOCMD} mod vendor
137 @git status > /dev/null
138 @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)
139 @[[ `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)
140 @echo "Vendor check OK."
Matteo Scandolo9061e432022-05-10 13:56:29 -0700141
Naveen Sampath04696f72022-06-13 15:19:14 +0530142lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
Matteo Scandolo9061e432022-05-10 13:56:29 -0700143
Naveen Sampath04696f72022-06-13 15:19:14 +0530144mod-update: ## Update go mod files
145 ${GOCMD} mod tidy
146 ${GOCMD} mod vendor
147
148test: ## Run unit tests
149 @mkdir -p ./tests/results
150 @${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
151 RETURN=$$? ;\
152 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
153 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
154 exit $$RETURN
Joey Armstrongec895f82024-04-25 14:26:07 -0400155
156## -----------------------------------------------------------------------
157## -----------------------------------------------------------------------
158pre-commit : .venv
159 source .venv/bin/activate && pre-commit
160
161## -----------------------------------------------------------------------
162## [TODO] Replace inlined target with repo:onf-make/makefiles/virtualenv/
163## -----------------------------------------------------------------------
164venv : .venv
165.venv :
166 virtualenv -p python3 $@
167 $@/bin/pip install -r requirements.txt
168
169## -----------------------------------------------------------------------
170## -----------------------------------------------------------------------
171help ::
172 @printf ' %-33.33s %s\n' 'pre-commit' \
173 'Invoke the pre-commit hook linting tool'
174
175clean ::
176 $(RM) -r .venv
177
178# [EOF]