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