blob: f31de4d8228671a8e5b86738767d0d49c468420f [file] [log] [blame]
Phaneendra Manda4c62c802019-03-06 21:37:49 +05301#
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 Jeanneret8b823f62019-05-11 11:01:28 -040017# set default shell
18SHELL = bash -e -o pipefail
19
20# Variables
21VERSION ?= $(shell cat ./VERSION)
22
Matt Jeanneretf880eb62019-07-16 20:08:03 -040023DOCKER_LABEL_VCS_DIRTY = false
24ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
25 DOCKER_LABEL_VCS_DIRTY = true
26endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040027## Docker related
Matt Jeanneretf880eb62019-07-16 20:08:03 -040028DOCKER_EXTRA_ARGS ?=
Matt Jeanneret8b823f62019-05-11 11:01:28 -040029DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
Matt Jeanneret8b823f62019-05-11 11:01:28 -040031DOCKER_TAG ?= ${VERSION}
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040032ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG}
Matt Jeanneret8b823f62019-05-11 11:01:28 -040033
34## Docker labels. Only set ref and commit date if committed
Matt Jeanneretf880eb62019-07-16 20:08:03 -040035DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
36DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
37DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
38DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret8b823f62019-05-11 11:01:28 -040039
David Bainbridge788e5202019-10-21 18:49:40 +000040# 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).
45export GO111MODULE=on
46
Matt Jeanneretf880eb62019-07-16 20:08:03 -040047DOCKER_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
Scott Baker355d1742019-10-24 10:57:52 -070056.PHONY: docker-build local-protos local-lib-go
Phaneendra Manda4c62c802019-03-06 21:37:49 +053057
58# This should to be the first and default target in this Makefile
59help:
60 @echo "Usage: make [<target>]"
61 @echo "where available targets are:"
David Bainbridge788e5202019-10-21 18:49:40 +000062 @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 Bainbridge63d51812019-10-22 15:55:29 +000065 @echo "build : Build all openolt adapter artifacts"
Zack Williams95504be2019-09-03 14:17:24 -070066 @echo "docker-build : Build openolt adapter docker image"
Matt Jeanneret8b823f62019-05-11 11:01:28 -040067 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge788e5202019-10-21 18:49:40 +000068 @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"
Scott Baker355d1742019-10-24 10:57:52 -070074 @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070075 @echo "sca : Runs various SCA through golangci-lint tool"
Matt Jeanneret8b823f62019-05-11 11:01:28 -040076 @echo "test : Run unit tests, if any"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053077 @echo
78
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040079## Local Development Helpers
Phaneendra Manda4c62c802019-03-06 21:37:49 +053080
Matt Jeanneret8b823f62019-05-11 11:01:28 -040081local-protos:
William Kurkianea869482019-04-09 15:16:11 -040082ifdef LOCAL_PROTOS
William Kurkiane4996f32019-11-13 15:47:43 -050083 rm -rf vendor/github.com/opencord/voltha-protos/v2/go
84 mkdir -p vendor/github.com/opencord/voltha-protos/v2/go
85 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v2/go
86 rm -rf vendor/github.com/opencord/voltha-protos/v2/go/vendor
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040087endif
88
Scott Baker355d1742019-10-24 10:57:52 -070089## Local Development Helpers
90local-lib-go:
91ifdef LOCAL_LIB_GO
Scott Baker5e312072019-10-25 13:00:25 -070092 mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg
93 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/
William Kurkianea869482019-04-09 15:16:11 -040094endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040095
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040096## Docker targets
97
David Bainbridge63d51812019-10-22 15:55:29 +000098build: docker-build
99
Scott Baker355d1742019-10-24 10:57:52 -0700100docker-build: local-protos local-lib-go
David K. Bainbridge8eb6cd62019-08-22 13:17:29 -0700101 docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt .
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400102
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400103docker-push:
104 docker push ${ADAPTER_IMAGENAME}
105
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400106## lint and unit tests
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530107
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400108lint-style:
109ifeq (,$(shell which gofmt))
110 go get -u github.com/golang/go/src/cmd/gofmt
111endif
112 @echo "Running style check..."
113 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
114 if [ ! -z "$$gofmt_out" ]; then \
115 echo "$$gofmt_out" ;\
116 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
117 exit 1 ;\
118 fi
119 @echo "Style check OK"
120
121lint-sanity:
122 @echo "Running sanity check..."
David Bainbridge788e5202019-10-21 18:49:40 +0000123 @go vet -mod=vendor ./...
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400124 @echo "Sanity check OK"
125
David Bainbridge788e5202019-10-21 18:49:40 +0000126lint-mod:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400127 @echo "Running dependency check..."
David Bainbridge788e5202019-10-21 18:49:40 +0000128 @go mod verify
Scott Baker22c0e9c2019-11-26 08:11:29 -0800129 @echo "Dependency check OK. Running vendor check..."
130 @git status > /dev/null
131 @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)
132 @[[ `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)
133 go mod tidy
134 go mod vendor
135 @git status > /dev/null
136 @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)
137 @[[ `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)
138 @echo "Vendor check OK."
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400139
David Bainbridge788e5202019-10-21 18:49:40 +0000140lint: lint-style lint-sanity lint-mod
141
142__GOPATH=$(shell go env GOPATH)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400143
Scott Bakera4cd50d2019-10-15 16:53:38 -0700144# Rules to automatically install golangci-lint
145GOLANGCI_LINT_TOOL?=$(shell which golangci-lint)
146ifeq (,$(GOLANGCI_LINT_TOOL))
David Bainbridge788e5202019-10-21 18:49:40 +0000147GOLANGCI_LINT_TOOL=$(__GOPATH)/bin/golangci-lint
Scott Bakera4cd50d2019-10-15 16:53:38 -0700148golangci_lint_tool_install:
149 # Same version as installed by Jenkins ci-management
150 # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint
David Bainbridge788e5202019-10-21 18:49:40 +0000151 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(__GOPATH)/bin v1.17.0
Scott Bakera4cd50d2019-10-15 16:53:38 -0700152else
153golangci_lint_tool_install:
154endif
155
156# Rules to automatically install go-junit-report
157GO_JUNIT_REPORT?=$(shell which go-junit-report)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400158ifeq (,$(GO_JUNIT_REPORT))
David Bainbridge788e5202019-10-21 18:49:40 +0000159GO_JUNIT_REPORT=$(__GOPATH)/bin/go-junit-report
Scott Bakera4cd50d2019-10-15 16:53:38 -0700160go_junit_install:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400161 go get -u github.com/jstemmer/go-junit-report
Scott Bakera4cd50d2019-10-15 16:53:38 -0700162else
163go_junit_install:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400164endif
165
Scott Bakera4cd50d2019-10-15 16:53:38 -0700166# Rules to automatically install gocover-covertura
167GOCOVER_COBERTURA?=$(shell which gocover-cobertura)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400168ifeq (,$(GOCOVER_COBERTURA))
David Bainbridge788e5202019-10-21 18:49:40 +0000169 @GOCOVER_COBERTURA=$(__GOPATH)/bin/gocover-cobertura
Scott Bakera4cd50d2019-10-15 16:53:38 -0700170gocover_cobertura_install:
171 go get -u github.com/t-yuki/gocover-cobertura
172else
173gocover_cobertura_install:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400174endif
175
Scott Bakera4cd50d2019-10-15 16:53:38 -0700176test: go_junit_install gocover_cobertura_install
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400177 @mkdir -p ./tests/results
178
David Bainbridge788e5202019-10-21 18:49:40 +0000179 @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400180 RETURN=$$? ;\
181 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
182 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
183 exit $$RETURN
184
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185GOLANGCI_LINT_TOOL:=$(shell which golangci-lint)
Scott Bakera4cd50d2019-10-15 16:53:38 -0700186sca: golangci_lint_tool_install
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 @mkdir -p ./sca-report
David Bainbridge788e5202019-10-21 18:49:40 +0000188 golangci-lint run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700189
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400190clean:
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400191
192distclean: clean
Scott Bakera4cd50d2019-10-15 16:53:38 -0700193 rm -rf ./sca_report
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400194
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530195# end file