blob: 938faa10fed1e4e38524760ae25a00c9f6847784 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17# set default shell
18SHELL = bash -e -o pipefail
19
20# Variables
21VERSION ?= $(shell cat ./VERSION)
22
Kent Hagermanfac11d42020-01-28 12:18:55 -050023# tool containers
Kent Hagerman3da1fd02020-02-26 10:54:07 -050024VOLTHA_TOOLS_VERSION ?= 2.0.0
Kent Hagermanfac11d42020-01-28 12:18:55 -050025
Kent Hagerman08d15ab2020-02-14 14:54:52 -050026GO = 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
Kent Hagermanfac11d42020-01-28 12:18:55 -050027GO_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
28GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Kent Hagerman08d15ab2020-02-14 14:54:52 -050029GOLANGCI_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
Scott Baker2c1c4822019-10-16 11:02:41 -070030
31.PHONY: local-protos
32
33# This should to be the first and default target in this Makefile
34help:
35 @echo "Usage: make [<target>]"
36 @echo "where available targets are:"
37 @echo
38 @echo "build : Build the library"
39 @echo "clean : Remove files created by the build"
40 @echo "distclean : Remove build and testing artifacts and reports"
Scott Baker2c1c4822019-10-16 11:02:41 -070041 @echo "lint-mod : Verify the integrity of the 'mod' files"
42 @echo "lint : Shorthand for lint-style & lint-sanity"
43 @echo "mod-update : Update go.mod and the vendor directory"
44 @echo "test : Generate reports for all go tests"
45 @echo
46
47## Local Development Helpers
48local-protos:
49 @mkdir -p python/local_imports
50ifdef LOCAL_PROTOS
Scott Bakerf1b096c2019-11-01 12:36:30 -070051 rm -rf vendor/github.com/opencord/voltha-protos
Girish Gowdra89c985b2020-10-14 15:02:09 -070052 mkdir -p vendor/github.com/opencord/voltha-protos/v4/go
53 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go
Scott Baker2c1c4822019-10-16 11:02:41 -070054 rm -rf python/local_imports/voltha-protos
55 mkdir -p python/local_imports/voltha-protos/dist
Scott Bakerf1b096c2019-11-01 12:36:30 -070056 cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/
Scott Baker2c1c4822019-10-16 11:02:41 -070057endif
58
59## build the library
Scott Bakerf1b096c2019-11-01 12:36:30 -070060build: local-protos
Kent Hagermanfac11d42020-01-28 12:18:55 -050061 ${GO} build -mod=vendor ./...
Scott Baker2c1c4822019-10-16 11:02:41 -070062
63## lint and unit tests
64
Scott Baker2c1c4822019-10-16 11:02:41 -070065lint-mod:
66 @echo "Running dependency check..."
Kent Hagermanfac11d42020-01-28 12:18:55 -050067 @${GO} mod verify
Scott Bakera36b4982019-11-26 08:09:23 -080068 @echo "Dependency check OK. Running vendor check..."
69 @git status > /dev/null
Kent Hagerman3da1fd02020-02-26 10:54:07 -050070 @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)
71 @[[ `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)
Kent Hagermanfac11d42020-01-28 12:18:55 -050072 ${GO} mod tidy
73 ${GO} mod vendor
Scott Bakera36b4982019-11-26 08:09:23 -080074 @git status > /dev/null
Kent Hagerman3da1fd02020-02-26 10:54:07 -050075 @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)
76 @[[ `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)
Scott Bakera36b4982019-11-26 08:09:23 -080077 @echo "Vendor check OK."
Scott Baker2c1c4822019-10-16 11:02:41 -070078
Kent Hagerman3da1fd02020-02-26 10:54:07 -050079lint: lint-mod
Scott Baker2c1c4822019-10-16 11:02:41 -070080
Kent Hagermanfac11d42020-01-28 12:18:55 -050081sca:
Kent Hagerman3da1fd02020-02-26 10:54:07 -050082 @rm -rf ./sca-report
Scott Baker2c1c4822019-10-16 11:02:41 -070083 @mkdir -p ./sca-report
Kent Hagerman3da1fd02020-02-26 10:54:07 -050084 @echo "Running static code analysis..."
85 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
86 @echo ""
87 @echo "Static code analysis OK"
Scott Baker2c1c4822019-10-16 11:02:41 -070088
Matteo Scandolod58eaef2020-03-30 12:30:02 -070089test: local-protos
Scott Baker2c1c4822019-10-16 11:02:41 -070090 @mkdir -p ./tests/results
Kent Hagermanfac11d42020-01-28 12:18:55 -050091 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Scott Baker2c1c4822019-10-16 11:02:41 -070092 RETURN=$$? ;\
Kent Hagermanfac11d42020-01-28 12:18:55 -050093 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
94 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker2c1c4822019-10-16 11:02:41 -070095 exit $$RETURN
96
Kent Hagermanfac11d42020-01-28 12:18:55 -050097clean: distclean
Scott Baker2c1c4822019-10-16 11:02:41 -070098
Kent Hagermanfac11d42020-01-28 12:18:55 -050099distclean:
100 rm -rf ./sca-report ./tests
Scott Baker2c1c4822019-10-16 11:02:41 -0700101
Kent Hagermanfac11d42020-01-28 12:18:55 -0500102mod-update:
103 ${GO} mod tidy
104 ${GO} mod vendor