blob: 9165c7b8d760cc0ad257023c4dadd0f79d586119 [file] [log] [blame]
Chip Boling6e27b352020-02-14 09:10:01 -06001#
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
23# tool containers
David K. Bainbridgeadf422d2021-04-09 16:06:41 +000024VOLTHA_TOOLS_VERSION ?= 2.4.0
Chip Boling6e27b352020-02-14 09:10:01 -060025
26GO = 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
27GO_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
29GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
30GOLANGCI_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
31
32# This should to be the first and default target in this Makefile
33help:
34 @echo "Usage: make [<target>]"
35 @echo "where available targets are:"
36 @echo
37 @echo "build : Build the library"
38 @echo "clean : Remove files created by the build"
39 @echo "distclean : Remove build and testing artifacts and reports"
40 @echo "lint-style : Verify code is properly gofmt-ed"
41 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
42 @echo "lint-mod : Verify the integrity of the 'mod' files"
43 @echo "lint : Shorthand for lint-style & lint-sanity"
44 @echo "mod-update : Update go.mod and the vendor directory"
45 @echo "test : Generate reports for all go tests"
46 @echo
47
48## build the library
49build:
50 ${GO} build -mod=vendor ./...
51
52## lint and unit tests
53
54lint-style:
55 @echo "Running style check..."
56 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
57 if [ ! -z "$$gofmt_out" ]; then \
58 echo "$$gofmt_out" ;\
59 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
60 exit 1 ;\
61 fi
62 @echo "Style check OK"
63
64lint-sanity:
65 @echo "Running sanity check..."
66 @${GO} vet -mod=vendor ./...
67 @echo "Sanity check OK"
68
69lint-mod:
70 @echo "Running dependency check..."
71 @${GO} mod verify
72 @echo "Dependency check OK. Running vendor check..."
73 @git status > /dev/null
74 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1)
75 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1)
76 ${GO} mod tidy
77 ${GO} mod vendor
78 @git status > /dev/null
79 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1)
80 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1)
81 @echo "Vendor check OK."
82
83lint: lint-style lint-sanity lint-mod
84
85sca:
86 rm -rf ./sca-report
87 @mkdir -p ./sca-report
88 ${GOLANGCI_LINT} run --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
89
90test:
91 @mkdir -p ./tests/results
92 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
93 RETURN=$$? ;\
94 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
95 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
96 exit $$RETURN
97
98clean: distclean
99
100distclean:
101 rm -rf ./sca-report ./tests
102
103mod-update:
104 ${GO} mod tidy
David K. Bainbridgeadf422d2021-04-09 16:06:41 +0000105 ${GO} mod vendor