blob: 40525f12662b9529c002f350a19fcb311f14cf6b [file] [log] [blame]
Holger Hildebrandtda7758b2020-03-16 11:30:03 +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 ?=
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000031DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
32DOCKER_TARGET ?= prod
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000033ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG}
34TYPE ?= minimal
35
36## Docker labels. Only set ref and commit date if committed
37DOCKER_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)
41
42DOCKER_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
51# tool containers
David K. Bainbridged80007b2021-04-12 12:22:29 +000052VOLTHA_TOOLS_VERSION ?= 2.4.0
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000053
54GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
55GO_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 Scandolo761f7512020-11-23 15:52:40 -080056GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openonu-adapter-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000057GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
58GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
59HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
60
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000061.PHONY: docker-build local-protos local-lib-go help
62.DEFAULT_GOAL := help
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000063
64## Local Development Helpers
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000065local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000066ifdef LOCAL_PROTOS
Holger Hildebrandt6e846d12020-12-11 14:35:55 +000067 rm -rf vendor/github.com/opencord/voltha-protos/v4/go
68 mkdir -p vendor/github.com/opencord/voltha-protos/v4/go
69 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v4/go
70 rm -rf vendor/github.com/opencord/voltha-protos/v4/go/vendor
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000071endif
72
73## Local Development Helpers
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000074local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000075ifdef LOCAL_LIB_GO
Girish Gowdra50e56422021-06-01 16:46:04 -070076 rm -rf vendor/github.com/opencord/voltha-lib-go/v5/pkg
77 mkdir -p vendor/github.com/opencord/voltha-lib-go/v5/pkg
78 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v5/pkg/
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000079endif
80
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000081build: docker-build ## Alias for 'docker build'
82
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000083## Docker targets
Holger Hildebrandt6999fe52021-02-11 11:49:05 +000084docker-build: local-protos local-lib-go ## Build openonu adapter docker image (set BUILD_PROFILED=true to also build the profiled image)
David K. Bainbridge740ca852021-04-15 00:23:35 +000085 docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg CGO_PARAMETER=0 -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openonu .
Holger Hildebrandt6999fe52021-02-11 11:49:05 +000086ifdef BUILD_PROFILED
David K. Bainbridge740ca852021-04-15 00:23:35 +000087 docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openonu .
88endif
89ifdef BUILD_RACE
90 docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-race" -t ${ADAPTER_IMAGENAME}-rd -f docker/Dockerfile.openonu .
Holger Hildebrandt6999fe52021-02-11 11:49:05 +000091endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000092
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000093docker-push: ## Push the docker images to an external repository
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000094 docker push ${ADAPTER_IMAGENAME}
Holger Hildebrandt6999fe52021-02-11 11:49:05 +000095ifdef BUILD_PROFILED
96 docker push ${ADAPTER_IMAGENAME}-profile
97endif
David K. Bainbridge740ca852021-04-15 00:23:35 +000098ifdef BUILD_RACE
99 docker push ${ADAPTER_IMAGENAME}-rd
100endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000101
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000102docker-kind-load: ## Load docker images into a KinD cluster
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000103 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
104 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
105
106
107## lint and unit tests
108
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000109lint-dockerfile: ## Perform static analysis on Dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000110 @echo "Running Dockerfile lint check ..."
111 @${HADOLINT} $$(find . -name "Dockerfile.*")
112 @echo "Dockerfile lint check OK"
113
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000114lint-style: ## Perform lint style checks on source code
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000115 @echo "Running style check..."
116 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
117 if [ ! -z "$$gofmt_out" ]; then \
118 echo "$$gofmt_out" ;\
119 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
120 exit 1 ;\
121 fi
122 @echo "Style check OK"
123
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000124lint-sanity: ## Perform basic code checks on source
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000125 @echo "Running sanity check..."
126 @${GO} vet -mod=vendor ./...
127 @echo "Sanity check OK"
128
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000129lint-mod: ## Verify the Go dependencies
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000130 @echo "Running dependency check..."
131 @${GO} mod verify
132 @echo "Dependency check OK. Running vendor check..."
133 @git status > /dev/null
134 @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)
135 @[[ `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)
136 ${GO} mod tidy
137 ${GO} mod vendor
138 @git status > /dev/null
139 @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)
140 @[[ `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)
141 @echo "Vendor check OK."
142
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000143lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile ## Run all lint targets
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000144
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000145test: lint ## Run unit tests
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000146 @mkdir -p ./tests/results
147 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
148 RETURN=$$? ;\
149 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
150 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
151 exit $$RETURN
152
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000153sca: ## Runs static code analysis with the golangci-lint tool
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000154 @mkdir -p ./sca-report
155 @echo "Running static code analysis..."
ozgecanetsiab6441962021-03-10 10:58:48 +0300156 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000157 @echo "Static code analysis OK"
158
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000159clean: distclean ## Removes any local filesystem artifacts generated by a build
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000160
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000161distclean: ## Removes any local filesystem artifacts generated by a build or test run
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000162 rm -rf ./sca-report
163
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000164mod-update: ## Update go mod files
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000165 ${GO} mod tidy
166 ${GO} mod vendor
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000167
168# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
169help: ## Print help for each Makefile target
170 @echo "Usage: make [<target>]"
171 @echo "where available targets are:"
172 @echo
173 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
174 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'