blob: d4549c7ce41e809caed6ee40209e7f6cb20ccf37 [file] [log] [blame]
Joey Armstrong7f9c4df2022-12-26 19:39:59 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2016-2023 Open Networking Foundation (ONF) and the ONF Contributors
Holger Hildebrandtda7758b2020-03-16 11:30:03 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joey Armstrong7f9c4df2022-12-26 19:39:59 -050016# -----------------------------------------------------------------------
17
18$(if $(DEBUG),$(warning ENTER))
19
20.DEFAULT_GOAL := help
21
22TOP ?= .
23MAKEDIR ?= $(TOP)/makefiles
24
25$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
26
27##--------------------##
28##---] INCLUDES [---##
29##--------------------##
30include $(MAKEDIR)/include.mk
31ifdef LOCAL_LINT
32 include $(MAKEDIR)/lint/golang/sca.mk
33endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000034
35# set default shell
36SHELL = bash -e -o pipefail
37
38# Variables
39VERSION ?= $(shell cat ./VERSION)
40
41DOCKER_LABEL_VCS_DIRTY = false
42ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
43 DOCKER_LABEL_VCS_DIRTY = true
44endif
Joey Armstrong7f9c4df2022-12-26 19:39:59 -050045
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000046## Docker related
47DOCKER_EXTRA_ARGS ?=
48DOCKER_REGISTRY ?=
49DOCKER_REPOSITORY ?=
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000050DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
51DOCKER_TARGET ?= prod
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000052ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG}
53TYPE ?= minimal
54
55## Docker labels. Only set ref and commit date if committed
56DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
57DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
58DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
59DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
60
61DOCKER_BUILD_ARGS ?= \
62 ${DOCKER_EXTRA_ARGS} \
63 --build-arg org_label_schema_version="${VERSION}" \
64 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
65 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
66 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
67 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
68 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
69
70# tool containers
David K. Bainbridged80007b2021-04-12 12:22:29 +000071VOLTHA_TOOLS_VERSION ?= 2.4.0
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000072
73GO = 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
74GO_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 -080075GOCOVER_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 +000076GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
77GOLANGCI_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
78HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
79
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000080.PHONY: docker-build local-protos local-lib-go help
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000081
82## Local Development Helpers
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000083local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000084ifdef LOCAL_PROTOS
khenaidoo7d3c5582021-08-11 18:09:44 -040085 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
86 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
87 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
88 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000089endif
90
91## Local Development Helpers
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000092local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000093ifdef LOCAL_LIB_GO
khenaidoo7d3c5582021-08-11 18:09:44 -040094 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
95 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
96 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000097endif
98
Joey Armstrong7f9c4df2022-12-26 19:39:59 -050099## -----------------------------------------------------------------------
100## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000101build: docker-build ## Alias for 'docker build'
102
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500103## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000104## Docker targets
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500105## -----------------------------------------------------------------------
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000106docker-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 +0000107 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 +0000108ifdef BUILD_PROFILED
David K. Bainbridge740ca852021-04-15 00:23:35 +0000109 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 .
110endif
111ifdef BUILD_RACE
112 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 +0000113endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000114
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500115## -----------------------------------------------------------------------
116## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000117docker-push: ## Push the docker images to an external repository
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000118 docker push ${ADAPTER_IMAGENAME}
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000119ifdef BUILD_PROFILED
120 docker push ${ADAPTER_IMAGENAME}-profile
121endif
David K. Bainbridge740ca852021-04-15 00:23:35 +0000122ifdef BUILD_RACE
123 docker push ${ADAPTER_IMAGENAME}-rd
124endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000125
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500126## -----------------------------------------------------------------------
127## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000128docker-kind-load: ## Load docker images into a KinD cluster
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000129 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
130 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
131
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500132## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000133## lint and unit tests
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500134## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000135lint-dockerfile: ## Perform static analysis on Dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000136 @echo "Running Dockerfile lint check ..."
137 @${HADOLINT} $$(find . -name "Dockerfile.*")
138 @echo "Dockerfile lint check OK"
139
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500140## -----------------------------------------------------------------------
141## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000142lint-style: ## Perform lint style checks on source code
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000143 @echo "Running style check..."
144 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
145 if [ ! -z "$$gofmt_out" ]; then \
146 echo "$$gofmt_out" ;\
147 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
148 exit 1 ;\
149 fi
150 @echo "Style check OK"
151
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500152## -----------------------------------------------------------------------
153## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000154lint-sanity: ## Perform basic code checks on source
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000155 @echo "Running sanity check..."
156 @${GO} vet -mod=vendor ./...
157 @echo "Sanity check OK"
158
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500159## -----------------------------------------------------------------------
160## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000161lint-mod: ## Verify the Go dependencies
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500162 $(HIDE)echo "Running dependency check..."
163 $(HIDE)$(GO) mod verify
164 $(HIDE)echo "Dependency check OK. Running vendor check..."
165 $(HIDE)git status > /dev/null
166 $(HIDE)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)
167
Joey Armstronga75baed2023-11-30 09:48:11 -0500168 $(HIDE)$(MAKE) --no-print-directory detect-local-edits
169
170 $(HIDE)$(MAKE) --no-print-directory mod-update
171
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500172 $(HIDE)git status > /dev/null
173 $(HIDE)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)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000174
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500175 $(MAKE) --no-print-directory detect-local-edits
176 $(HIDE)echo "Vendor check OK."
177
178## ---------------------------------------------------------------------
179## Intent: Determine if sandbox contains locally modified files
180## ---------------------------------------------------------------------
181clean-tree := git status --porcelain
182detect-local-edits:
183 $(HIDE)[[ `$(clean-tree)` == "" ]] || (echo "ERROR: Untracked files detected, commit or remove to continue" && echo "`git status`" && exit 1)
184
185## -----------------------------------------------------------------------
186## -----------------------------------------------------------------------
Joey Armstronge8c091f2023-01-17 16:56:26 -0500187lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000188
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500189## -----------------------------------------------------------------------
190## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000191test: lint ## Run unit tests
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000192 @mkdir -p ./tests/results
193 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
194 RETURN=$$? ;\
195 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
196 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
197 exit $$RETURN
198
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500199## -----------------------------------------------------------------------
200## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000201sca: ## Runs static code analysis with the golangci-lint tool
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000202 @mkdir -p ./sca-report
203 @echo "Running static code analysis..."
ozgecanetsiab6441962021-03-10 10:58:48 +0300204 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000205 @echo "Static code analysis OK"
206
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500207## -----------------------------------------------------------------------
208## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000209clean: distclean ## Removes any local filesystem artifacts generated by a build
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000210
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500211## -----------------------------------------------------------------------
212## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000213distclean: ## Removes any local filesystem artifacts generated by a build or test run
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500214 $(RM) -r ./sca-report
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000215
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500216## -----------------------------------------------------------------------
217## -----------------------------------------------------------------------
Joey Armstronga75baed2023-11-30 09:48:11 -0500218.PHONY: mod-update
219mod-update: mod-tidy mod-vendor
220
221## -----------------------------------------------------------------------
222## -----------------------------------------------------------------------
223.PHONY: mod-tidy
224mod-tidy :
225 $(call banner-enter,Target $@)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000226 ${GO} mod tidy
Joey Armstronga75baed2023-11-30 09:48:11 -0500227 $(call banner-leave,Target $@)
228
229## -----------------------------------------------------------------------
230## -----------------------------------------------------------------------
231.PHONY: mod-vendor
232mod-vendor : mod-tidy
233mod-vendor :
234 $(call banner-enter,Target $@)
235 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000236 ${GO} mod vendor
Joey Armstronga75baed2023-11-30 09:48:11 -0500237 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
238 $(call banner-leave,Target $@)
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000239
240# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500241help :: ## Print help for each Makefile target
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000242 @echo
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500243 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
244 | sort \
245 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
246
247# [EOF]