blob: 80b793f21b5087f6f9d6a48205ce08b582bb96f1 [file] [log] [blame]
Phaneendra Manda4c62c802019-03-06 21:37:49 +05301#
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 Jeanneret8b823f62019-05-11 11:01:28 -040017# set default shell
18SHELL = bash -e -o pipefail
19
20# Variables
21VERSION ?= $(shell cat ./VERSION)
22
Matt Jeanneretf880eb62019-07-16 20:08:03 -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 Jeanneret8b823f62019-05-11 11:01:28 -040027## Docker related
Matt Jeanneretf880eb62019-07-16 20:08:03 -040028DOCKER_EXTRA_ARGS ?=
Matt Jeanneret8b823f62019-05-11 11:01:28 -040029DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
David K. Bainbridge96b06512021-04-01 18:53:51 +000031DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040032ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openolt-adapter:${DOCKER_TAG}
David K. Bainbridge96b06512021-04-01 18:53:51 +000033DOCKER_TARGET ?= prod
Andrea Campanellacbf583e2019-12-17 15:26:00 -080034TYPE ?= minimal
Matt Jeanneret8b823f62019-05-11 11:01:28 -040035
36## Docker labels. Only set ref and commit date if committed
Matt Jeanneretf880eb62019-07-16 20:08:03 -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 Jeanneret8b823f62019-05-11 11:01:28 -040041
Matt Jeanneretf880eb62019-07-16 20:08:03 -040042DOCKER_BUILD_ARGS ?= \
43 ${DOCKER_EXTRA_ARGS} \
44 --build-arg org_label_schema_version="${VERSION}" \
45 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
46 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
47 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
48 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
49 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
50
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050051# tool containers
Matteo Scandolod5c22402020-11-23 14:28:37 -080052VOLTHA_TOOLS_VERSION ?= 2.3.1
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050053
Kent Hagerman52830082020-02-14 14:53:52 -050054GO = 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 Hagerman2cd4b7b2020-02-04 15:15:14 -050055GO_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
Matteo Scandolod5c22402020-11-23 14:28:37 -080056GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openolt-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Kent Hagerman52830082020-02-14 14:53:52 -050057GOLANGCI_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
58HADOLINT = 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 Hagerman2cd4b7b2020-02-04 15:15:14 -050059
Zack Williams75662252020-04-06 18:16:54 -070060.PHONY: docker-build local-protos local-lib-go help
61.DEFAULT_GOAL := help
Phaneendra Manda4c62c802019-03-06 21:37:49 +053062
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040063## Local Development Helpers
Zack Williams75662252020-04-06 18:16:54 -070064local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
William Kurkianea869482019-04-09 15:16:11 -040065ifdef LOCAL_PROTOS
Girish Gowdraa09aeab2020-09-14 16:30:52 -070066 rm -rf vendor/github.com/opencord/voltha-protos/v4/go
67 mkdir -p vendor/github.com/opencord/voltha-protos/v4/go
68 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go
69 rm -rf vendor/github.com/opencord/voltha-protos/v4/go/vendor
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040070endif
71
Zack Williams75662252020-04-06 18:16:54 -070072local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Baker355d1742019-10-24 10:57:52 -070073ifdef LOCAL_LIB_GO
Girish Gowdraa09aeab2020-09-14 16:30:52 -070074 mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg
75 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/
William Kurkianea869482019-04-09 15:16:11 -040076endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040077
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040078## Docker targets
Zack Williams75662252020-04-06 18:16:54 -070079build: docker-build ## Alias for 'docker build'
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -040080
Matteo Scandolo6ec3c352020-04-29 15:27:03 -070081docker-build: local-protos local-lib-go ## Build openolt adapter docker image (set BUILD_PROFILED=true to also build the profiled image)
David K. Bainbridge96b06512021-04-01 18:53:51 +000082 docker build $(DOCKER_BUILD_ARGS) --target ${DOCKER_TARGET} -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openolt .
Matteo Scandolo6ec3c352020-04-29 15:27:03 -070083ifdef BUILD_PROFILED
David K. Bainbridge96b06512021-04-01 18:53:51 +000084 docker build $(DOCKER_BUILD_ARGS) --target ${DOCKER_TARGET} --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openolt .
Matteo Scandolo6ec3c352020-04-29 15:27:03 -070085endif
Matteo Scandolo8f2b9572020-02-28 15:35:23 -080086
Zack Williams75662252020-04-06 18:16:54 -070087docker-push: ## Push the docker images to an external repository
Matt Jeanneret8b823f62019-05-11 11:01:28 -040088 docker push ${ADAPTER_IMAGENAME}
Matteo Scandolo24ed9672020-05-04 10:53:08 -070089ifdef BUILD_PROFILED
90 docker push ${ADAPTER_IMAGENAME}-profile
91endif
Matt Jeanneret8b823f62019-05-11 11:01:28 -040092
Zack Williams75662252020-04-06 18:16:54 -070093docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanellacbf583e2019-12-17 15:26:00 -080094 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
95 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
96
Matt Jeanneret8b823f62019-05-11 11:01:28 -040097## lint and unit tests
Zack Williams75662252020-04-06 18:16:54 -070098lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -050099 @echo "Running Dockerfile lint check ..."
100 @${HADOLINT} $$(find . -name "Dockerfile.*")
101 @echo "Dockerfile lint check OK"
102
Zack Williams75662252020-04-06 18:16:54 -0700103lint-mod: ## Verify the Go dependencies
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400104 @echo "Running dependency check..."
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500105 @${GO} mod verify
Scott Baker22c0e9c2019-11-26 08:11:29 -0800106 @echo "Dependency check OK. Running vendor check..."
107 @git status > /dev/null
Kent Hagermane71b52d2020-02-26 11:13:42 -0500108 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1)
109 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1)
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500110 ${GO} mod tidy
111 ${GO} mod vendor
Scott Baker22c0e9c2019-11-26 08:11:29 -0800112 @git status > /dev/null
Kent Hagermane71b52d2020-02-26 11:13:42 -0500113 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
114 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1)
Scott Baker22c0e9c2019-11-26 08:11:29 -0800115 @echo "Vendor check OK."
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400116
Zack Williams75662252020-04-06 18:16:54 -0700117lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
David Bainbridge788e5202019-10-21 18:49:40 +0000118
Zack Williams75662252020-04-06 18:16:54 -0700119test: ## Run unit tests
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400120 @mkdir -p ./tests/results
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500121 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400122 RETURN=$$? ;\
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500123 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
124 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400125 exit $$RETURN
126
Zack Williams75662252020-04-06 18:16:54 -0700127sca: ## Runs static code analysis with the golangci-lint tool
Kent Hagermane71b52d2020-02-26 11:13:42 -0500128 @rm -rf ./sca-report
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700129 @mkdir -p ./sca-report
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500130 @echo "Running static code analysis..."
Andrea Campanella21fc1b42021-03-10 16:41:33 +0100131 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Kent Hagermane71b52d2020-02-26 11:13:42 -0500132 @echo ""
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500133 @echo "Static code analysis OK"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700134
Zack Williams75662252020-04-06 18:16:54 -0700135clean: distclean ## Removes any local filesystem artifacts generated by a build
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400136
Zack Williams75662252020-04-06 18:16:54 -0700137distclean: ## Removes any local filesystem artifacts generated by a build or test run
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500138 rm -rf ./sca-report
Matt Jeanneretb5bf10e2019-05-18 15:02:51 -0400139
Zack Williams75662252020-04-06 18:16:54 -0700140mod-update: ## Update go mod files
Kent Hagerman2cd4b7b2020-02-04 15:15:14 -0500141 ${GO} mod tidy
142 ${GO} mod vendor
Zack Williams75662252020-04-06 18:16:54 -0700143
144# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
145help: ## Print help for each Makefile target
146 @echo "Usage: make [<target>]"
147 @echo "where available targets are:"
148 @echo
149 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
150 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'