blob: 3fe545adefcd77587ff366ff047ecc799cac8f27 [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
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040036
37## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040038DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
39DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
40DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
41DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040042
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040043DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070044 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040045 --build-arg org_label_schema_version="${VERSION}" \
46 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
47 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
48 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
49 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
50 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
51
52DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
53 --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
54 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040055
Scott Baker8461e152019-10-01 14:44:30 -070056.PHONY: rw_core ro_core local-protos local-pyvoltha
khenaidoocfee5f42018-07-19 22:47:38 -040057
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:"
62 @echo
David K. Bainbridge1678e192019-05-17 11:48:29 -070063 @echo "build : Build the docker images."
64 @echo " - If this is the first time you are building, choose 'make build' option."
65 @echo "rw_core : Build the rw_core docker image"
66 @echo "ro_core : Build the ro_core docker image"
David K. Bainbridge1678e192019-05-17 11:48:29 -070067 @echo "ofagent : Build the openflow agent docker image"
68 @echo "cli : Build the voltha CLI docker image"
David K. Bainbridge1678e192019-05-17 11:48:29 -070069 @echo "venv : Build local Python virtualenv"
70 @echo "clean : Remove files created by the build and tests"
71 @echo "distclean : Remove venv directory"
72 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge5f3619c2019-07-10 22:51:09 +000073 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
David K. Bainbridge1678e192019-05-17 11:48:29 -070074 @echo "lint-style : Verify code is properly gofmt-ed"
75 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
76 @echo "lint-dep : Verify the integrity of the 'dep' files"
77 @echo "lint : Shorthand for lint-style & lint-sanity"
78 @echo "test : Generate reports for all go tests"
khenaidoocfee5f42018-07-19 22:47:38 -040079 @echo
80
David K. Bainbridge1678e192019-05-17 11:48:29 -070081## Local Development Helpers
82local-protos:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040083 @mkdir -p python/local_imports
David K. Bainbridge1678e192019-05-17 11:48:29 -070084ifdef LOCAL_PROTOS
85 mkdir -p vendor/github.com/opencord/voltha-protos/go
86 cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go
William Kurkiand4eccbc2019-07-30 10:17:21 -040087 rm -rf python/local_imports/voltha-protos
David K. Bainbridge1678e192019-05-17 11:48:29 -070088 mkdir -p python/local_imports/voltha-protos/dist
89 cp ../voltha-protos/dist/*.tar.gz python/local_imports/voltha-protos/dist/
90endif
91
92local-pyvoltha:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040093 @mkdir -p python/local_imports
David K. Bainbridge1678e192019-05-17 11:48:29 -070094ifdef LOCAL_PYVOLTHA
William Kurkiand4eccbc2019-07-30 10:17:21 -040095 rm -rf python/local_imports/pyvoltha
David K. Bainbridge1678e192019-05-17 11:48:29 -070096 mkdir -p python/local_imports/pyvoltha/dist
97 cp ../pyvoltha/dist/*.tar.gz python/local_imports/pyvoltha/dist/
98endif
99
100## Python venv dev environment
101
102VENVDIR := python/venv-volthago
103
104venv: distclean local-protos local-pyvoltha
105 virtualenv ${VENVDIR};\
106 source ./${VENVDIR}/bin/activate ; set -u ;\
107 rm -f ${VENVDIR}/local/bin ${VENVDIR}/local/lib ${VENVDIR}/local/include ;\
108 pip install -r python/requirements.txt
109ifdef LOCAL_PYVOLTHA
110 source ./${VENVDIR}/bin/activate ; set -u ;\
111 pip install python/local_imports/pyvoltha/dist/*.tar.gz
112endif
113ifdef LOCAL_PROTOS
114 source ./${VENVDIR}/bin/activate ; set -u ;\
115 pip install python/local_imports/voltha-protos/dist/*.tar.gz
116endif
khenaidoocfee5f42018-07-19 22:47:38 -0400117
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400118## Docker targets
khenaidoocfee5f42018-07-19 22:47:38 -0400119
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400120build: docker-build
khenaidoocfee5f42018-07-19 22:47:38 -0400121
Scott Baker676f0dd2019-10-14 17:07:48 -0700122docker-build: rw_core ro_core ofagent cli
sslobodra3ea7d42019-01-16 15:03:16 -0500123
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400124rw_core: local-protos
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400125 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 -0400126
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400127ro_core: local-protos
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400128 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 -0500129
David K. Bainbridge1678e192019-05-17 11:48:29 -0700130ofagent: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400131 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 -0700132
133cli: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400134 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 -0700135
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400136docker-push:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400137 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
138 docker push ${ROCORE_IMAGENAME}:${DOCKER_TAG}
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400139 docker push ${OFAGENT_IMAGENAME}:${DOCKER_TAG}
140 docker push ${CLI_IMAGENAME}:${DOCKER_TAG}
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400141
142## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500143
David Bainbridge5f3619c2019-07-10 22:51:09 +0000144PATH:=$(GOPATH)/bin:$(PATH)
145HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
146lint-dockerfile:
147ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
148 mkdir -p $(GOPATH)/bin
149 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
150 chmod 755 $(GOPATH)/bin/hadolint
151endif
152 @echo "Running Dockerfile lint check ..."
153 @hadolint $$(find . -name "Dockerfile.*")
154 @echo "Dockerfile lint check OK"
155
Kent Hagerman074d0e02019-04-24 17:58:16 -0400156lint-style:
Zack Williams7cf78002019-04-30 21:45:35 -0700157ifeq (,$(shell which gofmt))
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400158 go get -u github.com/golang/go/src/cmd/gofmt
Zack Williams7cf78002019-04-30 21:45:35 -0700159endif
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400160 @echo "Running style check..."
161 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700162 if [ ! -z "$$gofmt_out" ]; then \
163 echo "$$gofmt_out" ;\
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400164 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700165 exit 1 ;\
Kent Hagerman074d0e02019-04-24 17:58:16 -0400166 fi
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400167 @echo "Style check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400168
169lint-sanity:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400170 @echo "Running sanity check..."
171 @go vet ./...
172 @echo "Sanity check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400173
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400174lint-dep:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400175 @echo "Running dependency check..."
176 @dep check
177 @echo "Dependency check OK"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400178
David Bainbridge5f3619c2019-07-10 22:51:09 +0000179lint: lint-style lint-sanity lint-dep lint-dockerfile
Kent Hagerman074d0e02019-04-24 17:58:16 -0400180
Zack Williams7cf78002019-04-30 21:45:35 -0700181GO_JUNIT_REPORT:=$(shell which go-junit-report)
182GOCOVER_COBERTURA:=$(shell which gocover-cobertura)
David K. Bainbridge1678e192019-05-17 11:48:29 -0700183
Kent Hagerman074d0e02019-04-24 17:58:16 -0400184test:
Zack Williams7cf78002019-04-30 21:45:35 -0700185ifeq (,$(GO_JUNIT_REPORT))
186 go get -u github.com/jstemmer/go-junit-report
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400187 @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
Zack Williams7cf78002019-04-30 21:45:35 -0700188endif
Zack Williams7cf78002019-04-30 21:45:35 -0700189ifeq (,$(GOCOVER_COBERTURA))
190 go get -u github.com/t-yuki/gocover-cobertura
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400191 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
Zack Williams7cf78002019-04-30 21:45:35 -0700192endif
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400193 @mkdir -p ./tests/results
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400194 @go test -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 -0700195 RETURN=$$? ;\
196 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
197 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
198 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400199
David K. Bainbridge1678e192019-05-17 11:48:29 -0700200clean:
201 rm -rf python/local_imports
202 find python -name '*.pyc' | xargs rm -f
203
204distclean: clean
205 rm -rf ${VENVDIR}
206
khenaidoocfee5f42018-07-19 22:47:38 -0400207# end file