blob: 218db5e078cd6b038f4a850cb4a7631ae6fe9cf3 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001#
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
17# set default shell
18SHELL = bash -e -o pipefail
19
20# Variables
21VERSION ?= $(shell cat ./VERSION)
22
23DOCKER_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
27## Docker related
28DOCKER_EXTRA_ARGS ?=
29DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
31DOCKER_TAG ?= ${VERSION}
32OPENDEVMGR_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}opendevice-manager:${DOCKER_TAG}
33TYPE ?= minimal
34
35## Docker labels. Only set ref and commit date if committed
36DOCKER_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)
40
41DOCKER_BUILD_ARGS ?= \
42 ${DOCKER_EXTRA_ARGS} \
43 --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
50# tool containers
51VOLTHA_TOOLS_VERSION ?= 2.3.1
52
53GO = 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
54GO_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
55GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/opendevice-manager -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
56GOLANGCI_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
57HADOLINT = 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
58
59.PHONY: docker-build local-protos local-lib-go help
60.DEFAULT_GOAL := help
61
62## Local Development Helpers
63local-protos: ## Copies a local version of the dmi-protos dependency into the vendor directory
64ifdef LOCAL_PROTOS
65 rm -rf vendor/github.com/opencord/device-management-interface/go
66 mkdir -p vendor/github.com/opencord/device-management-interface/go
67 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/device-management-interface/go
68 rm -rf vendor/github.com/opencord/device-management-interface/go/vendor
69endif
70
71local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
72ifdef LOCAL_LIB_GO
73 mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg
74 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/
75endif
76
77## Docker targets
78build: docker-build ## Alias for 'docker build'
79
80# docker-build: local-protos local-lib-go ## Build opendevice manager docker image (set BUILD_PROFILED=true to also build the profiled image)
81docker-build: ## Build opendevice manager docker image (set BUILD_PROFILED=true to also build the profiled image)
82 docker build $(DOCKER_BUILD_ARGS) -t ${OPENDEVMGR_IMAGENAME} -f docker/Dockerfile.opendevmgr .
83ifdef BUILD_PROFILED
84 docker build $(DOCKER_BUILD_ARGS) --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${OPENDEVMGR_IMAGENAME}-profile -f docker/Dockerfile.opendevmgr .
85endif
86
87docker-push: ## Push the docker images to an external repository
88 docker push ${OPENDEVMGR_IMAGENAME}
89ifdef BUILD_PROFILED
90 docker push ${OPENDEVMGR_IMAGENAME}-profile
91endif
92
93docker-kind-load: ## Load docker images into a KinD cluster
94 @if [ "`kind get clusters | grep opendevmgr-$(TYPE)`" = '' ]; then echo "no opendevmgr-$(TYPE) cluster found" && exit 1; fi
95 kind load docker-image ${OPENDEVMGR_IMAGENAME} --name=opendevmgr-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
96
97## lint and unit tests
98lint-dockerfile: ## Perform static analysis on Dockerfile
99 @echo "Running Dockerfile lint check ..."
100 @${HADOLINT} $$(find . -name "Dockerfile.*")
101 @echo "Dockerfile lint check OK"
102
103lint-mod: ## Verify the Go dependencies
104 @echo "Running dependency check..."
105 @${GO} mod verify
106 @echo "Dependency check OK. Running vendor check..."
107 @git status > /dev/null
108 @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)
110 ${GO} mod tidy
111 ${GO} mod vendor
112 @git status > /dev/null
113 @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)
115 @echo "Vendor check OK."
116
117lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
118
119test: ## Run unit tests
120 @echo "Call unit test case suite"
121
122sca: ## Runs static code analysis with the golangci-lint tool
123 @rm -rf ./sca-report
124 @mkdir -p ./sca-report
125 @echo "Running static code analysis..."
126 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
127 @echo ""
128 @echo "Static code analysis OK"
129
130clean: distclean ## Removes any local filesystem artifacts generated by a build
131
132distclean: ## Removes any local filesystem artifacts generated by a build or test run
133 rm -rf ./sca-report
134
135mod-update: ## Update go mod files
136 ${GO} mod tidy
137 ${GO} mod vendor
138
139# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
140help: ## Print help for each Makefile target
141 @echo "Usage: make [<target>]"
142 @echo "where available targets are:"
143 @echo
144 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
145 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'