blob: c31e0e562d08c8079f7a3c9156e39e5aac823a5b [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
Andrea Campanella7a6cce22019-12-17 14:10:27 -080034TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040035
36## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040037DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
38DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
39DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
40DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040041
girishk5259f8e2019-10-10 18:44:44 +000042# Default is GO111MODULE=auto, which will refuse to use go mod if running
43# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
44# force module usage. This affects commands executed from this Makefile, but
45# not the environment inside the Docker build (which does not build from
46# inside a GOPATH).
47export GO111MODULE=on
48
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040049DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070050 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040051 --build-arg org_label_schema_version="${VERSION}" \
52 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
53 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
54 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
55 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
56 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
57
58DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040059 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040060
Zack Williamsb9b1afb2019-12-09 15:28:52 -070061.PHONY: rw_core ro_core local-protos
khenaidoocfee5f42018-07-19 22:47:38 -040062
63# This should to be the first and default target in this Makefile
64help:
65 @echo "Usage: make [<target>]"
66 @echo "where available targets are:"
67 @echo
David K. Bainbridge1678e192019-05-17 11:48:29 -070068 @echo "build : Build the docker images."
69 @echo " - If this is the first time you are building, choose 'make build' option."
70 @echo "rw_core : Build the rw_core docker image"
71 @echo "ro_core : Build the ro_core docker image"
David K. Bainbridge1678e192019-05-17 11:48:29 -070072 @echo "clean : Remove files created by the build and tests"
Zack Williamsb9b1afb2019-12-09 15:28:52 -070073 @echo "distclean : Remove sca directory and clean"
David K. Bainbridge1678e192019-05-17 11:48:29 -070074 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge5f3619c2019-07-10 22:51:09 +000075 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
David K. Bainbridge1678e192019-05-17 11:48:29 -070076 @echo "lint-style : Verify code is properly gofmt-ed"
77 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
girishk5259f8e2019-10-10 18:44:44 +000078 @echo "lint-mod : Verify the integrity of the 'mod' files"
David K. Bainbridge1678e192019-05-17 11:48:29 -070079 @echo "lint : Shorthand for lint-style & lint-sanity"
npujar03b018e2019-11-13 15:29:36 +053080 @echo "sca : Runs various SCA through golangci-lint tool"
David K. Bainbridge1678e192019-05-17 11:48:29 -070081 @echo "test : Generate reports for all go tests"
khenaidoocfee5f42018-07-19 22:47:38 -040082 @echo
83
David K. Bainbridge1678e192019-05-17 11:48:29 -070084## Local Development Helpers
85local-protos:
David K. Bainbridge1678e192019-05-17 11:48:29 -070086ifdef LOCAL_PROTOS
William Kurkian905834f2019-11-13 15:40:46 -050087 mkdir -p vendor/github.com/opencord/voltha-protos/v2/go
88 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v2/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070089endif
90
Scott Bakercb7c88a2019-10-16 18:32:48 -070091## Local Development Helpers
92local-lib-go:
93ifdef LOCAL_LIB_GO
Scott Baker17f1d9d2019-10-25 12:51:25 -070094 mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg
95 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070096endif
97
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040098## Docker targets
khenaidoocfee5f42018-07-19 22:47:38 -040099
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400100build: docker-build
khenaidoocfee5f42018-07-19 22:47:38 -0400101
Zack Williamsb9b1afb2019-12-09 15:28:52 -0700102docker-build: rw_core ro_core
sslobodra3ea7d42019-01-16 15:03:16 -0500103
Scott Bakercb7c88a2019-10-16 18:32:48 -0700104rw_core: local-protos local-lib-go
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400105 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 -0400106
Scott Bakercb7c88a2019-10-16 18:32:48 -0700107ro_core: local-protos local-lib-go
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400108 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 -0500109
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400110docker-push:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400111 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
112 docker push ${ROCORE_IMAGENAME}:${DOCKER_TAG}
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400113
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800114docker-kind-load:
115 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
116 kind load docker-image ${RWCORE_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev)
117 kind load docker-image ${ROCORE_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev)
118
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400119## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500120
David Bainbridge5f3619c2019-07-10 22:51:09 +0000121PATH:=$(GOPATH)/bin:$(PATH)
122HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
123lint-dockerfile:
124ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
125 mkdir -p $(GOPATH)/bin
126 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
127 chmod 755 $(GOPATH)/bin/hadolint
128endif
129 @echo "Running Dockerfile lint check ..."
130 @hadolint $$(find . -name "Dockerfile.*")
131 @echo "Dockerfile lint check OK"
132
Kent Hagerman074d0e02019-04-24 17:58:16 -0400133lint-style:
Zack Williams7cf78002019-04-30 21:45:35 -0700134ifeq (,$(shell which gofmt))
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400135 go get -u github.com/golang/go/src/cmd/gofmt
Zack Williams7cf78002019-04-30 21:45:35 -0700136endif
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400137 @echo "Running style check..."
138 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700139 if [ ! -z "$$gofmt_out" ]; then \
140 echo "$$gofmt_out" ;\
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400141 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700142 exit 1 ;\
Kent Hagerman074d0e02019-04-24 17:58:16 -0400143 fi
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400144 @echo "Style check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400145
146lint-sanity:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400147 @echo "Running sanity check..."
girishk5259f8e2019-10-10 18:44:44 +0000148 @go vet -mod=vendor ./...
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400149 @echo "Sanity check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400150
girishk5259f8e2019-10-10 18:44:44 +0000151lint-mod:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400152 @echo "Running dependency check..."
girishk5259f8e2019-10-10 18:44:44 +0000153 @go mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800154 @echo "Dependency check OK. Running vendor check..."
155 @git status > /dev/null
156 @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)
157 @[[ `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)
158 go mod tidy
159 go mod vendor
160 @git status > /dev/null
161 @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)
162 @[[ `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)
163 @echo "Vendor check OK."
164
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400165
girishk5259f8e2019-10-10 18:44:44 +0000166lint: lint-style lint-sanity lint-mod lint-dockerfile
Kent Hagerman074d0e02019-04-24 17:58:16 -0400167
Scott Baker18ea5a42019-10-15 16:08:42 -0700168# Rules to automatically install golangci-lint
169GOLANGCI_LINT_TOOL?=$(shell which golangci-lint)
170ifeq (,$(GOLANGCI_LINT_TOOL))
171GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint
172golangci_lint_tool_install:
173 # Same version as installed by Jenkins ci-management
174 # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint
175 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0
176else
177golangci_lint_tool_install:
178endif
David K. Bainbridge1678e192019-05-17 11:48:29 -0700179
Scott Baker18ea5a42019-10-15 16:08:42 -0700180# Rules to automatically install go-junit-report
181GO_JUNIT_REPORT?=$(shell which go-junit-report)
Zack Williams7cf78002019-04-30 21:45:35 -0700182ifeq (,$(GO_JUNIT_REPORT))
Scott Baker18ea5a42019-10-15 16:08:42 -0700183GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
184go_junit_install:
Zack Williams7cf78002019-04-30 21:45:35 -0700185 go get -u github.com/jstemmer/go-junit-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700186else
187go_junit_install:
Zack Williams7cf78002019-04-30 21:45:35 -0700188endif
Scott Baker18ea5a42019-10-15 16:08:42 -0700189
190# Rules to automatically install gocover-covertura
191GOCOVER_COBERTURA?=$(shell which gocover-cobertura)
Zack Williams7cf78002019-04-30 21:45:35 -0700192ifeq (,$(GOCOVER_COBERTURA))
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400193 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
Scott Baker18ea5a42019-10-15 16:08:42 -0700194gocover_cobertura_install:
195 go get -u github.com/t-yuki/gocover-cobertura
196else
197gocover_cobertura_install:
Zack Williams7cf78002019-04-30 21:45:35 -0700198endif
Scott Baker18ea5a42019-10-15 16:08:42 -0700199
200sca: golangci_lint_tool_install
201 rm -rf ./sca-report
202 @mkdir -p ./sca-report
Zack Williamsb9b1afb2019-12-09 15:28:52 -0700203 $(GOLANGCI_LINT_TOOL) run -E golint -D structcheck --out-format junit-xml ./rw_core/... ./ro_core/... ./tests/... ./common/... 2>&1 | tee ./sca-report/sca-report.xml
Scott Baker18ea5a42019-10-15 16:08:42 -0700204
Scott Bakercb7c88a2019-10-16 18:32:48 -0700205test: go_junit_install gocover_cobertura_install local-lib-go
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400206 @mkdir -p ./tests/results
girishk5259f8e2019-10-10 18:44:44 +0000207 @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 -0700208 RETURN=$$? ;\
209 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
210 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
211 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400212
David K. Bainbridge1678e192019-05-17 11:48:29 -0700213clean:
David K. Bainbridge1678e192019-05-17 11:48:29 -0700214
215distclean: clean
Zack Williamsb9b1afb2019-12-09 15:28:52 -0700216 rm -rf ./sca_report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700217
Scott Bakercb7c88a2019-10-16 18:32:48 -0700218mod-update:
219 go mod tidy
220 go mod vendor
221
khenaidoocfee5f42018-07-19 22:47:38 -0400222# end file