blob: 27ad8b70a299dfad133bfd8fa94bf5dcfa5937fd [file] [log] [blame]
khenaidoocfee5f42018-07-19 22:47:38 -04001#
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 Jeanneret2e3051a2019-05-11 15:01:46 -040017# set default shell
18SHELL = bash -e -o pipefail
Kent Hagerman074d0e02019-04-24 17:58:16 -040019
Zack Williams27f59a42019-05-10 09:12:07 -070020# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040021VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070022
Kent Hagermanf4a3aab2019-05-22 14:42:34 -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 Jeanneret2e3051a2019-05-11 15:01:46 -040027## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070028DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040029DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
31DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
32RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
33ROCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-ro-core
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040034OFAGENT_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-ofagent
35CLI_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-cli
36PONSIMOLT_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-ponsim-olt
37PONSIMONU_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-ponsim-onu
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040038
39## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040040DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
41DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
42DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
43DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040044
girishk38b31ff2019-10-10 18:44:44 +000045# Default is GO111MODULE=auto, which will refuse to use go mod if running
46# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
47# force module usage. This affects commands executed from this Makefile, but
48# not the environment inside the Docker build (which does not build from
49# inside a GOPATH).
50export GO111MODULE=on
51
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040052DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070053 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040054 --build-arg org_label_schema_version="${VERSION}" \
55 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
56 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
57 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
58 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
59 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
60
61DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
62 --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
63 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040064
Scott Bakerbeb3cfa2019-10-01 14:44:30 -070065.PHONY: rw_core ro_core local-protos local-pyvoltha
khenaidoocfee5f42018-07-19 22:47:38 -040066
67# This should to be the first and default target in this Makefile
68help:
69 @echo "Usage: make [<target>]"
70 @echo "where available targets are:"
71 @echo
David K. Bainbridge1678e192019-05-17 11:48:29 -070072 @echo "build : Build the docker images."
73 @echo " - If this is the first time you are building, choose 'make build' option."
74 @echo "rw_core : Build the rw_core docker image"
75 @echo "ro_core : Build the ro_core docker image"
David K. Bainbridge1678e192019-05-17 11:48:29 -070076 @echo "ofagent : Build the openflow agent docker image"
77 @echo "cli : Build the voltha CLI docker image"
78 @echo "adapter_ponsim_olt : Build the ponsim olt adapter docker image"
79 @echo "adapter_ponsim_onu : Build the ponsim onu adapter docker image"
80 @echo "venv : Build local Python virtualenv"
81 @echo "clean : Remove files created by the build and tests"
82 @echo "distclean : Remove venv directory"
83 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge5f3619c2019-07-10 22:51:09 +000084 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
David K. Bainbridge1678e192019-05-17 11:48:29 -070085 @echo "lint-style : Verify code is properly gofmt-ed"
86 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
girishk38b31ff2019-10-10 18:44:44 +000087 @echo "lint-mod : Verify the integrity of the 'mod' files"
David K. Bainbridge1678e192019-05-17 11:48:29 -070088 @echo "lint : Shorthand for lint-style & lint-sanity"
89 @echo "test : Generate reports for all go tests"
khenaidoocfee5f42018-07-19 22:47:38 -040090 @echo
91
David K. Bainbridge1678e192019-05-17 11:48:29 -070092## Local Development Helpers
93local-protos:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040094 @mkdir -p python/local_imports
David K. Bainbridge1678e192019-05-17 11:48:29 -070095ifdef LOCAL_PROTOS
96 mkdir -p vendor/github.com/opencord/voltha-protos/go
97 cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go
William Kurkiand4eccbc2019-07-30 10:17:21 -040098 rm -rf python/local_imports/voltha-protos
David K. Bainbridge1678e192019-05-17 11:48:29 -070099 mkdir -p python/local_imports/voltha-protos/dist
100 cp ../voltha-protos/dist/*.tar.gz python/local_imports/voltha-protos/dist/
101endif
102
103local-pyvoltha:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400104 @mkdir -p python/local_imports
David K. Bainbridge1678e192019-05-17 11:48:29 -0700105ifdef LOCAL_PYVOLTHA
William Kurkiand4eccbc2019-07-30 10:17:21 -0400106 rm -rf python/local_imports/pyvoltha
David K. Bainbridge1678e192019-05-17 11:48:29 -0700107 mkdir -p python/local_imports/pyvoltha/dist
108 cp ../pyvoltha/dist/*.tar.gz python/local_imports/pyvoltha/dist/
109endif
110
111## Python venv dev environment
112
113VENVDIR := python/venv-volthago
114
115venv: distclean local-protos local-pyvoltha
116 virtualenv ${VENVDIR};\
117 source ./${VENVDIR}/bin/activate ; set -u ;\
118 rm -f ${VENVDIR}/local/bin ${VENVDIR}/local/lib ${VENVDIR}/local/include ;\
119 pip install -r python/requirements.txt
120ifdef LOCAL_PYVOLTHA
121 source ./${VENVDIR}/bin/activate ; set -u ;\
122 pip install python/local_imports/pyvoltha/dist/*.tar.gz
123endif
124ifdef LOCAL_PROTOS
125 source ./${VENVDIR}/bin/activate ; set -u ;\
126 pip install python/local_imports/voltha-protos/dist/*.tar.gz
127endif
khenaidoocfee5f42018-07-19 22:47:38 -0400128
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400129## Docker targets
khenaidoocfee5f42018-07-19 22:47:38 -0400130
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400131build: docker-build
khenaidoocfee5f42018-07-19 22:47:38 -0400132
Scott Bakerbeb3cfa2019-10-01 14:44:30 -0700133docker-build: rw_core ro_core ofagent cli adapter_ponsim_olt adapter_ponsim_onu
sslobodra3ea7d42019-01-16 15:03:16 -0500134
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400135rw_core: local-protos
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400136 docker build $(DOCKER_BUILD_ARGS) -t ${RWCORE_IMAGENAME}:${DOCKER_TAG} -t ${RWCORE_IMAGENAME}:latest -f docker/Dockerfile.rw_core .
khenaidoocfee5f42018-07-19 22:47:38 -0400137
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400138ro_core: local-protos
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400139 docker build $(DOCKER_BUILD_ARGS) -t ${ROCORE_IMAGENAME}:${DOCKER_TAG} -t ${ROCORE_IMAGENAME}:latest -f docker/Dockerfile.ro_core .
Stephane Barbariea75791c2019-01-24 10:58:06 -0500140
David K. Bainbridge1678e192019-05-17 11:48:29 -0700141ofagent: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400142 docker build $(DOCKER_BUILD_ARGS_LOCAL) -t ${OFAGENT_IMAGENAME}:${DOCKER_TAG} -t ${OFAGENT_IMAGENAME}:latest -f python/docker/Dockerfile.ofagent python
David K. Bainbridge1678e192019-05-17 11:48:29 -0700143
144cli: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400145 docker build $(DOCKER_BUILD_ARGS_LOCAL) -t ${CLI_IMAGENAME}:${DOCKER_TAG} -t ${CLI_IMAGENAME}:latest -f python/docker/Dockerfile.cli python
David K. Bainbridge1678e192019-05-17 11:48:29 -0700146
147adapter_ponsim_olt: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400148 docker build $(DOCKER_BUILD_ARGS_LOCAL) -t ${PONSIMOLT_IMAGENAME}:${DOCKER_TAG} -t ${PONSIMOLT_IMAGENAME}:latest -f python/docker/Dockerfile.adapter_ponsim_olt python
David K. Bainbridge1678e192019-05-17 11:48:29 -0700149
150adapter_ponsim_onu: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400151 docker build $(DOCKER_BUILD_ARGS_LOCAL) -t ${PONSIMONU_IMAGENAME}:${DOCKER_TAG} -t ${PONSIMONU_IMAGENAME}:latest -f python/docker/Dockerfile.adapter_ponsim_onu python
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400152
153docker-push:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400154 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
155 docker push ${ROCORE_IMAGENAME}:${DOCKER_TAG}
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400156 docker push ${OFAGENT_IMAGENAME}:${DOCKER_TAG}
157 docker push ${CLI_IMAGENAME}:${DOCKER_TAG}
158 docker push ${PONSIMOLT_IMAGENAME}:${DOCKER_TAG}
159 docker push ${PONSIMONU_IMAGENAME}:${DOCKER_TAG}
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400160
161## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500162
David Bainbridge5f3619c2019-07-10 22:51:09 +0000163PATH:=$(GOPATH)/bin:$(PATH)
164HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
165lint-dockerfile:
166ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
167 mkdir -p $(GOPATH)/bin
168 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
169 chmod 755 $(GOPATH)/bin/hadolint
170endif
171 @echo "Running Dockerfile lint check ..."
172 @hadolint $$(find . -name "Dockerfile.*")
173 @echo "Dockerfile lint check OK"
174
Kent Hagerman074d0e02019-04-24 17:58:16 -0400175lint-style:
Zack Williams7cf78002019-04-30 21:45:35 -0700176ifeq (,$(shell which gofmt))
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400177 go get -u github.com/golang/go/src/cmd/gofmt
Zack Williams7cf78002019-04-30 21:45:35 -0700178endif
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400179 @echo "Running style check..."
180 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700181 if [ ! -z "$$gofmt_out" ]; then \
182 echo "$$gofmt_out" ;\
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400183 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700184 exit 1 ;\
Kent Hagerman074d0e02019-04-24 17:58:16 -0400185 fi
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400186 @echo "Style check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400187
188lint-sanity:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400189 @echo "Running sanity check..."
girishk38b31ff2019-10-10 18:44:44 +0000190 @go vet -mod=vendor ./...
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400191 @echo "Sanity check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400192
girishk38b31ff2019-10-10 18:44:44 +0000193lint-mod:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400194 @echo "Running dependency check..."
girishk38b31ff2019-10-10 18:44:44 +0000195 @go mod verify
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400196 @echo "Dependency check OK"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400197
girishk38b31ff2019-10-10 18:44:44 +0000198lint: lint-style lint-sanity lint-mod lint-dockerfile
Kent Hagerman074d0e02019-04-24 17:58:16 -0400199
Zack Williams7cf78002019-04-30 21:45:35 -0700200GO_JUNIT_REPORT:=$(shell which go-junit-report)
201GOCOVER_COBERTURA:=$(shell which gocover-cobertura)
David K. Bainbridge1678e192019-05-17 11:48:29 -0700202
Kent Hagerman074d0e02019-04-24 17:58:16 -0400203test:
Zack Williams7cf78002019-04-30 21:45:35 -0700204ifeq (,$(GO_JUNIT_REPORT))
205 go get -u github.com/jstemmer/go-junit-report
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400206 @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
Zack Williams7cf78002019-04-30 21:45:35 -0700207endif
Zack Williams7cf78002019-04-30 21:45:35 -0700208ifeq (,$(GOCOVER_COBERTURA))
209 go get -u github.com/t-yuki/gocover-cobertura
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400210 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
Zack Williams7cf78002019-04-30 21:45:35 -0700211endif
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400212 @mkdir -p ./tests/results
girishk38b31ff2019-10-10 18:44:44 +0000213 @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700214 RETURN=$$? ;\
215 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
216 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
217 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400218
David K. Bainbridge1678e192019-05-17 11:48:29 -0700219clean:
220 rm -rf python/local_imports
221 find python -name '*.pyc' | xargs rm -f
222
223distclean: clean
224 rm -rf ${VENVDIR}
225
khenaidoocfee5f42018-07-19 22:47:38 -0400226# end file