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 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 23 | # tool containers |
| 24 | VOLTHA_TOOLS_VERSION ?= 1.0.3 |
| 25 | |
Kent Hagerman | 08d15ab | 2020-02-14 14:54:52 -0500 | [diff] [blame] | 26 | GO = 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 Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 27 | 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 |
| 28 | GOCOVER_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 Hagerman | 08d15ab | 2020-02-14 14:54:52 -0500 | [diff] [blame] | 29 | GOFMT = 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 |
| 30 | 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 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 31 | |
| 32 | .PHONY: local-protos |
| 33 | |
| 34 | # This should to be the first and default target in this Makefile |
| 35 | help: |
| 36 | @echo "Usage: make [<target>]" |
| 37 | @echo "where available targets are:" |
| 38 | @echo |
| 39 | @echo "build : Build the library" |
| 40 | @echo "clean : Remove files created by the build" |
| 41 | @echo "distclean : Remove build and testing artifacts and reports" |
| 42 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 43 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
| 44 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
| 45 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 46 | @echo "mod-update : Update go.mod and the vendor directory" |
| 47 | @echo "test : Generate reports for all go tests" |
| 48 | @echo |
| 49 | |
| 50 | ## Local Development Helpers |
| 51 | local-protos: |
| 52 | @mkdir -p python/local_imports |
| 53 | ifdef LOCAL_PROTOS |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 54 | rm -rf vendor/github.com/opencord/voltha-protos |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 55 | mkdir -p vendor/github.com/opencord/voltha-protos/v3/go |
| 56 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 57 | rm -rf python/local_imports/voltha-protos |
| 58 | mkdir -p python/local_imports/voltha-protos/dist |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 59 | cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 60 | endif |
| 61 | |
| 62 | ## build the library |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 63 | build: local-protos |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 64 | ${GO} build -mod=vendor ./... |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 65 | |
| 66 | ## lint and unit tests |
| 67 | |
| 68 | lint-style: |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 69 | @echo "Running style check..." |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 70 | @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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..." |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 80 | @${GO} vet -mod=vendor ./... |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 81 | @echo "Sanity check OK" |
| 82 | |
| 83 | lint-mod: |
| 84 | @echo "Running dependency check..." |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 85 | @${GO} mod verify |
Scott Baker | a36b498 | 2019-11-26 08:09:23 -0800 | [diff] [blame] | 86 | @echo "Dependency check OK. Running vendor check..." |
| 87 | @git status > /dev/null |
| 88 | @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) |
| 89 | @[[ `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) |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 90 | ${GO} mod tidy |
| 91 | ${GO} mod vendor |
Scott Baker | a36b498 | 2019-11-26 08:09:23 -0800 | [diff] [blame] | 92 | @git status > /dev/null |
| 93 | @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) |
| 94 | @[[ `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) |
| 95 | @echo "Vendor check OK." |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 96 | |
| 97 | lint: lint-style lint-sanity lint-mod |
| 98 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 99 | sca: |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 100 | rm -rf ./sca-report |
| 101 | @mkdir -p ./sca-report |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 102 | ${GOLANGCI_LINT} run --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 103 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 104 | test: |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 105 | @mkdir -p ./tests/results |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 106 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 107 | RETURN=$$? ;\ |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 108 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 109 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 110 | exit $$RETURN |
| 111 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 112 | clean: distclean |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 113 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 114 | distclean: |
| 115 | rm -rf ./sca-report ./tests |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 116 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 117 | mod-update: |
| 118 | ${GO} mod tidy |
| 119 | ${GO} mod vendor |