blob: d51c5c5d1c0a31c54c807e4575c7c007c534e157 [file] [log] [blame]
Joey Armstrongb085c502023-01-17 13:56:24 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2016-2023 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 Armstrong7f8436c2023-07-09 20:23:27 -040027include config.mk
28include makefiles/include.mk
Scott Baker2c1c4822019-10-16 11:02:41 -070029
30# Variables
31VERSION ?= $(shell cat ./VERSION)
32
Joey Armstrongb085c502023-01-17 13:56:24 -050033## -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070034## Local Development Helpers
Joey Armstrongb085c502023-01-17 13:56:24 -050035## -----------------------------------------------------------------------
36.PHONY: local-protos
Scott Baker2c1c4822019-10-16 11:02:41 -070037local-protos:
38 @mkdir -p python/local_imports
39ifdef LOCAL_PROTOS
Joey Armstrong26245a32022-12-17 21:49:06 -050040 $(RM) -r vendor/github.com/opencord/voltha-protos
khenaidoo26721882021-08-11 17:42:52 -040041 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
42 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
Joey Armstrong26245a32022-12-17 21:49:06 -050043 $(RM) -r python/local_imports/voltha-protos
Scott Baker2c1c4822019-10-16 11:02:41 -070044 mkdir -p python/local_imports/voltha-protos/dist
Scott Bakerf1b096c2019-11-01 12:36:30 -070045 cp ${LOCAL_PROTOS}/dist/*.tar.gz python/local_imports/voltha-protos/dist/
Scott Baker2c1c4822019-10-16 11:02:41 -070046endif
47
Joey Armstrongb085c502023-01-17 13:56:24 -050048## -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070049## build the library
Joey Armstrongb085c502023-01-17 13:56:24 -050050## -----------------------------------------------------------------------
Scott Bakerf1b096c2019-11-01 12:36:30 -070051build: local-protos
Joey Armstrong7f8436c2023-07-09 20:23:27 -040052 $(call banner-enter,Target $@)
Kent Hagermanfac11d42020-01-28 12:18:55 -050053 ${GO} build -mod=vendor ./...
Joey Armstrong7f8436c2023-07-09 20:23:27 -040054 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -070055
Joey Armstrongb085c502023-01-17 13:56:24 -050056## -----------------------------------------------------------------------
57## -----------------------------------------------------------------------
Scott Baker2c1c4822019-10-16 11:02:41 -070058lint-mod:
Joey Armstrong7f8436c2023-07-09 20:23:27 -040059 $(call banner-entry,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -070060 @echo "Running dependency check..."
Kent Hagermanfac11d42020-01-28 12:18:55 -050061 @${GO} mod verify
Scott Bakera36b4982019-11-26 08:09:23 -080062 @echo "Dependency check OK. Running vendor check..."
63 @git status > /dev/null
Kent Hagerman3da1fd02020-02-26 10:54:07 -050064 @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)
65 @[[ `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 -040066
67 $(HIDE)$(MAKE) --no-print-directory mod-update
68
Scott Bakera36b4982019-11-26 08:09:23 -080069 @git status > /dev/null
Kent Hagerman3da1fd02020-02-26 10:54:07 -050070 @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)
71 @[[ `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 -080072 @echo "Vendor check OK."
Joey Armstrong7f8436c2023-07-09 20:23:27 -040073 $(call banner-leave,Target $@)
74
75## -----------------------------------------------------------------------
76## -----------------------------------------------------------------------
77.PHONY: mod-update
78mod-update: mod-tidy mod-vendor
79
80## -----------------------------------------------------------------------
81## -----------------------------------------------------------------------
82.PHONY: mod-tidy
83mod-tidy :
84 $(call banner-enter,Target $@)
85 ${GO} mod tidy
86 $(call banner-leave,Target $@)
87
88## -----------------------------------------------------------------------
89## -----------------------------------------------------------------------
90.PHONY: mod-vendor
91mod-vendor : mod-tidy
92mod-vendor :
93 $(call banner-enter,Target $@)
94 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
95 ${GO} mod vendor
96 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
97 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -070098
Joey Armstrongb085c502023-01-17 13:56:24 -050099## -----------------------------------------------------------------------
100## -----------------------------------------------------------------------
Kent Hagerman3da1fd02020-02-26 10:54:07 -0500101lint: lint-mod
Scott Baker2c1c4822019-10-16 11:02:41 -0700102
Joey Armstrongb085c502023-01-17 13:56:24 -0500103## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400104## Coverage report: Static code analysis
Joey Armstrongb085c502023-01-17 13:56:24 -0500105## -----------------------------------------------------------------------
Kent Hagermanfac11d42020-01-28 12:18:55 -0500106sca:
Joey Armstrong26245a32022-12-17 21:49:06 -0500107 @$(RM) -r ./sca-report
Scott Baker2c1c4822019-10-16 11:02:41 -0700108 @mkdir -p ./sca-report
Kent Hagerman3da1fd02020-02-26 10:54:07 -0500109 @echo "Running static code analysis..."
110 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
111 @echo ""
112 @echo "Static code analysis OK"
Scott Baker2c1c4822019-10-16 11:02:41 -0700113
Joey Armstrongb085c502023-01-17 13:56:24 -0500114## -----------------------------------------------------------------------
115## -----------------------------------------------------------------------
Matteo Scandolod58eaef2020-03-30 12:30:02 -0700116test: local-protos
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400117
118 $(call banner-enter,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700119 @mkdir -p ./tests/results
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400120
121 # No stream redirects, exit with shell status
122 $(MAKE) test-go
123
124 # Redirect I/O, ignore shell exit status (for now)
125 $(MAKE) test-go-cover
126
127 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700128
Joey Armstrongb085c502023-01-17 13:56:24 -0500129## -----------------------------------------------------------------------
130## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400131.PHONY: test-go
132test-go :
133
134 $(call banner-enter,Target $@)
135 @echo "** Testing attempt #1: exit-with-error-status: enabled"
136 -$(GO) test -mod=vendor ./...
137 $(call banner-leave,Target $@)
Scott Baker2c1c4822019-10-16 11:02:41 -0700138
Joey Armstrongb085c502023-01-17 13:56:24 -0500139## -----------------------------------------------------------------------
140## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400141.PHONY: test-go-cover
142test-go : gen-coverage-coverprofile gen-coverage-junit gen-coverage-cobertura
143
144
145## -----------------------------------------------------------------------
146## Intent: Generate coverprofile data
147## -----------------------------------------------------------------------
148cover-dir := ./tests/results
149go-cover-out := $(cover-dir)/go-test-coverage.out
150go-result-out := $(cover-dir)/go-test-results.out
151
152.PHONY: gen-coverage-coverprofile
153gen-coverage-coverprofile:
154
155 $(call banner-enter,Target $@)
156 @echo "** Testing attempt #2: exit-with-error-status: disabled"
157
158 # Fix docker volume perms if building locally
159 touch "$(go-cover-out)"
160 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-cover-out)")
161
162 # Fix docker volume perms if building locally
163 $(if $(LOCAL_FIX_PERMS),touch "$(go-result-out)")
164 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-result-out)")
165
166 # ------------------------------------------
167 # set -euo pipefail else tee masks $? return
168 # ------------------------------------------
169 @echo '** Running test coverage: exit-on-error is currently disabled'
170 -(\
171 set -euo pipefail; \
172 $(GO) test -mod=vendor -v -coverprofile "$(go-cover-out)" -covermode count ./... 2>&1 | tee "$(go-result-out)" \
173 )
174
175 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-result-out)")
176 $(call banner-leave,Target $@)
177
178## -----------------------------------------------------------------------
179## Intent: Morph coverage data into junit/xml content
180## -----------------------------------------------------------------------
181go-results-xml := $(cover-dir)/go-test-results.xml
182
183.PHONY: gen-coverage-junit
184gen-coverage-junit : gen-coverage-coverprofile
185gen-coverage-junit:
186 $(call banner-enter,Target $@)
187
188 # Fix docker volume perms if building locally
189 $(if $(LOCAL_FIX_PERMS),touch "$(go-results-xml)")
190 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(go-results-xml)")
191
192 ${GO_JUNIT_REPORT} < $(go-result-out) > "$(go-results-xml)"
193
194 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-results-xml)")
195 $(call banner-leave,Target $@)
196
197## -----------------------------------------------------------------------
198## Intent: Morph coverage data into cobertura xml
199## -----------------------------------------------------------------------
200go-cover-xml := $(cover-dir)/go-test-coverage.xml
201
202.PHONY: gen-coverage-cobertura
203gen-coverage-cobertura : gen-coverage-junit
204gen-coverage-cobertura :
205
206 $(call banner-enter,Target $@)
207
208 # Fix docker volume perms if building locally
209 $(if $(LOCAL_FIX_PERMS),touch "$(go-cover-xml)")
210 $(if $(LOCAL_FIX_PERMS),chmod o+w "$(coverproifle-xml)")
211
212 ${GOCOVER_COBERTURA} < "$(go-cover-out)" > "$(go-cover-xml)"
213
214 $(if $(LOCAL_FIX_PERMS),chmod o-w "$(go-cover-xml)")
215 $(call banner-leave,Target $@)
216
217## -----------------------------------------------------------------------
218## -----------------------------------------------------------------------
219distclean:
Joey Armstrong26245a32022-12-17 21:49:06 -0500220 $(RM) -r ./sca-report ./tests
Scott Baker2c1c4822019-10-16 11:02:41 -0700221
Joey Armstrongb085c502023-01-17 13:56:24 -0500222## -----------------------------------------------------------------------
223## -----------------------------------------------------------------------
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400224clean :: distclean
225
226## -----------------------------------------------------------------------
227## -----------------------------------------------------------------------
228sterile :: clean
229
230## -----------------------------------------------------------------------
231## -----------------------------------------------------------------------
232help ::
233 @echo "Usage: make [<target>]"
234 @echo "where available targets are:"
235 @echo
236 @echo "build : Build the library"
237 @echo "clean : Remove files created by the build"
238 @echo "distclean : Remove build and testing artifacts and reports"
239 @echo "lint-mod : Verify the integrity of the 'mod' files"
240 @echo "mod-update : Update go.mod and the vendor directory"
241 @echo "test : Generate reports for all go tests"
242 @echo
243 @echo '[REPORT: coverage]'
244 @echo ' gen-coverage-coverprofile Generate profiling data'
245 @echo ' gen-coverage-junit Generate junit coverage report'
246 @echo ' gen-coverage-cobertura Generate cobertura report'
Joey Armstrong26245a32022-12-17 21:49:06 -0500247
248# [EOF]
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400249