blob: ad3a4cac2fa939afe298bc89e45004af25c4090a [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -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
17# set default shell
18SHELL = bash -e -o pipefail
19
20# Variables
Kent Hagerman6379e092020-02-18 18:05:57 -050021VERSION ?= $(shell cat ./VERSION)
Don Newton98fd8812019-09-23 15:15:02 -040022
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
Kent Hagerman6379e092020-02-18 18:05:57 -050028DOCKER_EXTRA_ARGS ?=
29DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
31DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000032DOCKER_TARGET ?= prod
Zack Williamsa054c7f2020-03-23 10:19:29 -070033ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-ofagent-go
Don Newton98fd8812019-09-23 15:15:02 -040034
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
50DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
51 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
52
Kent Hagerman6379e092020-02-18 18:05:57 -050053# tool containers
David K. Bainbridge595b6702021-04-09 16:10:58 +000054VOLTHA_TOOLS_VERSION ?= 2.4.0
Kent Hagerman6379e092020-02-18 18:05:57 -050055
56GO = 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
57GO_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 Scandolo154295f2020-11-23 15:29:02 -080058GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/ofagent-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
Kent Hagerman6379e092020-02-18 18:05:57 -050059GOLANGCI_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
60HADOLINT = 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
61
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000062.PHONY: docker-build local-protos local-lib-go local-voltha help
63.DEFAULT_GOAL := help
Don Newton98fd8812019-09-23 15:15:02 -040064
65## Local Development Helpers
66
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000067local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
Don Newton98fd8812019-09-23 15:15:02 -040068ifdef LOCAL_PROTOS
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000069 rm -rf vendor/github.com/opencord/voltha-protos/go
Don Newton98fd8812019-09-23 15:15:02 -040070 mkdir -p vendor/github.com/opencord/voltha-protos/go
71 cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go
72endif
73
divyadesai892fab52020-03-24 08:45:09 +000074## Local Development Helpers
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000075local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
divyadesai892fab52020-03-24 08:45:09 +000076ifdef LOCAL_LIB_GO
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000077 rm -rf vendor/github.com/opencord/voltha-lib-go/v4/pkg
Maninder12b909f2020-10-23 14:23:36 +053078 mkdir -p vendor/github.com/opencord/voltha-lib-go/v4/pkg
79 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v4/pkg/
divyadesai892fab52020-03-24 08:45:09 +000080endif
81
82
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000083local-voltha: ## Copies a local version of the voltha-go dependency into the vendor directory
Don Newton98fd8812019-09-23 15:15:02 -040084ifdef LOCAL_VOLTHA
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000085 rm -rf vendor/github.com/opencord/voltha-go/
Don Newton98fd8812019-09-23 15:15:02 -040086 mkdir -p vendor/github.com/opencord/voltha-go/
87 cp -rf ${GOPATH}/src/github.com/opencord/voltha-go/ vendor/github.com/opencord/
88 rm -rf vendor/github.com/opencord/voltha-go/vendor
89endif
90
divyadesai892fab52020-03-24 08:45:09 +000091## Docker targets
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000092build: docker-build ## Alias for 'docker-build'
Don Newton98fd8812019-09-23 15:15:02 -040093
94## Docker targets
95
David K. Bainbridge3d5ac932021-04-06 18:08:35 +000096docker-build: local-protos local-voltha local-lib-go ## Build docker image (set BUILD_PROFILED=true to also build the profiled image)
David K. Bainbridgeb02c6b92021-04-14 23:36:08 +000097 docker build $(DOCKER_BUILD_ARGS) --build-arg CGO_PARAMETER=0 --target=${DOCKER_TARGET} -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG} -f docker/Dockerfile.ofagent-go .
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -070098ifdef BUILD_PROFILED
David K. Bainbridgeb02c6b92021-04-14 23:36:08 +000099 docker build $(DOCKER_BUILD_ARGS) --build-arg CGO_PARAMETER=1 --target=dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.ofagent-go .
100endif
101ifdef BUILD_RACE
102 docker build $(DOCKER_BUILD_ARGS) --build-arg CGO_PARAMETER=1 --target=dev --build-arg EXTRA_GO_BUILD_TAGS="--race" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-rd -f docker/Dockerfile.ofagent-go .
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700103endif
Don Newton98fd8812019-09-23 15:15:02 -0400104
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000105docker-push: ## Push the docker images to an external repository
Kent Hagerman6379e092020-02-18 18:05:57 -0500106 docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700107ifdef BUILD_PROFILED
108 docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile
109endif
David K. Bainbridgeb02c6b92021-04-14 23:36:08 +0000110ifdef BUILD_RACE
111 docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-rd
112endif
Don Newton98fd8812019-09-23 15:15:02 -0400113
114## lint and unit tests
115
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000116lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagerman6379e092020-02-18 18:05:57 -0500117 @echo "Running Dockerfile lint check..."
118 @${HADOLINT} $$(find . -name "Dockerfile.*")
119 @echo "Dockerfile lint check OK"
120
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000121lint-mod: ## Verify the Go dependencies
Don Newton98fd8812019-09-23 15:15:02 -0400122 @echo "Running dependency check..."
Kent Hagerman6379e092020-02-18 18:05:57 -0500123 @${GO} mod verify
124 @echo "Dependency check OK. Running vendor check..."
125 @git status > /dev/null
Kent Hagerman3243ee52020-02-26 12:11:55 -0500126 @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)
127 @[[ `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 Hagerman6379e092020-02-18 18:05:57 -0500128 ${GO} mod tidy
129 ${GO} mod vendor
130 @git status > /dev/null
Kent Hagerman3243ee52020-02-26 12:11:55 -0500131 @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)
132 @[[ `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)
Kent Hagerman6379e092020-02-18 18:05:57 -0500133 @echo "Vendor check OK."
Don Newton98fd8812019-09-23 15:15:02 -0400134
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000135lint: lint-mod lint-dockerfile ## Run all lint targets
Don Newton98fd8812019-09-23 15:15:02 -0400136
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000137test: local-lib-go ## Run unit tests
Don Newton98fd8812019-09-23 15:15:02 -0400138 @mkdir -p ./tests/results
Kent Hagerman6379e092020-02-18 18:05:57 -0500139 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Don Newton98fd8812019-09-23 15:15:02 -0400140 RETURN=$$? ;\
Kent Hagerman6379e092020-02-18 18:05:57 -0500141 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
142 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Don Newton98fd8812019-09-23 15:15:02 -0400143 exit $$RETURN
144
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000145sca: ## Runs static code analysis with the golangci-lint tool
Kent Hagerman6379e092020-02-18 18:05:57 -0500146 @rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400147 @mkdir -p ./sca-report
Kent Hagerman6379e092020-02-18 18:05:57 -0500148 @echo "Running static code analysis..."
149 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
150 @echo ""
151 @echo "Static code analysis OK"
Don Newton98fd8812019-09-23 15:15:02 -0400152
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000153clean: ## Removes any local filesystem artifacts generated by a build
Kent Hagerman6379e092020-02-18 18:05:57 -0500154 rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400155
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000156distclean: clean ## Removes any local filesystem artifacts generated by a build or test run
Don Newton98fd8812019-09-23 15:15:02 -0400157 rm -rf ${VENVDIR}
158
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000159mod-update: ## Update go mod files
Kent Hagerman6379e092020-02-18 18:05:57 -0500160 ${GO} mod tidy
161 ${GO} mod vendor
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000162
163# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
164help: ## Print help for each Makefile target
165 @echo "Usage: make [<target>]"
166 @echo "where available targets are:"
167 @echo
168 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
169 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'