blob: 5a7f04da2ef69f07f5186c2ba868fccaa14f9e07 [file] [log] [blame]
Joey Armstrongb085c502023-01-17 13:56:24 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05003# Copyright 2016-2024 Open Networking Foundation (ONF) and the ONF Contributors
Scott Baker2c1c4822019-10-16 11:02:41 -07004#
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 Armstrongb085c502023-01-17 13:56:24 -050016# -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070017
Joey Armstrongb085c502023-01-17 13:56:24 -050018.DEFAULT_GOAL := help
19
20##-------------------##
21##---] GLOBALS [---##
22##-------------------##
Joey Armstrongb085c502023-01-17 13:56:24 -050023
24##--------------------##
25##---] INCLUDES [---##
26##--------------------##
Joey Armstrong7cd92a12024-05-01 17:47:58 -040027$(if $(findstring disabled-joey,$(USER)),\
28 $(eval USE_LF_MK := 1)) # special snowflake
29
30##--------------------##
31##---] INCLUDES [---##
32##--------------------##
33ifdef USE_LF_MK
34 $(error should not be here)
35 include lf/include.mk
36else
37 include lf/transition.mk
38endif # ifdef USE_LF_MK
Scott Baker2c1c4822019-10-16 11:02:41 -070039
40# Variables
41VERSION ?= $(shell cat ./VERSION)
42
Joey Armstrongb085c502023-01-17 13:56:24 -050043## -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070044## Local Development Helpers
Joey Armstrongb085c502023-01-17 13:56:24 -050045## -----------------------------------------------------------------------
46.PHONY: local-protos
Scott Baker2c1c4822019-10-16 11:02:41 -070047local-protos:
48 @mkdir -p python/local_imports
49ifdef LOCAL_PROTOS
Joey Armstrong26245a32022-12-17 21:49:06 -050050 $(RM) -r vendor/github.com/opencord/voltha-protos
khenaidoo26721882021-08-11 17:42:52 -040051 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
52 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
Joey Armstrong26245a32022-12-17 21:49:06 -050053 $(RM) -r python/local_imports/voltha-protos
Scott Baker2c1c4822019-10-16 11:02:41 -070054 mkdir -p python/local_imports/voltha-protos/dist
Scott Bakerf1b096c2019-11-01 12:36:30 -070055 cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/
Scott Baker2c1c4822019-10-16 11:02:41 -070056endif
57
Joey Armstrongb085c502023-01-17 13:56:24 -050058## -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070059## build the library
Joey Armstrongb085c502023-01-17 13:56:24 -050060## -----------------------------------------------------------------------
Scott Bakerf1b096c2019-11-01 12:36:30 -070061build: local-protos
Joey Armstrong7f8436c2023-07-09 20:23:27 -040062 $(call banner-enter,Target $@)
Kent Hagermanfac11d42020-01-28 12:18:55 -050063 ${GO} build -mod=vendor ./...
Joey Armstrong7f8436c2023-07-09 20:23:27 -040064 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -070065
Joey Armstrongb085c502023-01-17 13:56:24 -050066## -----------------------------------------------------------------------
67## -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070068lint-mod:
Joey Armstrong7f8436c2023-07-09 20:23:27 -040069 $(call banner-entry,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -070070 @echo "Running dependency check..."
Kent Hagermanfac11d42020-01-28 12:18:55 -050071 @${GO} mod verify
Scott Bakera36b4982019-11-26 08:09:23 -080072 @echo "Dependency check OK. Running vendor check..."
73 @git status > /dev/null
Kent Hagerman3da1fd02020-02-26 10:54:07 -050074 @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)
75 @[[ `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 Armstrong7f8436c2023-07-09 20:23:27 -040076
77 $(HIDE)$(MAKE) --no-print-directory mod-update
78
Scott Bakera36b4982019-11-26 08:09:23 -080079 @git status > /dev/null
Kent Hagerman3da1fd02020-02-26 10:54:07 -050080 @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)
81 @[[ `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)
Scott Bakera36b4982019-11-26 08:09:23 -080082 @echo "Vendor check OK."
Joey Armstrong7f8436c2023-07-09 20:23:27 -040083 $(call banner-leave,Target $@)
84
85## -----------------------------------------------------------------------
86## -----------------------------------------------------------------------
87.PHONY: mod-update
Joey Armstrong074f64f2023-12-01 10:05:02 -050088mod-update: go-version mod-tidy mod-vendor
89
90## -----------------------------------------------------------------------
91## -----------------------------------------------------------------------
92.PHONY: go-version
93go-version :
94 $(call banner-enter,Target $@)
95 ${GO} version
96 $(call banner-leave,Target $@)
Joey Armstrong7f8436c2023-07-09 20:23:27 -040097
98## -----------------------------------------------------------------------
99## -----------------------------------------------------------------------
100.PHONY: mod-tidy
101mod-tidy :
102 $(call banner-enter,Target $@)
103 ${GO} mod tidy
104 $(call banner-leave,Target $@)
105
106## -----------------------------------------------------------------------
107## -----------------------------------------------------------------------
108.PHONY: mod-vendor
109mod-vendor : mod-tidy
110mod-vendor :
111 $(call banner-enter,Target $@)
112 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
113 ${GO} mod vendor
114 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
115 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700116
Joey Armstrongb085c502023-01-17 13:56:24 -0500117## -----------------------------------------------------------------------
118## -----------------------------------------------------------------------
Kent Hagerman3da1fd02020-02-26 10:54:07 -0500119lint: lint-mod
Scott Baker2c1c4822019-10-16 11:02:41 -0700120
Joey Armstrongb085c502023-01-17 13:56:24 -0500121## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400122## Coverage report: Static code analysis
Joey Armstrongb085c502023-01-17 13:56:24 -0500123## -----------------------------------------------------------------------
Kent Hagermanfac11d42020-01-28 12:18:55 -0500124sca:
Joey Armstrong26245a32022-12-17 21:49:06 -0500125 @$(RM) -r ./sca-report
Scott Baker2c1c4822019-10-16 11:02:41 -0700126 @mkdir -p ./sca-report
Kent Hagerman3da1fd02020-02-26 10:54:07 -0500127 @echo "Running static code analysis..."
Abhay Kumar40252eb2025-10-13 13:25:53 +0000128 @${GOLANGCI_LINT} run --output.text.path=stdout --output.junit-xml.path=./sca-report/sca-report.xml ./...
129 @cat ./sca-report/sca-report.xml
Kent Hagerman3da1fd02020-02-26 10:54:07 -0500130 @echo ""
131 @echo "Static code analysis OK"
Scott Baker2c1c4822019-10-16 11:02:41 -0700132
Joey Armstrongb085c502023-01-17 13:56:24 -0500133## -----------------------------------------------------------------------
134## -----------------------------------------------------------------------
Matteo Scandolod58eaef2020-03-30 12:30:02 -0700135test: local-protos
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400136
137 $(call banner-enter,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700138 @mkdir -p ./tests/results
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400139
140 # No stream redirects, exit with shell status
141 $(MAKE) test-go
142
143 # Redirect I/O, ignore shell exit status (for now)
144 $(MAKE) test-go-cover
145
146 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700147
Joey Armstrongb085c502023-01-17 13:56:24 -0500148## -----------------------------------------------------------------------
149## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400150.PHONY: test-go
151test-go :
152
153 $(call banner-enter,Target $@)
154 @echo "** Testing attempt #1: exit-with-error-status: enabled"
155 -$(GO) test -mod=vendor ./...
156 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700157
Joey Armstrongb085c502023-01-17 13:56:24 -0500158## -----------------------------------------------------------------------
159## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400160.PHONY: test-go-cover
Joey Armstrongfd77d5e2023-07-15 16:37:44 -0400161test-go-cover : gen-coverage-coverprofile gen-coverage-junit gen-coverage-cobertura
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400162
163## -----------------------------------------------------------------------
164## Intent: Generate coverprofile data
165## -----------------------------------------------------------------------
166cover-dir := ./tests/results
167go-cover-out := $(cover-dir)/go-test-coverage.out
168go-result-out := $(cover-dir)/go-test-results.out
169
170.PHONY: gen-coverage-coverprofile
171gen-coverage-coverprofile:
172
173 $(call banner-enter,Target $@)
174 @echo "** Testing attempt #2: exit-with-error-status: disabled"
175
176 # Fix docker volume perms if building locally
177 touch "$(go-cover-out)"
178 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-cover-out)")
179
180 # Fix docker volume perms if building locally
181 $(if $(LOCAL_FIX_PERMS),touch "$(go-result-out)")
182 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-result-out)")
183
Joey Armstrong7cd92a12024-05-01 17:47:58 -0400184 # ------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400185 # set -euo pipefail else tee masks $? return
186 # ------------------------------------------
187 @echo '** Running test coverage: exit-on-error is currently disabled'
188 -(\
189 set -euo pipefail; \
190 $(GO) test -mod=vendor -v -coverprofile "$(go-cover-out)" -covermode count ./... 2>&1 | tee "$(go-result-out)" \
Joey Armstrong7cd92a12024-05-01 17:47:58 -0400191 )
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400192
193 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-result-out)")
Joey Armstrongfd77d5e2023-07-15 16:37:44 -0400194 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-cover-out)")
195
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400196 $(call banner-leave,Target $@)
197
198## -----------------------------------------------------------------------
199## Intent: Morph coverage data into junit/xml content
200## -----------------------------------------------------------------------
201go-results-xml := $(cover-dir)/go-test-results.xml
202
203.PHONY: gen-coverage-junit
204gen-coverage-junit : gen-coverage-coverprofile
205gen-coverage-junit:
206 $(call banner-enter,Target $@)
207
208 # Fix docker volume perms if building locally
209 $(if $(LOCAL_FIX_PERMS),touch "$(go-results-xml)")
210 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-results-xml)")
211
212 ${GO_JUNIT_REPORT} < $(go-result-out) > "$(go-results-xml)"
213
214 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-results-xml)")
215 $(call banner-leave,Target $@)
216
217## -----------------------------------------------------------------------
218## Intent: Morph coverage data into cobertura xml
219## -----------------------------------------------------------------------
220go-cover-xml := $(cover-dir)/go-test-coverage.xml
221
222.PHONY: gen-coverage-cobertura
223gen-coverage-cobertura : gen-coverage-junit
224gen-coverage-cobertura :
225
226 $(call banner-enter,Target $@)
227
228 # Fix docker volume perms if building locally
229 $(if $(LOCAL_FIX_PERMS),touch "$(go-cover-xml)")
Joey Armstrongfd77d5e2023-07-15 16:37:44 -0400230 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-cover-xml)")
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400231
232 ${GOCOVER_COBERTURA} < "$(go-cover-out)" > "$(go-cover-xml)"
233
234 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-cover-xml)")
235 $(call banner-leave,Target $@)
236
237## -----------------------------------------------------------------------
238## -----------------------------------------------------------------------
239distclean:
Joey Armstrong26245a32022-12-17 21:49:06 -0500240 $(RM) -r ./sca-report ./tests
Scott Baker2c1c4822019-10-16 11:02:41 -0700241
Joey Armstrongb085c502023-01-17 13:56:24 -0500242## -----------------------------------------------------------------------
243## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400244clean :: distclean
245
246## -----------------------------------------------------------------------
247## -----------------------------------------------------------------------
248sterile :: clean
249
250## -----------------------------------------------------------------------
251## -----------------------------------------------------------------------
252help ::
253 @echo "Usage: make [<target>]"
254 @echo "where available targets are:"
255 @echo
256 @echo "build : Build the library"
257 @echo "clean : Remove files created by the build"
258 @echo "distclean : Remove build and testing artifacts and reports"
259 @echo "lint-mod : Verify the integrity of the 'mod' files"
260 @echo "mod-update : Update go.mod and the vendor directory"
261 @echo "test : Generate reports for all go tests"
262 @echo
263 @echo '[REPORT: coverage]'
264 @echo ' gen-coverage-coverprofile Generate profiling data'
265 @echo ' gen-coverage-junit Generate junit coverage report'
266 @echo ' gen-coverage-cobertura Generate cobertura report'
Joey Armstrong26245a32022-12-17 21:49:06 -0500267
268# [EOF]