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} |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 33 | |
| 34 | ## Docker labels. Only set ref and commit date if committed |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 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) |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 39 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 40 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 41 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 42 | # force module usage. This affects commands executed from this Makefile, but |
| 43 | # not the environment inside the Docker build (which does not build from |
| 44 | # inside a GOPATH). |
| 45 | export GO111MODULE=on |
| 46 | |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 47 | DOCKER_BUILD_ARGS ?= \ |
| 48 | ${DOCKER_EXTRA_ARGS} \ |
| 49 | --build-arg org_label_schema_version="${VERSION}" \ |
| 50 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 51 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 52 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 53 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 54 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 55 | |
David K. Bainbridge | 8eb6cd6 | 2019-08-22 13:17:29 -0700 | [diff] [blame] | 56 | .PHONY: docker-build local-protos local-voltha |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 57 | |
| 58 | # This should to be the first and default target in this Makefile |
| 59 | help: |
| 60 | @echo "Usage: make [<target>]" |
| 61 | @echo "where available targets are:" |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 62 | @echo "" |
| 63 | @echo "clean : Removes any local filesystem artifacts generated by a build" |
| 64 | @echo "distclean : Removes any local filesystem artifacts generated by a build or test run" |
David Bainbridge | 63d5181 | 2019-10-22 15:55:29 +0000 | [diff] [blame^] | 65 | @echo "build : Build all openolt adapter artifacts" |
Zack Williams | 95504be | 2019-09-03 14:17:24 -0700 | [diff] [blame] | 66 | @echo "docker-build : Build openolt adapter docker image" |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 67 | @echo "docker-push : Push the docker images to an external repository" |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 68 | @echo "help : Print this help" |
| 69 | @echo "lint : Run all lint targets" |
| 70 | @echo "lint-mod : Verify the Go dependencies" |
| 71 | @echo "lint-sanity : Run the Go language sanity tests (vet)" |
| 72 | @echo "lint-style : Verify the Go standard format of the source" |
| 73 | @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory" |
| 74 | @echo "local-voltha : Copies a local version of the VOTLHA dependencies into the vendor directory" |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 75 | @echo "sca : Runs various SCA through golangci-lint tool" |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 76 | @echo "test : Run unit tests, if any" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 77 | @echo |
| 78 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 79 | ## Local Development Helpers |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 80 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 81 | local-protos: |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 82 | ifdef LOCAL_PROTOS |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 83 | rm -rf vendor/github.com/opencord/voltha-protos/go |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 84 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 85 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/go |
| 86 | rm -rf vendor/github.com/opencord/voltha-protos/go/vendor |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 87 | endif |
| 88 | |
David K. Bainbridge | 8eb6cd6 | 2019-08-22 13:17:29 -0700 | [diff] [blame] | 89 | local-voltha: |
| 90 | ifdef LOCAL_VOLTHA |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 91 | rm -rf vendor/github.com/opencord/voltha-go |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 92 | mkdir -p vendor/github.com/opencord/voltha-go/ |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 93 | cp -rf ${LOCAL_VOLTHA} vendor/github.com/opencord/ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 94 | rm -rf vendor/github.com/opencord/voltha-go/vendor |
| 95 | endif |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 96 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 97 | ## Docker targets |
| 98 | |
David Bainbridge | 63d5181 | 2019-10-22 15:55:29 +0000 | [diff] [blame^] | 99 | build: docker-build |
| 100 | |
Zack Williams | 95504be | 2019-09-03 14:17:24 -0700 | [diff] [blame] | 101 | docker-build: local-protos local-voltha |
David K. Bainbridge | 8eb6cd6 | 2019-08-22 13:17:29 -0700 | [diff] [blame] | 102 | docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt . |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 103 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 104 | docker-push: |
| 105 | docker push ${ADAPTER_IMAGENAME} |
| 106 | |
Matt Jeanneret | 8b823f6 | 2019-05-11 11:01:28 -0400 | [diff] [blame] | 107 | ## lint and unit tests |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 108 | |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 109 | lint-style: |
| 110 | ifeq (,$(shell which gofmt)) |
| 111 | go get -u github.com/golang/go/src/cmd/gofmt |
| 112 | endif |
| 113 | @echo "Running style check..." |
| 114 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 115 | if [ ! -z "$$gofmt_out" ]; then \ |
| 116 | echo "$$gofmt_out" ;\ |
| 117 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 118 | exit 1 ;\ |
| 119 | fi |
| 120 | @echo "Style check OK" |
| 121 | |
| 122 | lint-sanity: |
| 123 | @echo "Running sanity check..." |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 124 | @go vet -mod=vendor ./... |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 125 | @echo "Sanity check OK" |
| 126 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 127 | lint-mod: |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 128 | @echo "Running dependency check..." |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 129 | @go mod verify |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 130 | @echo "Dependency check OK" |
| 131 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 132 | lint: lint-style lint-sanity lint-mod |
| 133 | |
| 134 | __GOPATH=$(shell go env GOPATH) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 135 | |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 136 | # Rules to automatically install golangci-lint |
| 137 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
| 138 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 139 | GOLANGCI_LINT_TOOL=$(__GOPATH)/bin/golangci-lint |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 140 | golangci_lint_tool_install: |
| 141 | # Same version as installed by Jenkins ci-management |
| 142 | # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 143 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(__GOPATH)/bin v1.17.0 |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 144 | else |
| 145 | golangci_lint_tool_install: |
| 146 | endif |
| 147 | |
| 148 | # Rules to automatically install go-junit-report |
| 149 | GO_JUNIT_REPORT?=$(shell which go-junit-report) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 150 | ifeq (,$(GO_JUNIT_REPORT)) |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 151 | GO_JUNIT_REPORT=$(__GOPATH)/bin/go-junit-report |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 152 | go_junit_install: |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 153 | go get -u github.com/jstemmer/go-junit-report |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 154 | else |
| 155 | go_junit_install: |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 156 | endif |
| 157 | |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 158 | # Rules to automatically install gocover-covertura |
| 159 | GOCOVER_COBERTURA?=$(shell which gocover-cobertura) |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 160 | ifeq (,$(GOCOVER_COBERTURA)) |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 161 | @GOCOVER_COBERTURA=$(__GOPATH)/bin/gocover-cobertura |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 162 | gocover_cobertura_install: |
| 163 | go get -u github.com/t-yuki/gocover-cobertura |
| 164 | else |
| 165 | gocover_cobertura_install: |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 166 | endif |
| 167 | |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 168 | test: go_junit_install gocover_cobertura_install |
Matt Jeanneret | 384d8c9 | 2019-05-06 14:27:31 -0400 | [diff] [blame] | 169 | @mkdir -p ./tests/results |
| 170 | |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 171 | @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] | 172 | RETURN=$$? ;\ |
| 173 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 174 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 175 | exit $$RETURN |
| 176 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 177 | GOLANGCI_LINT_TOOL:=$(shell which golangci-lint) |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 178 | sca: golangci_lint_tool_install |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 179 | @mkdir -p ./sca-report |
David Bainbridge | 788e520 | 2019-10-21 18:49:40 +0000 | [diff] [blame] | 180 | golangci-lint run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 181 | |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 182 | clean: |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 183 | |
| 184 | distclean: clean |
Scott Baker | a4cd50d | 2019-10-15 16:53:38 -0700 | [diff] [blame] | 185 | rm -rf ./sca_report |
Matt Jeanneret | b5bf10e | 2019-05-18 15:02:51 -0400 | [diff] [blame] | 186 | |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 187 | # end file |