blob: dba48ec41439dc987ee4f29436a2b8fb42ac5a91 [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
23# Default is GO111MODULE=auto, which will refuse to use go mod if running
24# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
25# force module usage. This affects commands executed from this Makefile, but
26# not the environment inside the Docker build (which does not build from
27# inside a GOPATH).
28export GO111MODULE=on
29
30.PHONY: local-protos
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## Local Development Helpers
49local-protos:
50 @mkdir -p python/local_imports
51ifdef LOCAL_PROTOS
Scott Bakerf1b096c2019-11-01 12:36:30 -070052 rm -rf vendor/github.com/opencord/voltha-protos
53 mkdir -p vendor/github.com/opencord/voltha-protos/v2/go
54 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v2/go
Scott Baker2c1c4822019-10-16 11:02:41 -070055 rm -rf python/local_imports/voltha-protos
56 mkdir -p python/local_imports/voltha-protos/dist
Scott Bakerf1b096c2019-11-01 12:36:30 -070057 cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/
Scott Baker2c1c4822019-10-16 11:02:41 -070058endif
59
60## build the library
Scott Bakerf1b096c2019-11-01 12:36:30 -070061build: local-protos
Scott Baker2c1c4822019-10-16 11:02:41 -070062 go build -mod=vendor ./...
63
64## lint and unit tests
65
66lint-style:
67ifeq (,$(shell which gofmt))
68 go get -u github.com/golang/go/src/cmd/gofmt
69endif
70 @echo "Running style check..."
71 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
72 if [ ! -z "$$gofmt_out" ]; then \
73 echo "$$gofmt_out" ;\
74 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
75 exit 1 ;\
76 fi
77 @echo "Style check OK"
78
79lint-sanity:
80 @echo "Running sanity check..."
81 @go vet -mod=vendor ./...
82 @echo "Sanity check OK"
83
84lint-mod:
85 @echo "Running dependency check..."
86 @go mod verify
87 @echo "Dependency check OK"
88
89lint: lint-style lint-sanity lint-mod
90
91# Rules to automatically install golangci-lint
92GOLANGCI_LINT_TOOL?=$(shell which golangci-lint)
93ifeq (,$(GOLANGCI_LINT_TOOL))
94GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint
95golangci_lint_tool_install:
96 # Same version as installed by Jenkins ci-management
97 # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint
98 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0
99else
100golangci_lint_tool_install:
101endif
102
103# Rules to automatically install go-junit-report
104GO_JUNIT_REPORT?=$(shell which go-junit-report)
105ifeq (,$(GO_JUNIT_REPORT))
106GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
107go_junit_install:
108 go get -u github.com/jstemmer/go-junit-report
109else
110go_junit_install:
111endif
112
113# Rules to automatically install gocover-covertura
114GOCOVER_COBERTURA?=$(shell which gocover-cobertura)
115ifeq (,$(GOCOVER_COBERTURA))
116 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
117gocover_cobertura_install:
118 go get -u github.com/t-yuki/gocover-cobertura
119else
120gocover_cobertura_install:
121endif
122
123sca: golangci_lint_tool_install
124 rm -rf ./sca-report
125 @mkdir -p ./sca-report
126 $(GOLANGCI_LINT_TOOL) run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml
127
128test: go_junit_install gocover_cobertura_install
129 @mkdir -p ./tests/results
130 @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
131 RETURN=$$? ;\
132 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
133 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
134 exit $$RETURN
135
136clean:
137
138distclean: clean
139 rm -rf ./sca_report ./tests
140
141mod-update: build
142 go mod tidy
143 go mod vendor
144
145# end file