blob: 447ffe229955a24d44f056f92abbbd23a76e1b46 [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
Andrea Campanella7a6cce22019-12-17 14:10:27 -080033TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040034
35## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040036DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
37DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
38DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
39DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040040
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040041DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070042 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040043 --build-arg org_label_schema_version="${VERSION}" \
44 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
45 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
46 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
47 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
48 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
49
50DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040051 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040052
Kent Hagerman812b2572020-01-28 11:57:51 -050053# tool containers
54VOLTHA_TOOLS_VERSION ?= 1.0.3
55
Kent Hagerman1b820752020-02-14 14:45:51 -050056GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
Kent Hagerman812b2572020-01-28 11:57:51 -050057GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
58GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Kent Hagerman1b820752020-02-14 14:45:51 -050059GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
60GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
61HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
Kent Hagerman812b2572020-01-28 11:57:51 -050062
Kent Hagermand5e8bd52020-01-30 16:30:16 -050063.PHONY: rw_core local-protos
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"
David K. Bainbridge1678e192019-05-17 11:48:29 -070073 @echo "clean : Remove files created by the build and tests"
Zack Williamsb9b1afb2019-12-09 15:28:52 -070074 @echo "distclean : Remove sca directory and clean"
David K. Bainbridge1678e192019-05-17 11:48:29 -070075 @echo "docker-push : Push the docker images to an external repository"
David Bainbridge5f3619c2019-07-10 22:51:09 +000076 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
David K. Bainbridge1678e192019-05-17 11:48:29 -070077 @echo "lint-style : Verify code is properly gofmt-ed"
78 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
girishk5259f8e2019-10-10 18:44:44 +000079 @echo "lint-mod : Verify the integrity of the 'mod' files"
David K. Bainbridge1678e192019-05-17 11:48:29 -070080 @echo "lint : Shorthand for lint-style & lint-sanity"
npujar03b018e2019-11-13 15:29:36 +053081 @echo "sca : Runs various SCA through golangci-lint tool"
David K. Bainbridge1678e192019-05-17 11:48:29 -070082 @echo "test : Generate reports for all go tests"
khenaidoocfee5f42018-07-19 22:47:38 -040083 @echo
84
David K. Bainbridge1678e192019-05-17 11:48:29 -070085## Local Development Helpers
86local-protos:
David K. Bainbridge1678e192019-05-17 11:48:29 -070087ifdef LOCAL_PROTOS
serkant.uluderya2ae470f2020-01-21 11:13:09 -080088 mkdir -p vendor/github.com/opencord/voltha-protos/v3/go
89 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070090endif
91
Scott Bakercb7c88a2019-10-16 18:32:48 -070092## Local Development Helpers
93local-lib-go:
94ifdef LOCAL_LIB_GO
serkant.uluderya2ae470f2020-01-21 11:13:09 -080095 mkdir -p vendor/github.com/opencord/voltha-lib-go/v3/pkg
96 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v3/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070097endif
98
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040099## Docker targets
khenaidoocfee5f42018-07-19 22:47:38 -0400100
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400101build: docker-build
khenaidoocfee5f42018-07-19 22:47:38 -0400102
Kent Hagermand5e8bd52020-01-30 16:30:16 -0500103docker-build: rw_core
sslobodra3ea7d42019-01-16 15:03:16 -0500104
Scott Bakercb7c88a2019-10-16 18:32:48 -0700105rw_core: local-protos local-lib-go
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400106 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 -0400107
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400108docker-push:
Kent Hagermanf4a3aab2019-05-22 14:42:34 -0400109 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400110
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800111docker-kind-load:
112 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
113 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)
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800114
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400115## lint and unit tests
khenaidood2b6df92018-12-13 16:37:20 -0500116
David Bainbridge5f3619c2019-07-10 22:51:09 +0000117lint-dockerfile:
David Bainbridge5f3619c2019-07-10 22:51:09 +0000118 @echo "Running Dockerfile lint check ..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500119 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000120 @echo "Dockerfile lint check OK"
121
Kent Hagerman074d0e02019-04-24 17:58:16 -0400122lint-style:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400123 @echo "Running style check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500124 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700125 if [ ! -z "$$gofmt_out" ]; then \
126 echo "$$gofmt_out" ;\
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400127 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700128 exit 1 ;\
Kent Hagerman074d0e02019-04-24 17:58:16 -0400129 fi
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400130 @echo "Style check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400131
132lint-sanity:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400133 @echo "Running sanity check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500134 @${GO} vet -mod=vendor ./...
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400135 @echo "Sanity check OK"
Kent Hagerman074d0e02019-04-24 17:58:16 -0400136
girishk5259f8e2019-10-10 18:44:44 +0000137lint-mod:
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400138 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500139 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800140 @echo "Dependency check OK. Running vendor check..."
141 @git status > /dev/null
142 @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)
143 @[[ `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)
Kent Hagerman812b2572020-01-28 11:57:51 -0500144 ${GO} mod tidy
145 ${GO} mod vendor
Scott Bakere4c2a982019-11-21 16:32:03 -0800146 @git status > /dev/null
147 @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)
148 @[[ `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)
149 @echo "Vendor check OK."
150
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400151
girishk5259f8e2019-10-10 18:44:44 +0000152lint: lint-style lint-sanity lint-mod lint-dockerfile
Kent Hagerman074d0e02019-04-24 17:58:16 -0400153
Kent Hagerman812b2572020-01-28 11:57:51 -0500154sca:
155 @rm -rf ./sca-report
Scott Baker18ea5a42019-10-15 16:08:42 -0700156 @mkdir -p ./sca-report
Kent Hagerman812b2572020-01-28 11:57:51 -0500157 @echo "Running static code analysis..."
Kent Hagerman1b820752020-02-14 14:45:51 -0500158 @${GOLANGCI_LINT} run --deadline=4m -E golint --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
159 @echo ""
Kent Hagerman812b2572020-01-28 11:57:51 -0500160 @echo "Static code analysis OK"
Scott Baker18ea5a42019-10-15 16:08:42 -0700161
Kent Hagerman812b2572020-01-28 11:57:51 -0500162test: local-lib-go
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400163 @mkdir -p ./tests/results
Kent Hagerman812b2572020-01-28 11:57:51 -0500164 @${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 -0700165 RETURN=$$? ;\
Kent Hagerman812b2572020-01-28 11:57:51 -0500166 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
167 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Zack Williams7cf78002019-04-30 21:45:35 -0700168 exit $$RETURN
Kent Hagerman074d0e02019-04-24 17:58:16 -0400169
Kent Hagerman812b2572020-01-28 11:57:51 -0500170clean: distclean
David K. Bainbridge1678e192019-05-17 11:48:29 -0700171
Kent Hagerman812b2572020-01-28 11:57:51 -0500172distclean:
173 rm -rf ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700174
Scott Bakercb7c88a2019-10-16 18:32:48 -0700175mod-update:
Kent Hagerman812b2572020-01-28 11:57:51 -0500176 ${GO} mod tidy
177 ${GO} mod vendor