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 ?= |
David K. Bainbridge | 96b0651 | 2021-04-01 18:53:51 +0000 | [diff] [blame] | 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 32 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG} |
David K. Bainbridge | 96b0651 | 2021-04-01 18:53:51 +0000 | [diff] [blame] | 33 | DOCKER_TARGET ?= prod |
Andrea Campanella | cbf583e | 2019-12-17 15:26:00 -0800 | [diff] [blame] | 34 | TYPE ?= minimal |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 35 | |
| 36 | ## Docker labels. Only set ref and commit date if committed |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 37 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 38 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 39 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 40 | 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] | 41 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 42 | DOCKER_BUILD_ARGS ?= \ |
| 43 | ${DOCKER_EXTRA_ARGS} \ |
| 44 | --build-arg org_label_schema_version="${VERSION}" \ |
| 45 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 46 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 47 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 48 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 49 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 50 | |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 51 | # tool containers |
David K. Bainbridge | 2f2658d | 2021-04-09 16:13:57 +0000 | [diff] [blame] | 52 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 53 | |
Kent Hagerman | 5283008 | 2020-02-14 14:53:52 -0500 | [diff] [blame] | 54 | 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] | 55 | 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 |
Matteo Scandolo | d5c2240 | 2020-11-23 14:28:37 -0800 | [diff] [blame] | 56 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openolt-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Kent Hagerman | 5283008 | 2020-02-14 14:53:52 -0500 | [diff] [blame] | 57 | 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 |
| 58 | 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] | 59 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 60 | .PHONY: docker-build local-protos local-lib-go help |
| 61 | .DEFAULT_GOAL := help |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 62 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 63 | ## Local Development Helpers |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 64 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 65 | ifdef LOCAL_PROTOS |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 66 | rm -rf vendor/github.com/opencord/voltha-protos/v4/go |
| 67 | mkdir -p vendor/github.com/opencord/voltha-protos/v4/go |
| 68 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go |
| 69 | rm -rf vendor/github.com/opencord/voltha-protos/v4/go/vendor |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 70 | endif |
| 71 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 72 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
Scott Baker | 355d174 | 2019-10-24 10:57:52 -0700 | [diff] [blame] | 73 | ifdef LOCAL_LIB_GO |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 74 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v5/pkg |
| 75 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v5/pkg/ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 76 | endif |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 77 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 78 | ## Docker targets |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 79 | build: docker-build ## Alias for 'docker build' |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 80 | |
Matteo Scandolo | 6ec3c35 | 2020-04-29 15:27:03 -0700 | [diff] [blame] | 81 | docker-build: local-protos local-lib-go ## Build openolt adapter docker image (set BUILD_PROFILED=true to also build the profiled image) |
David K. Bainbridge | e2d9b33 | 2021-04-15 00:11:51 +0000 | [diff] [blame] | 82 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg CGO_PARAMETER=0 -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt . |
Matteo Scandolo | 6ec3c35 | 2020-04-29 15:27:03 -0700 | [diff] [blame] | 83 | ifdef BUILD_PROFILED |
David K. Bainbridge | e2d9b33 | 2021-04-15 00:11:51 +0000 | [diff] [blame] | 84 | docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openolt . |
| 85 | endif |
| 86 | ifdef BUILD_RACE |
| 87 | docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-race" -t ${ADAPTER_IMAGENAME}-rd -f docker/Dockerfile.openolt . |
Matteo Scandolo | 6ec3c35 | 2020-04-29 15:27:03 -0700 | [diff] [blame] | 88 | endif |
Matteo Scandolo | 8f2b957 | 2020-02-28 15:35:23 -0800 | [diff] [blame] | 89 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 90 | docker-push: ## Push the docker images to an external repository |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 91 | docker push ${ADAPTER_IMAGENAME} |
Matteo Scandolo | 24ed967 | 2020-05-04 10:53:08 -0700 | [diff] [blame] | 92 | ifdef BUILD_PROFILED |
| 93 | docker push ${ADAPTER_IMAGENAME}-profile |
| 94 | endif |
David K. Bainbridge | e2d9b33 | 2021-04-15 00:11:51 +0000 | [diff] [blame] | 95 | ifdef BUILD_RACE |
| 96 | docker push ${ADAPTER_IMAGENAME}-rd |
| 97 | endif |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 98 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 99 | docker-kind-load: ## Load docker images into a KinD cluster |
Andrea Campanella | cbf583e | 2019-12-17 15:26:00 -0800 | [diff] [blame] | 100 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 101 | kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 102 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 103 | ## lint and unit tests |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 104 | lint-dockerfile: ## Perform static analysis on Dockerfile |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 105 | @echo "Running Dockerfile lint check ..." |
| 106 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 107 | @echo "Dockerfile lint check OK" |
| 108 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 109 | lint-mod: ## Verify the Go dependencies |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 110 | @echo "Running dependency check..." |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 111 | @${GO} mod verify |
Scott Baker | 22c0e9c | 2019-11-26 08:11:29 -0800 | [diff] [blame] | 112 | @echo "Dependency check OK. Running vendor check..." |
| 113 | @git status > /dev/null |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 114 | @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) |
| 115 | @[[ `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] | 116 | ${GO} mod tidy |
| 117 | ${GO} mod vendor |
Scott Baker | 22c0e9c | 2019-11-26 08:11:29 -0800 | [diff] [blame] | 118 | @git status > /dev/null |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 119 | @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) |
| 120 | @[[ `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] | 121 | @echo "Vendor check OK." |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 122 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 123 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 124 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 125 | test: ## Run unit tests |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 126 | @mkdir -p ./tests/results |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 127 | @${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] | 128 | RETURN=$$? ;\ |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 129 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 130 | ${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] | 131 | exit $$RETURN |
| 132 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 133 | sca: ## Runs static code analysis with the golangci-lint tool |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 134 | @rm -rf ./sca-report |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 135 | @mkdir -p ./sca-report |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 136 | @echo "Running static code analysis..." |
Andrea Campanella | 21fc1b4 | 2021-03-10 16:41:33 +0100 | [diff] [blame] | 137 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Kent Hagerman | e71b52d | 2020-02-26 11:13:42 -0500 | [diff] [blame] | 138 | @echo "" |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 139 | @echo "Static code analysis OK" |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 140 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 141 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 142 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 143 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 144 | rm -rf ./sca-report |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 145 | |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 146 | mod-update: ## Update go mod files |
Kent Hagerman | 2cd4b7b | 2020-02-04 15:15:14 -0500 | [diff] [blame] | 147 | ${GO} mod tidy |
| 148 | ${GO} mod vendor |
Zack Williams | 7566225 | 2020-04-06 18:16:54 -0700 | [diff] [blame] | 149 | |
| 150 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
| 151 | help: ## Print help for each Makefile target |
| 152 | @echo "Usage: make [<target>]" |
| 153 | @echo "where available targets are:" |
| 154 | @echo |
| 155 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 156 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |