blob: 239fe0473066b542f0466c149e2e79c4d34b1bb4 [file] [log] [blame]
# -*- makefile -*-
# -----------------------------------------------------------------------
# Copyright 2022-2024 Open Networking Foundation Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------
# SPDX-FileCopyrightText: 2022-2024 Open Networking Foundation Contributors
# SPDX-License-Identifier: Apache-2.0
# -----------------------------------------------------------------------
# Intent: Build, test and release voltha-go-controller
# -----------------------------------------------------------------------
# Todo: Refactor common makefile logic and targets shared by
# several VOLTHA repositories.
# -----------------------------------------------------------------------
DOCKER_NAME := voltha-go-controller
DOCKER_TAG := latest
GOLINT := golint
GOCMD = 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
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOTOOL=$(GOCMD) tool
# Static code analyzers
GOIMPORTS := goimports
GO_FILES := `find . -path vendor -prune -o -name '*.go'`
STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS)
#STATIC_TOOLS := $(GOFMT)
# set default shell
SHELL = bash -e -o pipefail
# Variables
VERSION ?= $(shell head -n 1 ./VERSION)
# DOCKER_TAG ?= ${VERSION}
IMAGENAME ?= ${DOCKER_NAME}:${DOCKER_TAG}
COVERAGE_DIR = ./tests/results
COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out
TEST_TARGETS := test-default test-verbose test-short
test-short: ARGS=-short
test-verbose: ARGS=-v
# tool containers
VOLTHA_TOOLS_VERSION ?= 2.4.0
HADOLINT = 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
GOLANGCI_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
GO_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
GOCOVER_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
## Local Development Helpers
local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
ifdef LOCAL_PROTOS
rm -rf vendor/github.com/opencord/voltha-protos/v5/go
mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
endif
local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
ifdef LOCAL_LIB_GO
mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
endif
# This should to be the first and default target in this Makefile
help :: ## Print help for each Makefile target
@echo "Usage: make [<target>]"
@echo "where available targets are:"
@echo
@grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
| sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
exe:
@echo Building voltha-go-controller....
@cd voltha-go-controller && go build
vgcctl:
@echo Building vgcctl....
@cd voltha-go-controller/cli && go build -o vgcctl
docker: exe vgcctl
@echo Building Docker $(DOCKER_NAME)....
sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller .
## Docker targets
build: local-protos local-lib-go docker ## Build voltha-go-controller image
build-docker-profile: sca exe-profile vgcctl
@echo Building Docker $(DOCKER_NAME)....
sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller .
sca: ## Runs static code analysis with the golangci-lint tool
@rm -rf ./sca-report
@mkdir -p ./sca-report
@echo "Running static code analysis..."
@${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
@echo ""
@echo "Static code analysis OK"
clean :: ## Removes any local filesystem artifacts generated by a build
rm -f voltha-go-controller/voltha-go-controller
rm -f voltha-go-controller/cli/vgcctl
mock-gen:
mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go
## lint and unit tests
lint-dockerfile: ## Perform static analysis on Dockerfile
@echo "Running Dockerfile lint check ..."
@${HADOLINT} $$(find . -name "Dockerfile.*")
@echo "Dockerfile lint check OK"
lint-mod: ## Verify the Go dependencies
@echo "Running dependency check..."
@${GOCMD} mod verify
@echo "Dependency check OK. Running vendor check..."
@git status > /dev/null
@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)
@[[ `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)
${GOCMD} mod tidy
${GOCMD} mod vendor
@git status > /dev/null
@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)
@[[ `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)
@echo "Vendor check OK."
lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
mod-update: ## Update go mod files
${GOCMD} mod tidy
${GOCMD} mod vendor
test: ## Run unit tests
@mkdir -p ./tests/results
@${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
RETURN=$$? ;\
${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
exit $$RETURN
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
pre-commit : .venv
source .venv/bin/activate && pre-commit
## -----------------------------------------------------------------------
## [TODO] Replace inlined target with repo:onf-make/makefiles/virtualenv/
## -----------------------------------------------------------------------
venv : .venv
.venv :
virtualenv -p python3 $@
$@/bin/pip install -r requirements.txt
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
help ::
@printf ' %-33.33s %s\n' 'pre-commit' \
'Invoke the pre-commit hook linting tool'
clean ::
$(RM) -r .venv
# [EOF]