Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [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 |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 21 | VERSION ?= $(shell cat ./VERSION) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 22 | |
| 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 |
| 27 | ## Docker related |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 28 | DOCKER_EXTRA_ARGS ?= |
| 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
| 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 32 | DOCKER_TARGET ?= prod |
Zack Williams | a054c7f | 2020-03-23 10:19:29 -0700 | [diff] [blame] | 33 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-ofagent-go |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 34 | |
| 35 | ## Docker labels. Only set ref and commit date if committed |
| 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) |
| 40 | |
| 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 | |
| 50 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 51 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 52 | |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 53 | # tool containers |
Matteo Scandolo | 154295f | 2020-11-23 15:29:02 -0800 | [diff] [blame] | 54 | VOLTHA_TOOLS_VERSION ?= 2.3.1 |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 55 | |
| 56 | 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 |
| 57 | 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 | 154295f | 2020-11-23 15:29:02 -0800 | [diff] [blame] | 58 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/ofagent-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 59 | 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 |
| 60 | 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 |
| 61 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 62 | .PHONY: docker-build local-protos local-lib-go local-voltha help |
| 63 | .DEFAULT_GOAL := help |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 64 | |
| 65 | ## Local Development Helpers |
| 66 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 67 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 68 | ifdef LOCAL_PROTOS |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 69 | rm -rf vendor/github.com/opencord/voltha-protos/go |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 70 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
| 71 | cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go |
| 72 | endif |
| 73 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 74 | ## Local Development Helpers |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 75 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 76 | ifdef LOCAL_LIB_GO |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 77 | rm -rf vendor/github.com/opencord/voltha-lib-go/v4/pkg |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame] | 78 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg |
| 79 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/ |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 80 | endif |
| 81 | |
| 82 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 83 | local-voltha: ## Copies a local version of the voltha-go dependency into the vendor directory |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 84 | ifdef LOCAL_VOLTHA |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 85 | rm -rf vendor/github.com/opencord/voltha-go/ |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 86 | mkdir -p vendor/github.com/opencord/voltha-go/ |
| 87 | cp -rf ${GOPATH}/src/github.com/opencord/voltha-go/ vendor/github.com/opencord/ |
| 88 | rm -rf vendor/github.com/opencord/voltha-go/vendor |
| 89 | endif |
| 90 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 91 | ## Docker targets |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 92 | build: docker-build ## Alias for 'docker-build' |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 93 | |
| 94 | ## Docker targets |
| 95 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 96 | docker-build: local-protos local-voltha local-lib-go ## Build docker image (set BUILD_PROFILED=true to also build the profiled image) |
| 97 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG} -f docker/Dockerfile.ofagent-go . |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 98 | ifdef BUILD_PROFILED |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 99 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.ofagent-go . |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 100 | endif |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 101 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 102 | docker-push: ## Push the docker images to an external repository |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 103 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG} |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 104 | ifdef BUILD_PROFILED |
| 105 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile |
| 106 | endif |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 107 | |
| 108 | ## lint and unit tests |
| 109 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 110 | lint-dockerfile: ## Perform static analysis on Dockerfile |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 111 | @echo "Running Dockerfile lint check..." |
| 112 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 113 | @echo "Dockerfile lint check OK" |
| 114 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 115 | lint-mod: ## Verify the Go dependencies |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 116 | @echo "Running dependency check..." |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 117 | @${GO} mod verify |
| 118 | @echo "Dependency check OK. Running vendor check..." |
| 119 | @git status > /dev/null |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 120 | @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) |
| 121 | @[[ `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 | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 122 | ${GO} mod tidy |
| 123 | ${GO} mod vendor |
| 124 | @git status > /dev/null |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 125 | @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) |
| 126 | @[[ `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) |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 127 | @echo "Vendor check OK." |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 128 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 129 | lint: lint-mod lint-dockerfile ## Run all lint targets |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 130 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 131 | test: local-lib-go ## Run unit tests |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 132 | @mkdir -p ./tests/results |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 133 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 134 | RETURN=$$? ;\ |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 135 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 136 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 137 | exit $$RETURN |
| 138 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 139 | sca: ## Runs static code analysis with the golangci-lint tool |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 140 | @rm -rf ./sca-report |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 141 | @mkdir -p ./sca-report |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 142 | @echo "Running static code analysis..." |
| 143 | @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 144 | @echo "" |
| 145 | @echo "Static code analysis OK" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 146 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 147 | clean: ## Removes any local filesystem artifacts generated by a build |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 148 | rm -rf ./sca-report |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 149 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 150 | distclean: clean ## Removes any local filesystem artifacts generated by a build or test run |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 151 | rm -rf ${VENVDIR} |
| 152 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 153 | mod-update: ## Update go mod files |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 154 | ${GO} mod tidy |
| 155 | ${GO} mod vendor |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame^] | 156 | |
| 157 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
| 158 | help: ## Print help for each Makefile target |
| 159 | @echo "Usage: make [<target>]" |
| 160 | @echo "where available targets are:" |
| 161 | @echo |
| 162 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 163 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |