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) |
Zack Williams | a054c7f | 2020-03-23 10:19:29 -0700 | [diff] [blame] | 32 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-ofagent-go |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 33 | |
| 34 | ## Docker labels. Only set ref and commit date if committed |
| 35 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 36 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 37 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 38 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 39 | |
| 40 | DOCKER_BUILD_ARGS ?= \ |
| 41 | ${DOCKER_EXTRA_ARGS} \ |
| 42 | --build-arg org_label_schema_version="${VERSION}" \ |
| 43 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 44 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 45 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 46 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 48 | |
| 49 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 50 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 51 | |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 52 | # tool containers |
Matteo Scandolo | 154295f | 2020-11-23 15:29:02 -0800 | [diff] [blame] | 53 | VOLTHA_TOOLS_VERSION ?= 2.3.1 |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 54 | |
| 55 | 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 |
| 56 | 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] | 57 | 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] | 58 | 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 |
| 59 | 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 |
| 60 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 61 | .PHONY: local-protos local-voltha |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 62 | |
| 63 | # This should to be the first and default target in this Makefile |
| 64 | help: |
| 65 | @echo "Usage: make [<target>]" |
| 66 | @echo "where available targets are:" |
| 67 | @echo |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 68 | @echo "build : Build ofagent-go docker image" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 69 | @echo "docker-build : Build ofagent-go docker image" |
| 70 | @echo "help : Print this help" |
| 71 | @echo "docker-push : Push the docker images to an external repository" |
| 72 | @echo "lint : Run lint verification, depenancy, gofmt and reference check" |
| 73 | @echo "sca : Runs various SCA through golangci-lint tool" |
| 74 | @echo "test : Run unit tests, if any" |
| 75 | @echo |
| 76 | |
| 77 | |
| 78 | ## Local Development Helpers |
| 79 | |
| 80 | local-protos: |
| 81 | ifdef LOCAL_PROTOS |
| 82 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
| 83 | cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go |
| 84 | endif |
| 85 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 86 | ## Local Development Helpers |
| 87 | local-lib-go: |
| 88 | ifdef LOCAL_LIB_GO |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame] | 89 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg |
| 90 | 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] | 91 | endif |
| 92 | |
| 93 | |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 94 | local-voltha: |
| 95 | ifdef LOCAL_VOLTHA |
| 96 | mkdir -p vendor/github.com/opencord/voltha-go/ |
| 97 | cp -rf ${GOPATH}/src/github.com/opencord/voltha-go/ vendor/github.com/opencord/ |
| 98 | rm -rf vendor/github.com/opencord/voltha-go/vendor |
| 99 | endif |
| 100 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 101 | ## Docker targets |
| 102 | build: docker-build |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 103 | |
| 104 | ## Docker targets |
| 105 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 106 | docker-build: local-protos local-voltha local-lib-go |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 107 | docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG} -f docker/Dockerfile.ofagent-go . |
| 108 | ifdef BUILD_PROFILED |
| 109 | docker build $(DOCKER_BUILD_ARGS) --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.ofagent-go . |
| 110 | endif |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 111 | |
| 112 | docker-push: |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 113 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG} |
Matteo Scandolo | a98f0dc | 2020-05-13 10:39:29 -0700 | [diff] [blame] | 114 | ifdef BUILD_PROFILED |
| 115 | docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile |
| 116 | endif |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 117 | |
| 118 | ## lint and unit tests |
| 119 | |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 120 | lint-dockerfile: |
| 121 | @echo "Running Dockerfile lint check..." |
| 122 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 123 | @echo "Dockerfile lint check OK" |
| 124 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 125 | lint-mod: |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 126 | @echo "Running dependency check..." |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 127 | @${GO} mod verify |
| 128 | @echo "Dependency check OK. Running vendor check..." |
| 129 | @git status > /dev/null |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 130 | @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) |
| 131 | @[[ `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] | 132 | ${GO} mod tidy |
| 133 | ${GO} mod vendor |
| 134 | @git status > /dev/null |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 135 | @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) |
| 136 | @[[ `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] | 137 | @echo "Vendor check OK." |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 138 | |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 139 | lint: lint-mod lint-dockerfile |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 140 | |
divyadesai | 892fab5 | 2020-03-24 08:45:09 +0000 | [diff] [blame] | 141 | test: local-lib-go |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 142 | @mkdir -p ./tests/results |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 143 | @${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] | 144 | RETURN=$$? ;\ |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 145 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 146 | ${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] | 147 | exit $$RETURN |
| 148 | |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 149 | sca: |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 150 | @rm -rf ./sca-report |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 151 | @mkdir -p ./sca-report |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 152 | @echo "Running static code analysis..." |
| 153 | @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 154 | @echo "" |
| 155 | @echo "Static code analysis OK" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 156 | |
| 157 | clean: |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 158 | rm -rf ./sca-report |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 159 | |
| 160 | distclean: clean |
| 161 | rm -rf ${VENVDIR} |
| 162 | |
Kent Hagerman | 6379e09 | 2020-02-18 18:05:57 -0500 | [diff] [blame] | 163 | mod-update: |
| 164 | ${GO} mod tidy |
| 165 | ${GO} mod vendor |