blob: 011de3c2381bb6c18af160e3f941974d55ab8788 [file] [log] [blame]
Joey Armstrong7f9c4df2022-12-26 19:39:59 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
Joey Armstrong1b6902d2024-01-02 15:43:57 -05003# Copyright 2016-2024 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
Joey Armstrongfe2bf572024-04-29 12:20:35 -040022$(if $(findstring disabled-joey,$(USER)),\
23 $(eval USE_LF_MK := 1)) # special snowflake
Joey Armstrong7f9c4df2022-12-26 19:39:59 -050024
25##--------------------##
26##---] INCLUDES [---##
27##--------------------##
Joey Armstrongfe2bf572024-04-29 12:20:35 -040028ifdef USE_LF_MK
29 include lf/include.mk
30else
31 include lf/transition.mk
32endif # ifdef USE_LF_MK
33
34
35# TOP ?= .
36# MAKEDIR ?= $(TOP)/makefiles
37#
38# $(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
39
40##--------------------##
41##---] INCLUDES [---##
42##--------------------##
43# include $(MAKEDIR)/include.mk
44# ifdef LOCAL_LINT
45# include $(MAKEDIR)/lint/golang/sca.mk
46# endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000047
48# set default shell
Joey Armstrongfe2bf572024-04-29 12:20:35 -040049# SHELL = bash -e -o pipefail
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000050
51# Variables
52VERSION ?= $(shell cat ./VERSION)
53
54DOCKER_LABEL_VCS_DIRTY = false
55ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
56 DOCKER_LABEL_VCS_DIRTY = true
57endif
Joey Armstrong7f9c4df2022-12-26 19:39:59 -050058
Joey Armstrong1b6902d2024-01-02 15:43:57 -050059## [TODO] Refactor with repo:onf-make:makefiles/docker/include.mk
60
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000061## Docker related
62DOCKER_EXTRA_ARGS ?=
63DOCKER_REGISTRY ?=
64DOCKER_REPOSITORY ?=
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000065DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
66DOCKER_TARGET ?= prod
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000067ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG}
68TYPE ?= minimal
69
70## Docker labels. Only set ref and commit date if committed
71DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
72DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
73DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
74DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
75
76DOCKER_BUILD_ARGS ?= \
77 ${DOCKER_EXTRA_ARGS} \
78 --build-arg org_label_schema_version="${VERSION}" \
79 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
80 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
81 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
82 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
83 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
84
85# tool containers
David K. Bainbridged80007b2021-04-12 12:22:29 +000086VOLTHA_TOOLS_VERSION ?= 2.4.0
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000087
88GO = 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
89GO_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 -080090GOCOVER_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 +000091GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
92GOLANGCI_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
93HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
94
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000095.PHONY: docker-build local-protos local-lib-go help
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000096
Joey Armstrong1b6902d2024-01-02 15:43:57 -050097## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000098## Local Development Helpers
Joey Armstrong1b6902d2024-01-02 15:43:57 -050099## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000100local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000101ifdef LOCAL_PROTOS
khenaidoo7d3c5582021-08-11 18:09:44 -0400102 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
103 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
104 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
105 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000106endif
107
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500108## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000109## Local Development Helpers
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500110## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000111local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000112ifdef LOCAL_LIB_GO
khenaidoo7d3c5582021-08-11 18:09:44 -0400113 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
114 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
115 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000116endif
117
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500118## -----------------------------------------------------------------------
119## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000120build: docker-build ## Alias for 'docker build'
121
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500122## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000123## Docker targets
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500124## -----------------------------------------------------------------------
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000125docker-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 +0000126 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 +0000127ifdef BUILD_PROFILED
David K. Bainbridge740ca852021-04-15 00:23:35 +0000128 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 .
129endif
130ifdef BUILD_RACE
131 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 +0000132endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000133
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500134## -----------------------------------------------------------------------
135## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000136docker-push: ## Push the docker images to an external repository
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000137 docker push ${ADAPTER_IMAGENAME}
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000138ifdef BUILD_PROFILED
139 docker push ${ADAPTER_IMAGENAME}-profile
140endif
David K. Bainbridge740ca852021-04-15 00:23:35 +0000141ifdef BUILD_RACE
142 docker push ${ADAPTER_IMAGENAME}-rd
143endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000144
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500145## -----------------------------------------------------------------------
146## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000147docker-kind-load: ## Load docker images into a KinD cluster
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000148 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
149 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
150
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500151## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000152## lint and unit tests
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500153## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000154lint-dockerfile: ## Perform static analysis on Dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000155 @echo "Running Dockerfile lint check ..."
156 @${HADOLINT} $$(find . -name "Dockerfile.*")
157 @echo "Dockerfile lint check OK"
158
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500159## -----------------------------------------------------------------------
160## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000161lint-style: ## Perform lint style checks on source code
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000162 @echo "Running style check..."
163 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
164 if [ ! -z "$$gofmt_out" ]; then \
165 echo "$$gofmt_out" ;\
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500166 echo "Style check failed on one or more files ^, run 'go fmt -s -e -w' to fix." ;\
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000167 exit 1 ;\
168 fi
169 @echo "Style check OK"
170
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500171## -----------------------------------------------------------------------
172## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000173lint-sanity: ## Perform basic code checks on source
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000174 @echo "Running sanity check..."
175 @${GO} vet -mod=vendor ./...
176 @echo "Sanity check OK"
177
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500178## -----------------------------------------------------------------------
179## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000180lint-mod: ## Verify the Go dependencies
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500181 $(HIDE)echo "Running dependency check..."
182 $(HIDE)$(GO) mod verify
183 $(HIDE)echo "Dependency check OK. Running vendor check..."
184 $(HIDE)git status > /dev/null
185 $(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)
186
Joey Armstronga75baed2023-11-30 09:48:11 -0500187 $(HIDE)$(MAKE) --no-print-directory detect-local-edits
188
189 $(HIDE)$(MAKE) --no-print-directory mod-update
190
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500191 $(HIDE)git status > /dev/null
192 $(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 +0000193
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500194 $(MAKE) --no-print-directory detect-local-edits
195 $(HIDE)echo "Vendor check OK."
196
197## ---------------------------------------------------------------------
198## Intent: Determine if sandbox contains locally modified files
199## ---------------------------------------------------------------------
200clean-tree := git status --porcelain
201detect-local-edits:
202 $(HIDE)[[ `$(clean-tree)` == "" ]] || (echo "ERROR: Untracked files detected, commit or remove to continue" && echo "`git status`" && exit 1)
203
204## -----------------------------------------------------------------------
205## -----------------------------------------------------------------------
Joey Armstronge8c091f2023-01-17 16:56:26 -0500206lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000207
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500208## -----------------------------------------------------------------------
209## -----------------------------------------------------------------------
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500210results-dir := ./tests/results
211coverage-stem := $(results-dir)/go-test-coverage
212results-stem := $(results-dir)/go-test-results
213
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000214test: lint ## Run unit tests
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500215
216 $(call banner-enter,Target $@)
217 @mkdir -p $(results-dir)
218 $(HIDE)$(if $(LOCAL_FIX_PERMS),chmod o+w "$(results-dir)")
219
220 # Running remotely
221 ${GO} test -mod=vendor -v -coverprofile $(coverage-stem).out -covermode count ./... 2>&1 \
222 | tee $(results-stem).out ;\
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000223 RETURN=$$? ;\
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500224 ${GO_JUNIT_REPORT} < $(results-stem).out > $(results-stem).xml ;\
225 ${GOCOVER_COBERTURA} < $(coverage-stem).out > $(coverage-stem).xml ;\
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000226 exit $$RETURN
227
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500228 $(HIDE)$(if $(LOCAL_FIX_PERMS),chmod o-w "$(results-dir)")
229 $(call banner-enter,Target $@)
230
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500231## -----------------------------------------------------------------------
232## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000233sca: ## Runs static code analysis with the golangci-lint tool
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000234 @mkdir -p ./sca-report
235 @echo "Running static code analysis..."
ozgecanetsiab6441962021-03-10 10:58:48 +0300236 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000237 @echo "Static code analysis OK"
238
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500239## -----------------------------------------------------------------------
240## -----------------------------------------------------------------------
Joey Armstrongfe2bf572024-04-29 12:20:35 -0400241clean :: distclean ## Removes any local filesystem artifacts generated by a build
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000242
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500243## -----------------------------------------------------------------------
244## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000245distclean: ## Removes any local filesystem artifacts generated by a build or test run
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500246 $(RM) -r ./sca-report
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000247
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500248## -----------------------------------------------------------------------
249## -----------------------------------------------------------------------
Joey Armstronga75baed2023-11-30 09:48:11 -0500250.PHONY: mod-update
251mod-update: mod-tidy mod-vendor
252
253## -----------------------------------------------------------------------
254## -----------------------------------------------------------------------
255.PHONY: mod-tidy
256mod-tidy :
257 $(call banner-enter,Target $@)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000258 ${GO} mod tidy
Joey Armstronga75baed2023-11-30 09:48:11 -0500259 $(call banner-leave,Target $@)
260
261## -----------------------------------------------------------------------
262## -----------------------------------------------------------------------
263.PHONY: mod-vendor
264mod-vendor : mod-tidy
265mod-vendor :
266 $(call banner-enter,Target $@)
267 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000268 ${GO} mod vendor
Joey Armstronga75baed2023-11-30 09:48:11 -0500269 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
270 $(call banner-leave,Target $@)
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000271
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500272## -----------------------------------------------------------------------
273## For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
274## -----------------------------------------------------------------------
275## [TODO] Replace with simple printf(s), awk logic confused by double-colons
276## -----------------------------------------------------------------------
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500277help :: ## Print help for each Makefile target
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000278 @echo
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500279 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
280 | sort \
281 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500282 @printf '\n'
283 @printf ' %-33.33s %s\n' 'mod-update' \
Joey Armstrongfe2bf572024-04-29 12:20:35 -0400284 'Alias for makefile targets mod-tidy + mod-vendor'
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500285 @printf ' %-33.33s %s\n' 'mod-tidy' \
286 'Refresh packages, update go.mod and go.sum'
287 @printf ' %-33.33s %s\n' 'mod-vendor' \
288 'Update go package dependencies beneath vendor'.
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500289
290# [EOF]