blob: 210fd0ebcbd533aa180b9ff8b6f2daf48d2972b6 [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)
97 docker build $(DOCKER_BUILD_ARGS) --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. Bainbridge3d5ac932021-04-06 18:08:35 +000099 docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.ofagent-go .
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700100endif
Don Newton98fd8812019-09-23 15:15:02 -0400101
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000102docker-push: ## Push the docker images to an external repository
Kent Hagerman6379e092020-02-18 18:05:57 -0500103 docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}
Matteo Scandoloa98f0dc2020-05-13 10:39:29 -0700104ifdef BUILD_PROFILED
105 docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}-profile
106endif
Don Newton98fd8812019-09-23 15:15:02 -0400107
108## lint and unit tests
109
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000110lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagerman6379e092020-02-18 18:05:57 -0500111 @echo "Running Dockerfile lint check..."
112 @${HADOLINT} $$(find . -name "Dockerfile.*")
113 @echo "Dockerfile lint check OK"
114
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000115lint-mod: ## Verify the Go dependencies
Don Newton98fd8812019-09-23 15:15:02 -0400116 @echo "Running dependency check..."
Kent Hagerman6379e092020-02-18 18:05:57 -0500117 @${GO} mod verify
118 @echo "Dependency check OK. Running vendor check..."
119 @git status > /dev/null
Kent Hagerman3243ee52020-02-26 12:11:55 -0500120 @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)
121 @[[ `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 -0500122 ${GO} mod tidy
123 ${GO} mod vendor
124 @git status > /dev/null
Kent Hagerman3243ee52020-02-26 12:11:55 -0500125 @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)
126 @[[ `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 -0500127 @echo "Vendor check OK."
Don Newton98fd8812019-09-23 15:15:02 -0400128
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000129lint: lint-mod lint-dockerfile ## Run all lint targets
Don Newton98fd8812019-09-23 15:15:02 -0400130
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000131test: local-lib-go ## Run unit tests
Don Newton98fd8812019-09-23 15:15:02 -0400132 @mkdir -p ./tests/results
Kent Hagerman6379e092020-02-18 18:05:57 -0500133 @${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 -0400134 RETURN=$$? ;\
Kent Hagerman6379e092020-02-18 18:05:57 -0500135 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
136 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Don Newton98fd8812019-09-23 15:15:02 -0400137 exit $$RETURN
138
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000139sca: ## Runs static code analysis with the golangci-lint tool
Kent Hagerman6379e092020-02-18 18:05:57 -0500140 @rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400141 @mkdir -p ./sca-report
Kent Hagerman6379e092020-02-18 18:05:57 -0500142 @echo "Running static code analysis..."
143 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
144 @echo ""
145 @echo "Static code analysis OK"
Don Newton98fd8812019-09-23 15:15:02 -0400146
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000147clean: ## Removes any local filesystem artifacts generated by a build
Kent Hagerman6379e092020-02-18 18:05:57 -0500148 rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400149
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000150distclean: clean ## Removes any local filesystem artifacts generated by a build or test run
Don Newton98fd8812019-09-23 15:15:02 -0400151 rm -rf ${VENVDIR}
152
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000153mod-update: ## Update go mod files
Kent Hagerman6379e092020-02-18 18:05:57 -0500154 ${GO} mod tidy
155 ${GO} mod vendor
David K. Bainbridge3d5ac932021-04-06 18:08:35 +0000156
157# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
158help: ## Print help for each Makefile target
159 @echo "Usage: make [<target>]"
160 @echo "where available targets are:"
161 @echo
162 @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
163 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'