-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
176 lines (148 loc) · 7.74 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- 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]