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