blob: 206becdaacda1309243ec61e3422ffdc3b0a003e [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
girishk5259f8e2019-10-10 18:44:44 +000043# Default is GO111MODULE=auto, which will refuse to use go mod if running
44# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
45# force module usage. This affects commands executed from this Makefile, but
46# not the environment inside the Docker build (which does not build from
47# inside a GOPATH).
48export GO111MODULE=on
49
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040050DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070051 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040052 --build-arg org_label_schema_version="${VERSION}" \
53 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
54 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
55 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
56 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
57 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
58
59DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
60 --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
61 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040062
Scott Baker8461e152019-10-01 14:44:30 -070063.PHONY: rw_core ro_core local-protos local-pyvoltha
khenaidoocfee5f42018-07-19 22:47:38 -040064
65# This should to be the first and default target in this Makefile
66help:
67 @echo "Usage: make [<target>]"
68 @echo "where available targets are:"
69 @echo
David K. Bainbridge1678e192019-05-17 11:48:29 -070070 @echo "build : Build the docker images."
71 @echo " - If this is the first time you are building, choose 'make build' option."
72 @echo "rw_core : Build the rw_core docker image"
73 @echo "ro_core : Build the ro_core docker image"
David K. Bainbridge1678e192019-05-17 11:48:29 -070074 @echo "ofagent : Build the openflow agent docker image"
75 @echo "cli : Build the voltha CLI docker image"
David K. Bainbridge1678e192019-05-17 11:48:29 -070076 @echo "venv : Build local Python virtualenv"
77 @echo "clean : Remove files created by the build and tests"
78 @echo "distclean : Remove venv directory"
79 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge5f3619c2019-07-10 22:51:09 +000080 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
David K. Bainbridge1678e192019-05-17 11:48:29 -070081 @echo "lint-style : Verify code is properly gofmt-ed"
82 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
girishk5259f8e2019-10-10 18:44:44 +000083 @echo "lint-mod : Verify the integrity of the 'mod' files"
David K. Bainbridge1678e192019-05-17 11:48:29 -070084 @echo "lint : Shorthand for lint-style & lint-sanity"
85 @echo "test : Generate reports for all go tests"
khenaidoocfee5f42018-07-19 22:47:38 -040086 @echo
87
David K. Bainbridge1678e192019-05-17 11:48:29 -070088## Local Development Helpers
89local-protos:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040090 @mkdir -p python/local_imports
David K. Bainbridge1678e192019-05-17 11:48:29 -070091ifdef LOCAL_PROTOS
William Kurkian905834f2019-11-13 15:40:46 -050092 mkdir -p vendor/github.com/opencord/voltha-protos/v2/go
93 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v2/go
William Kurkiand4eccbc2019-07-30 10:17:21 -040094 rm -rf python/local_imports/voltha-protos
David K. Bainbridge1678e192019-05-17 11:48:29 -070095 mkdir -p python/local_imports/voltha-protos/dist
William Kurkian905834f2019-11-13 15:40:46 -050096 cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/
David K. Bainbridge1678e192019-05-17 11:48:29 -070097endif
98
Scott Bakercb7c88a2019-10-16 18:32:48 -070099## Local Development Helpers
100local-lib-go:
101ifdef LOCAL_LIB_GO
Scott Baker17f1d9d2019-10-25 12:51:25 -0700102 mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg
103 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -0700104endif
105
David K. Bainbridge1678e192019-05-17 11:48:29 -0700106local-pyvoltha:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400107 @mkdir -p python/local_imports
David K. Bainbridge1678e192019-05-17 11:48:29 -0700108ifdef LOCAL_PYVOLTHA
William Kurkiand4eccbc2019-07-30 10:17:21 -0400109 rm -rf python/local_imports/pyvoltha
David K. Bainbridge1678e192019-05-17 11:48:29 -0700110 mkdir -p python/local_imports/pyvoltha/dist
Scott Baker17f1d9d2019-10-25 12:51:25 -0700111 cp ${LOCAL_PYVOLTHA}/dist/*.tar.gz python/local_imports/pyvoltha/dist/
David K. Bainbridge1678e192019-05-17 11:48:29 -0700112endif
113
114## Python venv dev environment
115
116VENVDIR := python/venv-volthago
117
118venv: distclean local-protos local-pyvoltha
119 virtualenv ${VENVDIR};\
120 source ./${VENVDIR}/bin/activate ; set -u ;\
121 rm -f ${VENVDIR}/local/bin ${VENVDIR}/local/lib ${VENVDIR}/local/include ;\
122 pip install -r python/requirements.txt
123ifdef LOCAL_PYVOLTHA
124 source ./${VENVDIR}/bin/activate ; set -u ;\
125 pip install python/local_imports/pyvoltha/dist/*.tar.gz
126endif
127ifdef LOCAL_PROTOS
128 source ./${VENVDIR}/bin/activate ; set -u ;\
129 pip install python/local_imports/voltha-protos/dist/*.tar.gz
130endif
khenaidoocfee5f42018-07-19 22:47:38 -0400131
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400132## Docker targets
khenaidoocfee5f42018-07-19 22:47:38 -0400133
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400134build: docker-build
khenaidoocfee5f42018-07-19 22:47:38 -0400135
Scott Baker676f0dd2019-10-14 17:07:48 -0700136docker-build: rw_core ro_core ofagent cli
sslobodra3ea7d42019-01-16 15:03:16 -0500137
Scott Bakercb7c88a2019-10-16 18:32:48 -0700138rw_core: local-protos local-lib-go
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400139 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 -0400140
Scott Bakercb7c88a2019-10-16 18:32:48 -0700141ro_core: local-protos local-lib-go
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400142 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 -0500143
David K. Bainbridge1678e192019-05-17 11:48:29 -0700144ofagent: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400145 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 -0700146
147cli: local-protos local-pyvoltha
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400148 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 -0700149
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400150docker-push:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400151 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
152 docker push ${ROCORE_IMAGENAME}:${DOCKER_TAG}
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400153 docker push ${OFAGENT_IMAGENAME}:${DOCKER_TAG}
154 docker push ${CLI_IMAGENAME}:${DOCKER_TAG}
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400155
156## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500157
David Bainbridge5f3619c2019-07-10 22:51:09 +0000158PATH:=$(GOPATH)/bin:$(PATH)
159HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
160lint-dockerfile:
161ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
162 mkdir -p $(GOPATH)/bin
163 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
164 chmod 755 $(GOPATH)/bin/hadolint
165endif
166 @echo "Running Dockerfile lint check ..."
167 @hadolint $$(find . -name "Dockerfile.*")
168 @echo "Dockerfile lint check OK"
169
Kent Hagerman074d0e02019-04-24 17:58:16 -0400170lint-style:
Zack Williams7cf78002019-04-30 21:45:35 -0700171ifeq (,$(shell which gofmt))
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400172 go get -u github.com/golang/go/src/cmd/gofmt
Zack Williams7cf78002019-04-30 21:45:35 -0700173endif
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400174 @echo "Running style check..."
175 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700176 if [ ! -z "$$gofmt_out" ]; then \
177 echo "$$gofmt_out" ;\
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400178 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700179 exit 1 ;\
Kent Hagerman074d0e02019-04-24 17:58:16 -0400180 fi
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400181 @echo "Style check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400182
183lint-sanity:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400184 @echo "Running sanity check..."
girishk5259f8e2019-10-10 18:44:44 +0000185 @go vet -mod=vendor ./...
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400186 @echo "Sanity check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400187
girishk5259f8e2019-10-10 18:44:44 +0000188lint-mod:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400189 @echo "Running dependency check..."
girishk5259f8e2019-10-10 18:44:44 +0000190 @go mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800191 @echo "Dependency check OK. Running vendor check..."
192 @git status > /dev/null
193 @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)
194 @[[ `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)
195 go mod tidy
196 go mod vendor
197 @git status > /dev/null
198 @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)
199 @[[ `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)
200 @echo "Vendor check OK."
201
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400202
girishk5259f8e2019-10-10 18:44:44 +0000203lint: lint-style lint-sanity lint-mod lint-dockerfile
Kent Hagerman074d0e02019-04-24 17:58:16 -0400204
Scott Baker18ea5a42019-10-15 16:08:42 -0700205# Rules to automatically install golangci-lint
206GOLANGCI_LINT_TOOL?=$(shell which golangci-lint)
207ifeq (,$(GOLANGCI_LINT_TOOL))
208GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint
209golangci_lint_tool_install:
210 # Same version as installed by Jenkins ci-management
211 # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint
212 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0
213else
214golangci_lint_tool_install:
215endif
David K. Bainbridge1678e192019-05-17 11:48:29 -0700216
Scott Baker18ea5a42019-10-15 16:08:42 -0700217# Rules to automatically install go-junit-report
218GO_JUNIT_REPORT?=$(shell which go-junit-report)
Zack Williams7cf78002019-04-30 21:45:35 -0700219ifeq (,$(GO_JUNIT_REPORT))
Scott Baker18ea5a42019-10-15 16:08:42 -0700220GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
221go_junit_install:
Zack Williams7cf78002019-04-30 21:45:35 -0700222 go get -u github.com/jstemmer/go-junit-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700223else
224go_junit_install:
Zack Williams7cf78002019-04-30 21:45:35 -0700225endif
Scott Baker18ea5a42019-10-15 16:08:42 -0700226
227# Rules to automatically install gocover-covertura
228GOCOVER_COBERTURA?=$(shell which gocover-cobertura)
Zack Williams7cf78002019-04-30 21:45:35 -0700229ifeq (,$(GOCOVER_COBERTURA))
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400230 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
Scott Baker18ea5a42019-10-15 16:08:42 -0700231gocover_cobertura_install:
232 go get -u github.com/t-yuki/gocover-cobertura
233else
234gocover_cobertura_install:
Zack Williams7cf78002019-04-30 21:45:35 -0700235endif
Scott Baker18ea5a42019-10-15 16:08:42 -0700236
237sca: golangci_lint_tool_install
238 rm -rf ./sca-report
239 @mkdir -p ./sca-report
npujar1d86a522019-11-14 17:11:16 +0530240 $(GOLANGCI_LINT_TOOL) run -E golint -D structcheck --out-format junit-xml ./cli/... ./rw_core/... 2>&1 | tee ./sca-report/sca-report.xml
Scott Baker18ea5a42019-10-15 16:08:42 -0700241
Scott Bakercb7c88a2019-10-16 18:32:48 -0700242test: go_junit_install gocover_cobertura_install local-lib-go
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400243 @mkdir -p ./tests/results
girishk5259f8e2019-10-10 18:44:44 +0000244 @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 -0700245 RETURN=$$? ;\
246 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
247 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
248 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400249
David K. Bainbridge1678e192019-05-17 11:48:29 -0700250clean:
251 rm -rf python/local_imports
252 find python -name '*.pyc' | xargs rm -f
253
254distclean: clean
David Bainbridge81175e42019-10-16 19:06:37 +0000255 rm -rf ${VENVDIR} ./sca_report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700256
Scott Bakercb7c88a2019-10-16 18:32:48 -0700257mod-update:
258 go mod tidy
259 go mod vendor
260
khenaidoocfee5f42018-07-19 22:47:38 -0400261# end file