Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | # |
| 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 |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(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). |
| 28 | export GO111MODULE=on |
| 29 | |
| 30 | .PHONY: local-protos |
| 31 | |
| 32 | # This should to be the first and default target in this Makefile |
| 33 | help: |
| 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 |
| 49 | local-protos: |
| 50 | @mkdir -p python/local_imports |
| 51 | ifdef LOCAL_PROTOS |
| 52 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
| 53 | cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go |
| 54 | rm -rf python/local_imports/voltha-protos |
| 55 | mkdir -p python/local_imports/voltha-protos/dist |
| 56 | cp ../voltha-protos/dist/*.tar.gz python/local_imports/voltha-protos/dist/ |
| 57 | endif |
| 58 | |
| 59 | ## build the library |
| 60 | build: |
| 61 | go build -mod=vendor ./... |
| 62 | |
| 63 | ## lint and unit tests |
| 64 | |
| 65 | lint-style: |
| 66 | ifeq (,$(shell which gofmt)) |
| 67 | go get -u github.com/golang/go/src/cmd/gofmt |
| 68 | endif |
| 69 | @echo "Running style check..." |
| 70 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 71 | if [ ! -z "$$gofmt_out" ]; then \ |
| 72 | echo "$$gofmt_out" ;\ |
| 73 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 74 | exit 1 ;\ |
| 75 | fi |
| 76 | @echo "Style check OK" |
| 77 | |
| 78 | lint-sanity: |
| 79 | @echo "Running sanity check..." |
| 80 | @go vet -mod=vendor ./... |
| 81 | @echo "Sanity check OK" |
| 82 | |
| 83 | lint-mod: |
| 84 | @echo "Running dependency check..." |
| 85 | @go mod verify |
| 86 | @echo "Dependency check OK" |
| 87 | |
| 88 | lint: lint-style lint-sanity lint-mod |
| 89 | |
| 90 | # Rules to automatically install golangci-lint |
| 91 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
| 92 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
| 93 | GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint |
| 94 | golangci_lint_tool_install: |
| 95 | # Same version as installed by Jenkins ci-management |
| 96 | # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint |
| 97 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0 |
| 98 | else |
| 99 | golangci_lint_tool_install: |
| 100 | endif |
| 101 | |
| 102 | # Rules to automatically install go-junit-report |
| 103 | GO_JUNIT_REPORT?=$(shell which go-junit-report) |
| 104 | ifeq (,$(GO_JUNIT_REPORT)) |
| 105 | GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 106 | go_junit_install: |
| 107 | go get -u github.com/jstemmer/go-junit-report |
| 108 | else |
| 109 | go_junit_install: |
| 110 | endif |
| 111 | |
| 112 | # Rules to automatically install gocover-covertura |
| 113 | GOCOVER_COBERTURA?=$(shell which gocover-cobertura) |
| 114 | ifeq (,$(GOCOVER_COBERTURA)) |
| 115 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
| 116 | gocover_cobertura_install: |
| 117 | go get -u github.com/t-yuki/gocover-cobertura |
| 118 | else |
| 119 | gocover_cobertura_install: |
| 120 | endif |
| 121 | |
| 122 | sca: golangci_lint_tool_install |
| 123 | rm -rf ./sca-report |
| 124 | @mkdir -p ./sca-report |
| 125 | $(GOLANGCI_LINT_TOOL) run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml |
| 126 | |
| 127 | test: go_junit_install gocover_cobertura_install |
| 128 | @mkdir -p ./tests/results |
| 129 | @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 130 | RETURN=$$? ;\ |
| 131 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 132 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 133 | exit $$RETURN |
| 134 | |
| 135 | clean: |
| 136 | |
| 137 | distclean: clean |
| 138 | rm -rf ./sca_report ./tests |
| 139 | |
| 140 | mod-update: build |
| 141 | go mod tidy |
| 142 | go mod vendor |
| 143 | |
| 144 | # end file |