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 |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 54 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
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) |
David K. Bainbridge | b02c6b9 | 2021-04-14 23:36:08 +0000 | [diff] [blame] | 97 | docker build $(DOCKER_BUILD_ARGS) --build-arg CGO_PARAMETER=0 --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 | b02c6b9 | 2021-04-14 23:36:08 +0000 | [diff] [blame] | 99 | docker build $(DOCKER_BUILD_ARGS) --build-arg CGO_PARAMETER=1 --target=dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.ofagent-go . |
| 100 | endif |
| 101 | ifdef BUILD_RACE |
| 102 | docker build $(DOCKER_BUILD_ARGS) --build-arg CGO_PARAMETER=1 --target=dev --build-arg EXTRA_GO_BUILD_TAGS="--race" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-rd -f docker/Dockerfile.ofagent-go . |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 103 | endif |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 104 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 105 | docker-push: ## Push the docker images to an external repository |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 106 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG} |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 107 | ifdef BUILD_PROFILED |
| 108 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile |
| 109 | endif |
David K. Bainbridge | b02c6b9 | 2021-04-14 23:36:08 +0000 | [diff] [blame] | 110 | ifdef BUILD_RACE |
| 111 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-rd |
| 112 | endif |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 113 | |
| 114 | ## lint and unit tests |
| 115 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 116 | lint-dockerfile: ## Perform static analysis on Dockerfile |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 117 | @echo "Running Dockerfile lint check..." |
| 118 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 119 | @echo "Dockerfile lint check OK" |
| 120 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 121 | lint-mod: ## Verify the Go dependencies |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 122 | @echo "Running dependency check..." |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 123 | @${GO} mod verify |
| 124 | @echo "Dependency check OK. Running vendor check..." |
| 125 | @git status > /dev/null |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 126 | @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) |
| 127 | @[[ `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] | 128 | ${GO} mod tidy |
| 129 | ${GO} mod vendor |
| 130 | @git status > /dev/null |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 131 | @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) |
| 132 | @[[ `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] | 133 | @echo "Vendor check OK." |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 134 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 135 | lint: lint-mod lint-dockerfile ## Run all lint targets |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 136 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 137 | test: local-lib-go ## Run unit tests |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 138 | @mkdir -p ./tests/results |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 139 | @${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] | 140 | RETURN=$$? ;\ |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 141 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 142 | ${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] | 143 | exit $$RETURN |
| 144 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 145 | sca: ## Runs static code analysis with the golangci-lint tool |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 146 | @rm -rf ./sca-report |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 147 | @mkdir -p ./sca-report |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 148 | @echo "Running static code analysis..." |
| 149 | @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 150 | @echo "" |
| 151 | @echo "Static code analysis OK" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 152 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 153 | clean: ## Removes any local filesystem artifacts generated by a build |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 154 | rm -rf ./sca-report |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 155 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 156 | 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] | 157 | rm -rf ${VENVDIR} |
| 158 | |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 159 | mod-update: ## Update go mod files |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 160 | ${GO} mod tidy |
| 161 | ${GO} mod vendor |
David K. Bainbridge | 3d5ac93 | 2021-04-06 18:08:35 +0000 | [diff] [blame] | 162 | |
| 163 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
| 164 | help: ## Print help for each Makefile target |
| 165 | @echo "Usage: make [<target>]" |
| 166 | @echo "where available targets are:" |
| 167 | @echo |
| 168 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 169 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |