Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [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 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 23 | DOCKER_LABEL_VCS_DIRTY = false |
| 24 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 25 | DOCKER_LABEL_VCS_DIRTY = true |
| 26 | endif |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 27 | ## Docker related |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 28 | DOCKER_EXTRA_ARGS ?= |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 31 | DOCKER_TAG ?= ${VERSION} |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 32 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG} |
Andrea Campanella | cbf583e | 2019-12-17 15:26:00 -0800 | [diff] [blame] | 33 | TYPE ?= minimal |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 34 | |
| 35 | ## Docker labels. Only set ref and commit date if committed |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 36 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 37 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 38 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 39 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 40 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 41 | DOCKER_BUILD_ARGS ?= \ |
| 42 | ${DOCKER_EXTRA_ARGS} \ |
| 43 | --build-arg org_label_schema_version="${VERSION}" \ |
| 44 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 45 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 46 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 48 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 49 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 50 | # tool containers |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 51 | VOLTHA_TOOLS_VERSION ?= 2.0.0 |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 52 | |
Kent Hagerman | 5283008 | 2020-02-14 14:53:52 -0500 | [diff] [blame] | 53 | 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 | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 54 | 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 |
| 55 | 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 | 5283008 | 2020-02-14 14:53:52 -0500 | [diff] [blame] | 56 | 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 |
| 57 | HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 58 | |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 59 | .PHONY: docker-build local-protos local-lib-go |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 60 | |
| 61 | # This should to be the first and default target in this Makefile |
| 62 | help: |
| 63 | @echo "Usage: make [<target>]" |
| 64 | @echo "where available targets are:" |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 65 | @echo "" |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 66 | @echo "clean : Removes any local filesystem artifacts generated by a build" |
| 67 | @echo "distclean : Removes any local filesystem artifacts generated by a build or test run" |
| 68 | @echo "build : Build all openolt adapter artifacts" |
| 69 | @echo "docker-build : Build openolt adapter docker image" |
| 70 | @echo "docker-build-profile : Build openolt adapter docker image with profiling enabled" |
| 71 | @echo "docker-push : Push the docker images to an external repository" |
| 72 | @echo "help : Print this help" |
| 73 | @echo "lint : Run all lint targets" |
| 74 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 75 | @echo "lint-mod : Verify the Go dependencies" |
| 76 | @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory" |
| 77 | @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory" |
| 78 | @echo "sca : Runs various SCA through golangci-lint tool" |
| 79 | @echo "test : Run unit tests, if any" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 80 | @echo |
| 81 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 82 | ## Local Development Helpers |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 83 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 84 | local-protos: |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 85 | ifdef LOCAL_PROTOS |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 86 | rm -rf vendor/github.com/opencord/voltha-protos/v3/go |
| 87 | mkdir -p vendor/github.com/opencord/voltha-protos/v3/go |
| 88 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go |
| 89 | rm -rf vendor/github.com/opencord/voltha-protos/v3/go/vendor |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 90 | endif |
| 91 | |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 92 | ## Local Development Helpers |
| 93 | local-lib-go: |
| 94 | ifdef LOCAL_LIB_GO |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 95 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v3/pkg |
| 96 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v3/pkg/ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 97 | endif |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 98 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 99 | ## Docker targets |
| 100 | |
David Bainbridge | 63d5181 | 2019-10-22 15:55:29 +0000 | [diff] [blame] | 101 | build: docker-build |
| 102 | |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 103 | docker-build: local-protos local-lib-go |
David K. Bainbridge | 8eb6cd6 | 2019-08-22 13:17:29 -0700 | [diff] [blame] | 104 | docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt . |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 105 | |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 106 | docker-build-profile: local-protos local-lib-go |
| 107 | docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openolt_profile . |
| 108 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 109 | docker-push: |
| 110 | docker push ${ADAPTER_IMAGENAME} |
| 111 | |
Andrea Campanella | cbf583e | 2019-12-17 15:26:00 -0800 | [diff] [blame] | 112 | docker-kind-load: |
| 113 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 114 | kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 115 | |
| 116 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 117 | ## lint and unit tests |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 118 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 119 | lint-dockerfile: |
| 120 | @echo "Running Dockerfile lint check ..." |
| 121 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 122 | @echo "Dockerfile lint check OK" |
| 123 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 124 | lint-mod: |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 125 | @echo "Running dependency check..." |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 126 | @${GO} mod verify |
Scott Baker | 22c0e9c | 2019-11-26 08:11:29 -0800 | [diff] [blame] | 127 | @echo "Dependency check OK. Running vendor check..." |
| 128 | @git status > /dev/null |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 129 | @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) |
| 130 | @[[ `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 | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 131 | ${GO} mod tidy |
| 132 | ${GO} mod vendor |
Scott Baker | 22c0e9c | 2019-11-26 08:11:29 -0800 | [diff] [blame] | 133 | @git status > /dev/null |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 134 | @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) |
| 135 | @[[ `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 | 22c0e9c | 2019-11-26 08:11:29 -0800 | [diff] [blame] | 136 | @echo "Vendor check OK." |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 137 | |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 138 | lint: local-lib-go lint-mod lint-dockerfile |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 139 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 140 | test: |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 141 | @mkdir -p ./tests/results |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 142 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 143 | RETURN=$$? ;\ |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 144 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 145 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 146 | exit $$RETURN |
| 147 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 148 | sca: |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 149 | @rm -rf ./sca-report |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 150 | @mkdir -p ./sca-report |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 151 | @echo "Running static code analysis..." |
| 152 | @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 153 | @echo "" |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 154 | @echo "Static code analysis OK" |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 155 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 156 | clean: distclean |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 157 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 158 | distclean: |
| 159 | rm -rf ./sca-report |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 160 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 161 | mod-update: |
| 162 | ${GO} mod tidy |
| 163 | ${GO} mod vendor |