blob: 9ccda967ef6a3deb1f8ca65b10eb24bebe8bef4e [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
23## Docker related
24DOCKER_REGISTRY ?=
25DOCKER_REPOSITORY ?=
26DOCKER_BUILD_ARGS ?=
27DOCKER_TAG ?= ${VERSION}
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040028GOADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG}-go
29PYTHONADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG}-py
30ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG}
Matt Jeanneret8b823f62019-05-11 11:01:28 -040031
32## Docker labels. Only set ref and commit date if committed
33DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
34DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
35DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
36
37ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
38 DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
39else
40 DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)+dirty
Phaneendra Manda4c62c802019-03-06 21:37:49 +053041endif
42
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040043.PHONY: docker-build openolt_go openolt_python local-protos local-volthago local-pyvoltha
Phaneendra Manda4c62c802019-03-06 21:37:49 +053044
45# This should to be the first and default target in this Makefile
46help:
47 @echo "Usage: make [<target>]"
48 @echo "where available targets are:"
49 @echo
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040050 @echo "build : Build both openolt adapter docker images"
51 @echo "openolt_go : Build Golang openolt adapter docker image"
52 @echo "openolt_python : Build Python openolt adapter docker image"
Matt Jeanneret8b823f62019-05-11 11:01:28 -040053 @echo "help : Print this help"
54 @echo "docker-push : Push the docker images to an external repository"
55 @echo "lint : Run lint verification, depenancy, gofmt and reference check"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070056 @echo "sca : Runs various SCA through golangci-lint tool"
Matt Jeanneret8b823f62019-05-11 11:01:28 -040057 @echo "test : Run unit tests, if any"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053058 @echo
59
60
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040061## Local Development Helpers
Phaneendra Manda4c62c802019-03-06 21:37:49 +053062
Matt Jeanneret8b823f62019-05-11 11:01:28 -040063local-protos:
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040064 mkdir -p python/local_imports
William Kurkianea869482019-04-09 15:16:11 -040065ifdef LOCAL_PROTOS
66 mkdir -p vendor/github.com/opencord/voltha-protos/go
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040067 cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go
68 mkdir -p python/local_imports/voltha-protos/dist
69 cp ../voltha-protos/dist/*.tar.gz python/local_imports/voltha-protos/dist/
70endif
71
72local-pyvoltha:
73 mkdir -p python/local_imports
74ifdef LOCAL_PYVOLTHA
75 mkdir -p python/local_imports/pyvoltha/dist
76 cp ../pyvoltha/dist/*.tar.gz python/local_imports/pyvoltha/dist/
William Kurkianea869482019-04-09 15:16:11 -040077endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040078
79local-volthago:
William Kurkianea869482019-04-09 15:16:11 -040080ifdef LOCAL_VOLTHAGO
81 mkdir -p vendor/github.com/opencord/voltha-go/
82 cp -rf ${GOPATH}/src/github.com/opencord/voltha-go/ vendor/github.com/opencord/
83 rm -rf vendor/github.com/opencord/voltha-go/vendor
84endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040085
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040086
87## Python venv dev environment
88
89VENVDIR := python/venv-openolt
90
91venv: distclean local-protos local-pyvoltha
92 virtualenv ${VENVDIR};\
93 source ./${VENVDIR}/bin/activate ; set -u ;\
94 rm ${VENVDIR}/local/bin ${VENVDIR}/local/lib ${VENVDIR}/local/include ;\
95 pip install -r python/requirements.txt
96
97ifdef LOCAL_PYVOLTHA
98 source ./${VENVDIR}/bin/activate ; set -u ;\
99 pip install python/local_imports/pyvoltha/dist/*.tar.gz
100endif
101ifdef LOCAL_PROTOS
102 source ./${VENVDIR}/bin/activate ; set -u ;\
103 pip install python/local_imports/voltha-protos/dist/*.tar.gz
104endif
105
106
107## Docker targets
108
109build: docker-build
110
111docker-build: openolt_go openolt_python
112
113openolt_go: local-protos local-volthago
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400114 docker build $(DOCKER_BUILD_ARGS) \
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400115 -t ${GOADAPTER_IMAGENAME} \
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400116 --build-arg org_label_schema_version="${VERSION}" \
117 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
118 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
119 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
120 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
121 -f docker/Dockerfile.openolt .
122
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400123openolt_python: local-protos local-pyvoltha
124 docker build $(DOCKER_BUILD_ARGS) \
125 -t ${PYTHONADAPTER_IMAGENAME} \
126 --build-arg LOCAL_PYVOLTHA=$(LOCAL_PYVOLTHA) \
127 --build-arg LOCAL_PROTOS=$(LOCAL_PROTOS) \
128 --build-arg org_label_schema_version="${VERSION}" \
129 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
130 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
131 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
132 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
133 -f python/docker/Dockerfile.openolt_adapter python
134
135 # Current default image gets the base DOCKER_TAG
136 docker tag ${PYTHONADAPTER_IMAGENAME} ${ADAPTER_IMAGENAME}
137
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400138docker-push:
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400139 docker push ${GOADAPTER_IMAGENAME}
140 docker push ${PYTHONADAPTER_IMAGENAME}
Matt Jeanneret8b823f62019-05-11 11:01:28 -0400141 docker push ${ADAPTER_IMAGENAME}
142
143
144## lint and unit tests
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530145
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400146lint-style:
147ifeq (,$(shell which gofmt))
148 go get -u github.com/golang/go/src/cmd/gofmt
149endif
150 @echo "Running style check..."
151 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
152 if [ ! -z "$$gofmt_out" ]; then \
153 echo "$$gofmt_out" ;\
154 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
155 exit 1 ;\
156 fi
157 @echo "Style check OK"
158
159lint-sanity:
160 @echo "Running sanity check..."
161 @go vet ./...
162 @echo "Sanity check OK"
163
164lint-dep:
165 @echo "Running dependency check..."
166 @dep check
167 @echo "Dependency check OK"
168
169lint: lint-style lint-sanity lint-dep
170
171GO_JUNIT_REPORT:=$(shell which go-junit-report)
172GOCOVER_COBERTURA:=$(shell which gocover-cobertura)
173test:
174ifeq (,$(GO_JUNIT_REPORT))
175 go get -u github.com/jstemmer/go-junit-report
176 @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
177endif
178
179ifeq (,$(GOCOVER_COBERTURA))
180 go get -u github.com/t-yuki/gocover-cobertura
181 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
182endif
183
184 @mkdir -p ./tests/results
185
186 @go test -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
187 RETURN=$$? ;\
188 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
189 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
190 exit $$RETURN
191
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192GOLANGCI_LINT_TOOL:=$(shell which golangci-lint)
193sca:
194ifeq (,$(GOLANGCI_LINT_TOOL))
195 @echo "Please install golangci-lint tool to run sca"
196 exit 1
197endif
198 @mkdir -p ./sca-report
199 GO111MODULE=on golangci-lint run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml ;\
200 RETURN=$$? ;\
201 exit $$RETURN
202
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400203clean:
204 rm -rf python/local_imports
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205 rm -rf sca-report
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400206 find python -name '*.pyc' | xargs rm -f
207
208distclean: clean
209 rm -rf ${VENVDIR}
210
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530211# end file