khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -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 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 17 | # set default shell |
| 18 | SHELL = bash -e -o pipefail |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 19 | |
Zack Williams | 27f59a4 | 2019-05-10 09:12:07 -0700 | [diff] [blame] | 20 | # Variables |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 21 | VERSION ?= $(shell cat ./VERSION) |
Zack Williams | 27f59a4 | 2019-05-10 09:12:07 -0700 | [diff] [blame] | 22 | |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -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 | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 27 | ## Docker related |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame] | 28 | DOCKER_EXTRA_ARGS ?= |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
| 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 32 | RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core |
Andrea Campanella | 7a6cce2 | 2019-12-17 14:10:27 -0800 | [diff] [blame] | 33 | TYPE ?= minimal |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 34 | |
| 35 | ## Docker labels. Only set ref and commit date if committed |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 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) |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 40 | |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 41 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 42 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 43 | # force module usage. This affects commands executed from this Makefile, but |
| 44 | # not the environment inside the Docker build (which does not build from |
| 45 | # inside a GOPATH). |
| 46 | export GO111MODULE=on |
| 47 | |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 48 | DOCKER_BUILD_ARGS ?= \ |
David K. Bainbridge | e14914d | 2019-05-24 13:43:05 -0700 | [diff] [blame] | 49 | ${DOCKER_EXTRA_ARGS} \ |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 50 | --build-arg org_label_schema_version="${VERSION}" \ |
| 51 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 52 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 53 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 54 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 55 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 56 | |
| 57 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 58 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 59 | |
Kent Hagerman | d5e8bd5 | 2020-01-30 16:30:16 -0500 | [diff] [blame^] | 60 | .PHONY: rw_core local-protos |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 61 | |
| 62 | # This should to be the first and default target in this Makefile |
| 63 | help: |
| 64 | @echo "Usage: make [<target>]" |
| 65 | @echo "where available targets are:" |
| 66 | @echo |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 67 | @echo "build : Build the docker images." |
| 68 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 69 | @echo "rw_core : Build the rw_core docker image" |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 70 | @echo "clean : Remove files created by the build and tests" |
Zack Williams | b9b1afb | 2019-12-09 15:28:52 -0700 | [diff] [blame] | 71 | @echo "distclean : Remove sca directory and clean" |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 72 | @echo "docker-push : Push the docker images to an external repository" |
David Bainbridge | 5f3619c | 2019-07-10 22:51:09 +0000 | [diff] [blame] | 73 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 74 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 75 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 76 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 77 | @echo "lint : Shorthand for lint-style & lint-sanity" |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 78 | @echo "sca : Runs various SCA through golangci-lint tool" |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 79 | @echo "test : Generate reports for all go tests" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 80 | @echo |
| 81 | |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 82 | ## Local Development Helpers |
| 83 | local-protos: |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 84 | ifdef LOCAL_PROTOS |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 85 | mkdir -p vendor/github.com/opencord/voltha-protos/v3/go |
| 86 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 87 | endif |
| 88 | |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 89 | ## Local Development Helpers |
| 90 | local-lib-go: |
| 91 | ifdef LOCAL_LIB_GO |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 92 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v3/pkg |
| 93 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v3/pkg/ |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 94 | endif |
| 95 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 96 | ## Docker targets |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 97 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 98 | build: docker-build |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 99 | |
Kent Hagerman | d5e8bd5 | 2020-01-30 16:30:16 -0500 | [diff] [blame^] | 100 | docker-build: rw_core |
sslobodr | a3ea7d4 | 2019-01-16 15:03:16 -0500 | [diff] [blame] | 101 | |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 102 | rw_core: local-protos local-lib-go |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 103 | docker build $(DOCKER_BUILD_ARGS) -t ${RWCORE_IMAGENAME}:${DOCKER_TAG} -t ${RWCORE_IMAGENAME}:latest -f docker/Dockerfile.rw_core . |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 104 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 105 | docker-push: |
Kent Hagerman | f4a3aab | 2019-05-22 14:42:34 -0400 | [diff] [blame] | 106 | docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG} |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 107 | |
Andrea Campanella | 7a6cce2 | 2019-12-17 14:10:27 -0800 | [diff] [blame] | 108 | docker-kind-load: |
| 109 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 110 | kind load docker-image ${RWCORE_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev) |
Andrea Campanella | 7a6cce2 | 2019-12-17 14:10:27 -0800 | [diff] [blame] | 111 | |
Matt Jeanneret | 2e3051a | 2019-05-11 15:01:46 -0400 | [diff] [blame] | 112 | ## lint and unit tests |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 113 | |
David Bainbridge | 5f3619c | 2019-07-10 22:51:09 +0000 | [diff] [blame] | 114 | PATH:=$(GOPATH)/bin:$(PATH) |
| 115 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 116 | lint-dockerfile: |
| 117 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 118 | mkdir -p $(GOPATH)/bin |
| 119 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 120 | chmod 755 $(GOPATH)/bin/hadolint |
| 121 | endif |
| 122 | @echo "Running Dockerfile lint check ..." |
| 123 | @hadolint $$(find . -name "Dockerfile.*") |
| 124 | @echo "Dockerfile lint check OK" |
| 125 | |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 126 | lint-style: |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 127 | ifeq (,$(shell which gofmt)) |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 128 | go get -u github.com/golang/go/src/cmd/gofmt |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 129 | endif |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 130 | @echo "Running style check..." |
| 131 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 132 | if [ ! -z "$$gofmt_out" ]; then \ |
| 133 | echo "$$gofmt_out" ;\ |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 134 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 135 | exit 1 ;\ |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 136 | fi |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 137 | @echo "Style check OK" |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 138 | |
| 139 | lint-sanity: |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 140 | @echo "Running sanity check..." |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 141 | @go vet -mod=vendor ./... |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 142 | @echo "Sanity check OK" |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 143 | |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 144 | lint-mod: |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 145 | @echo "Running dependency check..." |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 146 | @go mod verify |
Scott Baker | e4c2a98 | 2019-11-21 16:32:03 -0800 | [diff] [blame] | 147 | @echo "Dependency check OK. Running vendor check..." |
| 148 | @git status > /dev/null |
| 149 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1) |
| 150 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1) |
| 151 | go mod tidy |
| 152 | go mod vendor |
| 153 | @git status > /dev/null |
| 154 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 155 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 156 | @echo "Vendor check OK." |
| 157 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 158 | |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 159 | lint: lint-style lint-sanity lint-mod lint-dockerfile |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 160 | |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 161 | # Rules to automatically install golangci-lint |
| 162 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
| 163 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
| 164 | GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint |
| 165 | golangci_lint_tool_install: |
| 166 | # Same version as installed by Jenkins ci-management |
| 167 | # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint |
| 168 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0 |
| 169 | else |
| 170 | golangci_lint_tool_install: |
| 171 | endif |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 172 | |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 173 | # Rules to automatically install go-junit-report |
| 174 | GO_JUNIT_REPORT?=$(shell which go-junit-report) |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 175 | ifeq (,$(GO_JUNIT_REPORT)) |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 176 | GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 177 | go_junit_install: |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 178 | go get -u github.com/jstemmer/go-junit-report |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 179 | else |
| 180 | go_junit_install: |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 181 | endif |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 182 | |
| 183 | # Rules to automatically install gocover-covertura |
| 184 | GOCOVER_COBERTURA?=$(shell which gocover-cobertura) |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 185 | ifeq (,$(GOCOVER_COBERTURA)) |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 186 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 187 | gocover_cobertura_install: |
| 188 | go get -u github.com/t-yuki/gocover-cobertura |
| 189 | else |
| 190 | gocover_cobertura_install: |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 191 | endif |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 192 | |
| 193 | sca: golangci_lint_tool_install |
| 194 | rm -rf ./sca-report |
| 195 | @mkdir -p ./sca-report |
Kent Hagerman | d5e8bd5 | 2020-01-30 16:30:16 -0500 | [diff] [blame^] | 196 | $(GOLANGCI_LINT_TOOL) run --deadline=4m -E golint --out-format junit-xml ./rw_core/... ./tests/... ./common/... ./db/... 2>&1 | tee ./sca-report/sca-report.xml |
Scott Baker | 18ea5a4 | 2019-10-15 16:08:42 -0700 | [diff] [blame] | 197 | |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 198 | test: go_junit_install gocover_cobertura_install local-lib-go |
Kent Hagerman | ca4c51e | 2019-05-02 12:28:55 -0400 | [diff] [blame] | 199 | @mkdir -p ./tests/results |
girishk | 5259f8e | 2019-10-10 18:44:44 +0000 | [diff] [blame] | 200 | @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Zack Williams | 7cf7800 | 2019-04-30 21:45:35 -0700 | [diff] [blame] | 201 | RETURN=$$? ;\ |
| 202 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 203 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 204 | exit $$RETURN |
Kent Hagerman | 074d0e0 | 2019-04-24 17:58:16 -0400 | [diff] [blame] | 205 | |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 206 | clean: |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 207 | |
| 208 | distclean: clean |
Zack Williams | b9b1afb | 2019-12-09 15:28:52 -0700 | [diff] [blame] | 209 | rm -rf ./sca_report |
David K. Bainbridge | 1678e19 | 2019-05-17 11:48:29 -0700 | [diff] [blame] | 210 | |
Scott Baker | cb7c88a | 2019-10-16 18:32:48 -0700 | [diff] [blame] | 211 | mod-update: |
| 212 | go mod tidy |
| 213 | go mod vendor |
| 214 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 215 | # end file |