blob: 10c2b8e514954aa8c193476b3f4330d179f99e8e [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
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
Joey Armstrong1b6902d2024-01-02 15:43:57 -050046## [TODO] Refactor with repo:onf-make:makefiles/docker/include.mk
47
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000048## Docker related
49DOCKER_EXTRA_ARGS ?=
50DOCKER_REGISTRY ?=
51DOCKER_REPOSITORY ?=
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000052DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
53DOCKER_TARGET ?= prod
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000054ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG}
55TYPE ?= minimal
56
57## Docker labels. Only set ref and commit date if committed
58DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
59DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
60DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
61DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
62
63DOCKER_BUILD_ARGS ?= \
64 ${DOCKER_EXTRA_ARGS} \
65 --build-arg org_label_schema_version="${VERSION}" \
66 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
67 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
68 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
69 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
70 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
71
72# tool containers
David K. Bainbridged80007b2021-04-12 12:22:29 +000073VOLTHA_TOOLS_VERSION ?= 2.4.0
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000074
75GO = 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
76GO_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 -080077GOCOVER_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 +000078GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
79GOLANGCI_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
80HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
81
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000082.PHONY: docker-build local-protos local-lib-go help
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000083
Joey Armstrong1b6902d2024-01-02 15:43:57 -050084## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000085## Local Development Helpers
Joey Armstrong1b6902d2024-01-02 15:43:57 -050086## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000087local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000088ifdef LOCAL_PROTOS
khenaidoo7d3c5582021-08-11 18:09:44 -040089 rm -rf vendor/github.com/opencord/voltha-protos/v5/go
90 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
91 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
92 rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000093endif
94
Joey Armstrong1b6902d2024-01-02 15:43:57 -050095## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000096## Local Development Helpers
Joey Armstrong1b6902d2024-01-02 15:43:57 -050097## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +000098local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000099ifdef LOCAL_LIB_GO
khenaidoo7d3c5582021-08-11 18:09:44 -0400100 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
101 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
102 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000103endif
104
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500105## -----------------------------------------------------------------------
106## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000107build: docker-build ## Alias for 'docker build'
108
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500109## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000110## Docker targets
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500111## -----------------------------------------------------------------------
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000112docker-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 +0000113 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 +0000114ifdef BUILD_PROFILED
David K. Bainbridge740ca852021-04-15 00:23:35 +0000115 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 .
116endif
117ifdef BUILD_RACE
118 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 +0000119endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000120
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500121## -----------------------------------------------------------------------
122## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000123docker-push: ## Push the docker images to an external repository
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000124 docker push ${ADAPTER_IMAGENAME}
Holger Hildebrandt6999fe52021-02-11 11:49:05 +0000125ifdef BUILD_PROFILED
126 docker push ${ADAPTER_IMAGENAME}-profile
127endif
David K. Bainbridge740ca852021-04-15 00:23:35 +0000128ifdef BUILD_RACE
129 docker push ${ADAPTER_IMAGENAME}-rd
130endif
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000131
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500132## -----------------------------------------------------------------------
133## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000134docker-kind-load: ## Load docker images into a KinD cluster
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000135 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
136 kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
137
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500138## -----------------------------------------------------------------------
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000139## lint and unit tests
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500140## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000141lint-dockerfile: ## Perform static analysis on Dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000142 @echo "Running Dockerfile lint check ..."
143 @${HADOLINT} $$(find . -name "Dockerfile.*")
144 @echo "Dockerfile lint check OK"
145
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500146## -----------------------------------------------------------------------
147## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000148lint-style: ## Perform lint style checks on source code
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000149 @echo "Running style check..."
150 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
151 if [ ! -z "$$gofmt_out" ]; then \
152 echo "$$gofmt_out" ;\
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500153 echo "Style check failed on one or more files ^, run 'go fmt -s -e -w' to fix." ;\
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000154 exit 1 ;\
155 fi
156 @echo "Style check OK"
157
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500158## -----------------------------------------------------------------------
159## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000160lint-sanity: ## Perform basic code checks on source
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000161 @echo "Running sanity check..."
162 @${GO} vet -mod=vendor ./...
163 @echo "Sanity check OK"
164
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500165## -----------------------------------------------------------------------
166## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000167lint-mod: ## Verify the Go dependencies
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500168 $(HIDE)echo "Running dependency check..."
169 $(HIDE)$(GO) mod verify
170 $(HIDE)echo "Dependency check OK. Running vendor check..."
171 $(HIDE)git status > /dev/null
172 $(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)
173
Joey Armstronga75baed2023-11-30 09:48:11 -0500174 $(HIDE)$(MAKE) --no-print-directory detect-local-edits
175
176 $(HIDE)$(MAKE) --no-print-directory mod-update
177
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500178 $(HIDE)git status > /dev/null
179 $(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 +0000180
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500181 $(MAKE) --no-print-directory detect-local-edits
182 $(HIDE)echo "Vendor check OK."
183
184## ---------------------------------------------------------------------
185## Intent: Determine if sandbox contains locally modified files
186## ---------------------------------------------------------------------
187clean-tree := git status --porcelain
188detect-local-edits:
189 $(HIDE)[[ `$(clean-tree)` == "" ]] || (echo "ERROR: Untracked files detected, commit or remove to continue" && echo "`git status`" && exit 1)
190
191## -----------------------------------------------------------------------
192## -----------------------------------------------------------------------
Joey Armstronge8c091f2023-01-17 16:56:26 -0500193lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000194
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500195## -----------------------------------------------------------------------
196## -----------------------------------------------------------------------
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500197results-dir := ./tests/results
198coverage-stem := $(results-dir)/go-test-coverage
199results-stem := $(results-dir)/go-test-results
200
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000201test: lint ## Run unit tests
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500202
203 $(call banner-enter,Target $@)
204 @mkdir -p $(results-dir)
205 $(HIDE)$(if $(LOCAL_FIX_PERMS),chmod o+w "$(results-dir)")
206
207 # Running remotely
208 ${GO} test -mod=vendor -v -coverprofile $(coverage-stem).out -covermode count ./... 2>&1 \
209 | tee $(results-stem).out ;\
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000210 RETURN=$$? ;\
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500211 ${GO_JUNIT_REPORT} < $(results-stem).out > $(results-stem).xml ;\
212 ${GOCOVER_COBERTURA} < $(coverage-stem).out > $(coverage-stem).xml ;\
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000213 exit $$RETURN
214
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500215 $(HIDE)$(if $(LOCAL_FIX_PERMS),chmod o-w "$(results-dir)")
216 $(call banner-enter,Target $@)
217
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500218## -----------------------------------------------------------------------
219## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000220sca: ## Runs static code analysis with the golangci-lint tool
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000221 @mkdir -p ./sca-report
222 @echo "Running static code analysis..."
ozgecanetsiab6441962021-03-10 10:58:48 +0300223 @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000224 @echo "Static code analysis OK"
225
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500226## -----------------------------------------------------------------------
227## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000228clean: distclean ## Removes any local filesystem artifacts generated by a build
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000229
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500230## -----------------------------------------------------------------------
231## -----------------------------------------------------------------------
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000232distclean: ## Removes any local filesystem artifacts generated by a build or test run
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500233 $(RM) -r ./sca-report
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000234
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500235## -----------------------------------------------------------------------
236## -----------------------------------------------------------------------
Joey Armstronga75baed2023-11-30 09:48:11 -0500237.PHONY: mod-update
238mod-update: mod-tidy mod-vendor
239
240## -----------------------------------------------------------------------
241## -----------------------------------------------------------------------
242.PHONY: mod-tidy
243mod-tidy :
244 $(call banner-enter,Target $@)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000245 ${GO} mod tidy
Joey Armstronga75baed2023-11-30 09:48:11 -0500246 $(call banner-leave,Target $@)
247
248## -----------------------------------------------------------------------
249## -----------------------------------------------------------------------
250.PHONY: mod-vendor
251mod-vendor : mod-tidy
252mod-vendor :
253 $(call banner-enter,Target $@)
254 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000255 ${GO} mod vendor
Joey Armstronga75baed2023-11-30 09:48:11 -0500256 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
257 $(call banner-leave,Target $@)
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000258
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500259## -----------------------------------------------------------------------
260## For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
261## -----------------------------------------------------------------------
262## [TODO] Replace with simple printf(s), awk logic confused by double-colons
263## -----------------------------------------------------------------------
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500264help :: ## Print help for each Makefile target
David K. Bainbridgeee01bc42021-04-06 17:38:20 +0000265 @echo
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500266 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
267 | sort \
268 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
Joey Armstrong1b6902d2024-01-02 15:43:57 -0500269 @printf '\n'
270 @printf ' %-33.33s %s\n' 'mod-update' \
271 'Alias for makefile targets mod-tidy + mod-vendor
272 @printf ' %-33.33s %s\n' 'mod-tidy' \
273 'Refresh packages, update go.mod and go.sum'
274 @printf ' %-33.33s %s\n' 'mod-vendor' \
275 'Update go package dependencies beneath vendor'.
Joey Armstrong7f9c4df2022-12-26 19:39:59 -0500276
277# [EOF]