blob: 077d623b636bf8f3bf176704aa7f95765c682902 [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 +053026GOLINT := golint
27GOCMD = 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
28GOBUILD=$(GOCMD) build
29GOCLEAN=$(GOCMD) clean
30GOTEST=$(GOCMD) test
31GOTOOL=$(GOCMD) tool
32
33# Static code analyzers
34GOIMPORTS := goimports
35GO_FILES := `find . -path vendor -prune -o -name '*.go'`
36STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS)
37#STATIC_TOOLS := $(GOFMT)
Matteo Scandolo9061e432022-05-10 13:56:29 -070038
39# set default shell
40SHELL = bash -e -o pipefail
41
42# Variables
Naveen Sampath04696f72022-06-13 15:19:14 +053043VERSION ?= $(shell head -n 1 ./VERSION)
abhay9690fd72025-01-07 14:22:03 +053044## Docker related
45DOCKER_NAME := voltha-go-controller
46DOCKER_LABEL_VCS_DIRTY = false
47ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
48 DOCKER_LABEL_VCS_DIRTY = true
49endif
50DOCKER_EXTRA_ARGS ?=
51DOCKER_REGISTRY ?=
52DOCKER_REPOSITORY ?= voltha/
53DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
54IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-go-controller:${DOCKER_TAG}
55DOCKER_TARGET ?= prod
56
57## Docker labels. Only set ref and commit date if committed
58DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
59DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
60DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
61DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
62DOCKER_BUILD_ARGS ?= \
63 ${DOCKER_EXTRA_ARGS} \
64 --build-arg org_label_schema_version="${VERSION}" \
65 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
66 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
67 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
68 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
69 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
70
Matteo Scandolo9061e432022-05-10 13:56:29 -070071
Naveen Sampath04696f72022-06-13 15:19:14 +053072COVERAGE_DIR = ./tests/results
73COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out
74TEST_TARGETS := test-default test-verbose test-short
75test-short: ARGS=-short
76test-verbose: ARGS=-v
Matteo Scandolo9061e432022-05-10 13:56:29 -070077# tool containers
78VOLTHA_TOOLS_VERSION ?= 2.4.0
79
Matteo Scandolo9061e432022-05-10 13:56:29 -070080HADOLINT = 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 +053081GOLANGCI_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
82GO_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
83GOCOVER_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
84
85## Local Development Helpers
86local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
87ifdef LOCAL_PROTOS
88 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
89 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
90 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
91 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
92endif
93
94local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
95ifdef LOCAL_LIB_GO
96 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
97 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
98endif
99
100# This should to be the first and default target in this Makefile
Joey Armstrongec895f82024-04-25 14:26:07 -0400101help :: ## Print help for each Makefile target
Naveen Sampath04696f72022-06-13 15:19:14 +0530102 @echo "Usage: make [<target>]"
103 @echo "where available targets are:"
104 @echo
105 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
106 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
107
108
109exe:
110 @echo Building voltha-go-controller....
111 @cd voltha-go-controller && go build
112
113vgcctl:
114 @echo Building vgcctl....
115 @cd voltha-go-controller/cli && go build -o vgcctl
116
abhay55a8b262025-01-06 18:22:24 +0530117docker-build:
118 @echo Building Docker $(DOCKER_NAME)....
abhay9690fd72025-01-07 14:22:03 +0530119 docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
120ifdef BUILD_PROFILED
121 docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${IMAGENAME}-profile -f docker/Dockerfile.voltha-go-controller .
122endif
123ifdef BUILD_RACE
124 docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg EXTRA_GO_BUILD_TAGS="-race" -t ${IMAGENAME}-rd -f docker/Dockerfile.voltha-go-controller .
125endif
abhay41e845e2025-01-06 15:54:18 +0530126
abhay83672ba2025-01-06 19:06:13 +0530127docker-push: ## Push the docker images to an external repository
128 docker push ${IMAGENAME}
129ifdef BUILD_PROFILED
130 docker push ${IMAGENAME}-profile
131endif
132ifdef BUILD_RACE
133 docker push ${IMAGENAME}-rd
134endif
135
Naveen Sampath04696f72022-06-13 15:19:14 +0530136docker: exe vgcctl
137 @echo Building Docker $(DOCKER_NAME)....
138 sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
139
140## Docker targets
141build: local-protos local-lib-go docker ## Build voltha-go-controller image
142
143build-docker-profile: sca exe-profile vgcctl
144 @echo Building Docker $(DOCKER_NAME)....
145 sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller .
146
147sca: ## Runs static code analysis with the golangci-lint tool
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +0530148 @rm -rf ./sca-report
149 @mkdir -p ./sca-report
Naveen Sampath04696f72022-06-13 15:19:14 +0530150 @echo "Running static code analysis..."
Tinoj Josephc2ccd6b2022-07-19 04:32:15 +0530151 @${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
152 @echo ""
153 @echo "Static code analysis OK"
Naveen Sampath04696f72022-06-13 15:19:14 +0530154
Joey Armstrongec895f82024-04-25 14:26:07 -0400155clean :: ## Removes any local filesystem artifacts generated by a build
Naveen Sampath04696f72022-06-13 15:19:14 +0530156 rm -f voltha-go-controller/voltha-go-controller
157 rm -f voltha-go-controller/cli/vgcctl
158
159mock-gen:
160 mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
Matteo Scandolo9061e432022-05-10 13:56:29 -0700161
162## lint and unit tests
Naveen Sampath04696f72022-06-13 15:19:14 +0530163lint-dockerfile: ## Perform static analysis on Dockerfile
164 @echo "Running Dockerfile lint check ..."
165 @${HADOLINT} $$(find . -name "Dockerfile.*")
166 @echo "Dockerfile lint check OK"
Matteo Scandolo9061e432022-05-10 13:56:29 -0700167
Naveen Sampath04696f72022-06-13 15:19:14 +0530168lint-mod: ## Verify the Go dependencies
169 @echo "Running dependency check..."
170 @${GOCMD} mod verify
171 @echo "Dependency check OK. Running vendor check..."
172 @git status > /dev/null
173 @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)
174 @[[ `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)
175 ${GOCMD} mod tidy
176 ${GOCMD} mod vendor
177 @git status > /dev/null
178 @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)
179 @[[ `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)
180 @echo "Vendor check OK."
Matteo Scandolo9061e432022-05-10 13:56:29 -0700181
Naveen Sampath04696f72022-06-13 15:19:14 +0530182lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
Matteo Scandolo9061e432022-05-10 13:56:29 -0700183
Naveen Sampath04696f72022-06-13 15:19:14 +0530184mod-update: ## Update go mod files
185 ${GOCMD} mod tidy
186 ${GOCMD} mod vendor
187
188test: ## Run unit tests
189 @mkdir -p ./tests/results
190 @${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
191 RETURN=$$? ;\
192 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
193 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
194 exit $$RETURN
Joey Armstrongec895f82024-04-25 14:26:07 -0400195
196## -----------------------------------------------------------------------
197## -----------------------------------------------------------------------
198pre-commit : .venv
199 source .venv/bin/activate && pre-commit
200
201## -----------------------------------------------------------------------
202## [TODO] Replace inlined target with repo:onf-make/makefiles/virtualenv/
203## -----------------------------------------------------------------------
204venv : .venv
205.venv :
206 virtualenv -p python3 $@
207 $@/bin/pip install -r requirements.txt
208
209## -----------------------------------------------------------------------
210## -----------------------------------------------------------------------
211help ::
212 @printf ' %-33.33s %s\n' 'pre-commit' \
213 'Invoke the pre-commit hook linting tool'
214
215clean ::
216 $(RM) -r .venv
217
218# [EOF]