blob: 929588d3a2af1aad03de0017e78c2beea83f3aa2 [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
Andrea Campanelladec47f92021-05-27 15:32:45 +020069 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
70 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
71 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
Don Newton98fd8812019-09-23 15:15:02 -040072endif
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. Bainbridgee05cf0c2021-08-19 03:16:50 +000077 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
78 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
79 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/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
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500114
115## -----------------------------------------------------------------------
116## -----------------------------------------------------------------------
117.PHONY: mod-update
118mod-update: go-version mod-tidy mod-vendor
119
120## -----------------------------------------------------------------------
121## -----------------------------------------------------------------------
122.PHONY: go-version
123go-version :
124 $(call banner-enter,Target $@)
125 ${GO} version
126 $(call banner-leave,Target $@)
127
128## -----------------------------------------------------------------------
129## -----------------------------------------------------------------------
130.PHONY: mod-tidy
131mod-tidy :
132 $(call banner-enter,Target $@)
133 ${GO} mod tidy
134 $(call banner-leave,Target $@)
135
136## -----------------------------------------------------------------------
137## -----------------------------------------------------------------------
138.PHONY: mod-vendor
139mod-vendor : mod-tidy
140mod-vendor :
141 $(call banner-enter,Target $@)
142 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
143 ${GO} mod vendor
144 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
145 $(call banner-leave,Target $@)
146
Don Newton98fd8812019-09-23 15:15:02 -0400147## lint and unit tests
148
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000149lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagerman6379e092020-02-18 18:05:57 -0500150 @echo "Running Dockerfile lint check..."
151 @${HADOLINT} $$(find . -name "Dockerfile.*")
152 @echo "Dockerfile lint check OK"
153
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000154lint-mod: ## Verify the Go dependencies
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500155 $(call banner-entry,Target $@)
Don Newton98fd8812019-09-23 15:15:02 -0400156 @echo "Running dependency check..."
Kent Hagerman6379e092020-02-18 18:05:57 -0500157 @${GO} mod verify
158 @echo "Dependency check OK. Running vendor check..."
159 @git status > /dev/null
Kent Hagerman3243ee52020-02-26 12:11:55 -0500160 @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)
161 @[[ `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)
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500162
163 $(HIDE)$(MAKE) --no-print-directory mod-update
164
Kent Hagerman6379e092020-02-18 18:05:57 -0500165 @git status > /dev/null
Kent Hagerman3243ee52020-02-26 12:11:55 -0500166 @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)
167 @[[ `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 -0500168 @echo "Vendor check OK."
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500169 $(call banner-leave,Target $@)
Don Newton98fd8812019-09-23 15:15:02 -0400170
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500171## -----------------------------------------------------------------------
172## -----------------------------------------------------------------------
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000173lint: lint-mod lint-dockerfile ## Run all lint targets
Don Newton98fd8812019-09-23 15:15:02 -0400174
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500175## -----------------------------------------------------------------------
176## -----------------------------------------------------------------------
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000177test: local-lib-go ## Run unit tests
Don Newton98fd8812019-09-23 15:15:02 -0400178 @mkdir -p ./tests/results
Kent Hagerman6379e092020-02-18 18:05:57 -0500179 @${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 -0400180 RETURN=$$? ;\
Kent Hagerman6379e092020-02-18 18:05:57 -0500181 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
182 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Don Newton98fd8812019-09-23 15:15:02 -0400183 exit $$RETURN
184
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000185sca: ## Runs static code analysis with the golangci-lint tool
Kent Hagerman6379e092020-02-18 18:05:57 -0500186 @rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400187 @mkdir -p ./sca-report
Kent Hagerman6379e092020-02-18 18:05:57 -0500188 @echo "Running static code analysis..."
189 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
190 @echo ""
191 @echo "Static code analysis OK"
Don Newton98fd8812019-09-23 15:15:02 -0400192
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000193clean: ## Removes any local filesystem artifacts generated by a build
Kent Hagerman6379e092020-02-18 18:05:57 -0500194 rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400195
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000196distclean: clean ## Removes any local filesystem artifacts generated by a build or test run
Don Newton98fd8812019-09-23 15:15:02 -0400197 rm -rf ${VENVDIR}
198
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000199# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
200help: ## Print help for each Makefile target
201 @echo "Usage: make [<target>]"
202 @echo "where available targets are:"
203 @echo
204 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
205 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
Joey Armstrong43f5cc02024-01-12 18:33:25 -0500206 @printf ' %-33.33s %s\n' 'mod-update' 'Update go.mod files (vendor and tidy)'