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 |
David K. Bainbridge | c50d88c | 2021-04-08 15:47:03 +0000 | [diff] [blame] | 24 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 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 |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 28 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-lib-go/v7 -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 | 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] | 30 | |
| 31 | .PHONY: local-protos |
| 32 | |
| 33 | # This should to be the first and default target in this Makefile |
| 34 | help: |
| 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 41 | @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 |
| 48 | local-protos: |
| 49 | @mkdir -p python/local_imports |
| 50 | ifdef LOCAL_PROTOS |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 51 | rm -rf vendor/github.com/opencord/voltha-protos |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 52 | mkdir -p vendor/github.com/opencord/voltha-protos/v5/go |
| 53 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 54 | rm -rf python/local_imports/voltha-protos |
| 55 | mkdir -p python/local_imports/voltha-protos/dist |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 56 | cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 57 | endif |
| 58 | |
| 59 | ## build the library |
Scott Baker | f1b096c | 2019-11-01 12:36:30 -0700 | [diff] [blame] | 60 | build: local-protos |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 61 | ## ${GO} build -mod=vendor ./... |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 62 | ${GO} build -mod=vendor ./... |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 63 | |
| 64 | ## lint and unit tests |
| 65 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 66 | lint-mod: |
| 67 | @echo "Running dependency check..." |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 68 | @${GO} mod verify |
Scott Baker | a36b498 | 2019-11-26 08:09:23 -0800 | [diff] [blame] | 69 | @echo "Dependency check OK. Running vendor check..." |
| 70 | @git status > /dev/null |
Kent Hagerman | 3da1fd0 | 2020-02-26 10:54:07 -0500 | [diff] [blame] | 71 | @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) |
| 72 | @[[ `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 Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 73 | ${GO} mod tidy |
| 74 | ${GO} mod vendor |
Scott Baker | a36b498 | 2019-11-26 08:09:23 -0800 | [diff] [blame] | 75 | @git status > /dev/null |
Kent Hagerman | 3da1fd0 | 2020-02-26 10:54:07 -0500 | [diff] [blame] | 76 | @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) |
| 77 | @[[ `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 Baker | a36b498 | 2019-11-26 08:09:23 -0800 | [diff] [blame] | 78 | @echo "Vendor check OK." |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 79 | |
Kent Hagerman | 3da1fd0 | 2020-02-26 10:54:07 -0500 | [diff] [blame] | 80 | lint: lint-mod |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 81 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 82 | sca: |
Kent Hagerman | 3da1fd0 | 2020-02-26 10:54:07 -0500 | [diff] [blame] | 83 | @rm -rf ./sca-report |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 84 | @mkdir -p ./sca-report |
Kent Hagerman | 3da1fd0 | 2020-02-26 10:54:07 -0500 | [diff] [blame] | 85 | @echo "Running static code analysis..." |
| 86 | @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 87 | @echo "" |
| 88 | @echo "Static code analysis OK" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 89 | |
Matteo Scandolo | d58eaef | 2020-03-30 12:30:02 -0700 | [diff] [blame] | 90 | test: local-protos |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 91 | @mkdir -p ./tests/results |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 92 | @${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] | 93 | RETURN=$$? ;\ |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 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 ;\ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 96 | exit $$RETURN |
| 97 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 98 | clean: distclean |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 99 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 100 | distclean: |
| 101 | rm -rf ./sca-report ./tests |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 102 | |
Kent Hagerman | fac11d4 | 2020-01-28 12:18:55 -0500 | [diff] [blame] | 103 | mod-update: |
| 104 | ${GO} mod tidy |
| 105 | ${GO} mod vendor |