blob: 97af1ebc1ea20395fe5cb8e560ac0987eff69532 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001#
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}
Kengof20xx05d74022020-12-21 11:56:38 +090032ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-eponolt-adapter:1.1.0
Takahiro Suzukid7bf8202020-12-17 20:21:59 +090033TYPE ?= 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.0.0
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 -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 voltha-protos dependency into the vendor directory
64ifdef LOCAL_PROTOS
65 rm -rf vendor/github.com/opencord/voltha-protos/v3/go
66 mkdir -p vendor/github.com/opencord/voltha-protos/v3/go
67 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v3/go
68 rm -rf vendor/github.com/opencord/voltha-protos/v3/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/v3/pkg
74 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v3/pkg/
75endif
76
77## Docker targets
78build: docker-build ## Alias for 'docker build'
79
80docker-build: local-protos local-lib-go ## Build openolt adapter docker image (set BUILD_PROFILED=true to also build the profiled image)
81 docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.eponolt .
82ifdef BUILD_PROFILED
83 docker build $(DOCKER_BUILD_ARGS) --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.eponolt .
84endif
85
86docker-push: ## Push the docker images to an external repository
87 docker push ${ADAPTER_IMAGENAME}
88ifdef BUILD_PROFILED
89 docker push ${ADAPTER_IMAGENAME}-profile
90endif
91
92docker-kind-load: ## Load docker images into a KinD cluster
93 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
94 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
95
96## lint and unit tests
97lint-dockerfile: ## Perform static analysis on Dockerfile
98 @echo "Running Dockerfile lint check ..."
99 @${HADOLINT} $$(find . -name "Dockerfile.*")
100 @echo "Dockerfile lint check OK"
101
102lint-mod: ## Verify the Go dependencies
103 @echo "Running dependency check..."
104 @${GO} mod verify
105 @echo "Dependency check OK. Running vendor check..."
106 @git status > /dev/null
107 @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)
108 @[[ `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)
109 ${GO} mod tidy
110 ${GO} mod vendor
111 @git status > /dev/null
112 @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)
113 @[[ `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)
114 @echo "Vendor check OK."
115
116lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets
117
118test: ## Run unit tests
119 @mkdir -p ./tests/results
Kengof20xx05d74022020-12-21 11:56:38 +0900120 @${GO} test -mod=vendor -timeout 60m -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Takahiro Suzukid7bf8202020-12-17 20:21:59 +0900121 RETURN=$$? ;\
122 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
123 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
124 exit $$RETURN
125
126sca: ## Runs static code analysis with the golangci-lint tool
127 @rm -rf ./sca-report
128 @mkdir -p ./sca-report
129 @echo "Running static code analysis..."
130 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
131 @echo ""
132 @echo "Static code analysis OK"
133
134clean: distclean ## Removes any local filesystem artifacts generated by a build
135
136distclean: ## Removes any local filesystem artifacts generated by a build or test run
137 rm -rf ./sca-report
138
139mod-update: ## Update go mod files
140 ${GO} mod tidy
141 ${GO} mod vendor
142
143# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
144help: ## Print help for each Makefile target
145 @echo "Usage: make [<target>]"
146 @echo "where available targets are:"
147 @echo
148 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
149 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'