blob: df0fcaeadb8226d8b05f7980e4bda50555ae8742 [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}
Andrea Campanellacbf583e2019-12-17 15:26:00 -080033TYPE ?= minimal
Matt Jeanneret8b823f62019-05-11 11:01:28 -040034
35## Docker labels. Only set ref and commit date if committed
Matt Jeanneretf880eb62019-07-16 20:08:03 -040036DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
37DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
38DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
39DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret8b823f62019-05-11 11:01:28 -040040
Matt Jeanneretf880eb62019-07-16 20:08:03 -040041DOCKER_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
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050050# tool containers
51VOLTHA_TOOLS_VERSION ?= 1.0.3
52
53GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
54GO_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
55GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
56GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
57GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
58HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
59
Scott Baker355d1742019-10-24 10:57:52 -070060.PHONY: docker-build local-protos local-lib-go
Phaneendra Manda4c62c802019-03-06 21:37:49 +053061
62# This should to be the first and default target in this Makefile
63help:
64 @echo "Usage: make [<target>]"
65 @echo "where available targets are:"
David Bainbridge788e5202019-10-21 18:49:40 +000066 @echo ""
67 @echo "clean : Removes any local filesystem artifacts generated by a build"
68 @echo "distclean : Removes any local filesystem artifacts generated by a build or test run"
David Bainbridge63d51812019-10-22 15:55:29 +000069 @echo "build : Build all openolt adapter artifacts"
Zack Williams95504be2019-09-03 14:17:24 -070070 @echo "docker-build : Build openolt adapter docker image"
Matt Jeanneret8b823f62019-05-11 11:01:28 -040071 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge788e5202019-10-21 18:49:40 +000072 @echo "help : Print this help"
73 @echo "lint : Run all lint targets"
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050074 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
David Bainbridge788e5202019-10-21 18:49:40 +000075 @echo "lint-mod : Verify the Go dependencies"
76 @echo "lint-sanity : Run the Go language sanity tests (vet)"
77 @echo "lint-style : Verify the Go standard format of the source"
78 @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory"
Scott Baker355d1742019-10-24 10:57:52 -070079 @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070080 @echo "sca : Runs various SCA through golangci-lint tool"
Matt Jeanneret8b823f62019-05-11 11:01:28 -040081 @echo "test : Run unit tests, if any"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053082 @echo
83
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040084## Local Development Helpers
Phaneendra Manda4c62c802019-03-06 21:37:49 +053085
Matt Jeanneret8b823f62019-05-11 11:01:28 -040086local-protos:
William Kurkianea869482019-04-09 15:16:11 -040087ifdef LOCAL_PROTOS
Esin Karamanccb714b2019-11-29 15:02:06 +000088 rm -rf vendor/github.com/opencord/voltha-protos/v3/go
89 mkdir -p vendor/github.com/opencord/voltha-protos/v3/go
90 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go
91 rm -rf vendor/github.com/opencord/voltha-protos/v3/go/vendor
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040092endif
93
Scott Baker355d1742019-10-24 10:57:52 -070094## Local Development Helpers
95local-lib-go:
96ifdef LOCAL_LIB_GO
Esin Karamanccb714b2019-11-29 15:02:06 +000097 mkdir -p vendor/github.com/opencord/voltha-lib-go/v3/pkg
98 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v3/pkg/
William Kurkianea869482019-04-09 15:16:11 -040099endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400100
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400101## Docker targets
102
David Bainbridge63d51812019-10-22 15:55:29 +0000103build: docker-build
104
Scott Baker355d1742019-10-24 10:57:52 -0700105docker-build: local-protos local-lib-go
David K. Bainbridge8eb6cd62019-08-22 13:17:29 -0700106 docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt .
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400107
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400108docker-push:
109 docker push ${ADAPTER_IMAGENAME}
110
Andrea Campanellacbf583e2019-12-17 15:26:00 -0800111docker-kind-load:
112 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
113 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
114
115
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400116## lint and unit tests
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530117
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500118lint-dockerfile:
119 @echo "Running Dockerfile lint check ..."
120 @${HADOLINT} $$(find . -name "Dockerfile.*")
121 @echo "Dockerfile lint check OK"
122
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400123lint-style:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400124 @echo "Running style check..."
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500125 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400126 if [ ! -z "$$gofmt_out" ]; then \
127 echo "$$gofmt_out" ;\
128 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
129 exit 1 ;\
130 fi
131 @echo "Style check OK"
132
133lint-sanity:
134 @echo "Running sanity check..."
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500135 @${GO} vet -mod=vendor ./...
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400136 @echo "Sanity check OK"
137
David Bainbridge788e5202019-10-21 18:49:40 +0000138lint-mod:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400139 @echo "Running dependency check..."
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500140 @${GO} mod verify
Scott Baker22c0e9c2019-11-26 08:11:29 -0800141 @echo "Dependency check OK. Running vendor check..."
142 @git status > /dev/null
143 @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)
144 @[[ `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)
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500145 ${GO} mod tidy
146 ${GO} mod vendor
Scott Baker22c0e9c2019-11-26 08:11:29 -0800147 @git status > /dev/null
148 @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)
149 @[[ `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)
150 @echo "Vendor check OK."
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400151
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500152lint: lint-style lint-sanity lint-mod lint-dockerfile
David Bainbridge788e5202019-10-21 18:49:40 +0000153
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500154test:
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400155 @mkdir -p ./tests/results
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500156 @${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 -0400157 RETURN=$$? ;\
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500158 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
159 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400160 exit $$RETURN
161
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500162sca:
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700163 @mkdir -p ./sca-report
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500164 @echo "Running static code analysis..."
165 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
166 @echo "Static code analysis OK"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500168clean: distclean
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400169
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500170distclean:
171 rm -rf ./sca-report
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400172
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500173mod-update:
174 ${GO} mod tidy
175 ${GO} mod vendor