blob: ca24c5dd2a95290ae4bcc8ccd4c6f6bc5a0cf003 [file] [log] [blame]
Joey Armstrong9fc4fa82022-12-19 18:38:40 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
Joey Armstrong903c69d2024-02-01 19:46:39 -05003# Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Joey Armstrong9fc4fa82022-12-19 18:38:40 -05004#
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.
16# -----------------------------------------------------------------------
17
18$(if $(DEBUG),$(warning ENTER))
19
20.DEFAULT_GOAL := help
21
22TOP ?= .
23MAKEDIR ?= $(TOP)/makefiles
24
Joey Armstrongf863afb2023-03-29 12:08:38 -040025quoted = $(quote-single)$(1)$(quote-single)
26
Joey Armstrong9fc4fa82022-12-19 18:38:40 -050027$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
28
29##--------------------##
30##---] INCLUDES [---##
31##--------------------##
32include $(MAKEDIR)/include.mk
Joey Armstrongf863afb2023-03-29 12:08:38 -040033include $(MAKEDIR)/release/include.mk
34
Joey Armstrong9fc4fa82022-12-19 18:38:40 -050035ifdef LOCAL_LINT
36 include $(MAKEDIR)/lint/golang/sca.mk
37endif
38
39## Are lint-style and lint-sanity targets defined in docker ?
40help ::
41 @echo
David Bainbridgec4029aa2019-09-26 18:56:39 +000042 @echo "build - build the binary as a local executable"
43 @echo "install - build and install the binary into \$$GOPATH/bin"
44 @echo "run - runs voltctl using the command specified as \$$CMD"
David Bainbridge12f036f2019-10-15 22:09:04 +000045 @echo "lint-style - Verify code is properly gofmt-ed"
46 @echo "lint-sanity - Verify that 'go vet' doesn't report any issues"
47 @echo "lint-mod - Verify the integrity of the 'mod' files"
David Bainbridge86971522019-09-26 22:09:39 +000048 @echo "lint - run static code analysis"
David Bainbridge12f036f2019-10-15 22:09:04 +000049 @echo "sca - Runs various SCA through golangci-lint tool"
David Bainbridge86971522019-09-26 22:09:39 +000050 @echo "test - run unity tests"
David Bainbridge12f036f2019-10-15 22:09:04 +000051 @echo "check - runs targets that should be run before a commit"
David Bainbridgec4029aa2019-09-26 18:56:39 +000052 @echo "clean - remove temporary and generated files"
Zack Williamse940c7a2019-08-21 14:25:39 -070053
Joey Armstrongf863afb2023-03-29 12:08:38 -040054# SHELL=bash -e -o pipefail
David Bainbridge12f036f2019-10-15 22:09:04 +000055
David Bainbridge86971522019-09-26 22:09:39 +000056VERSION=$(shell cat ./VERSION)
Zack Williamse940c7a2019-08-21 14:25:39 -070057GITCOMMIT=$(shell git rev-parse HEAD)
58ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
59GITDIRTY=false
60else
61GITDIRTY=true
62endif
63GOVERSION=$(shell go version 2>&1 | sed -E 's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
64HOST_OS=$(shell uname -s | tr A-Z a-z)
65ifeq ($(shell uname -m),x86_64)
66 HOST_ARCH ?= amd64
67else
68 HOST_ARCH ?= $(shell uname -m)
69endif
70BUILDTIME=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
71
72LDFLAGS=-ldflags \
Kent Hagerman813cb902020-02-20 10:01:38 -050073 "-X \"github.com/opencord/voltctl/internal/pkg/cli/version.Version=$(VERSION)\" \
74 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.VcsRef=$(GITCOMMIT)\" \
75 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.VcsDirty=$(GITDIRTY)\" \
76 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.GoVersion=$(GOVERSION)\" \
77 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.Os=$(HOST_OS)\" \
78 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.Arch=$(HOST_ARCH)\" \
79 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.BuildTime=$(BUILDTIME)\""
Zack Williamse940c7a2019-08-21 14:25:39 -070080
81# Release related items
82# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
83# Inspired by: https://github.com/kubernetes/minikube/releases
84RELEASE_DIR ?= release
85RELEASE_NAME ?= voltctl
Ciprian Barbu72bdf892020-01-29 13:42:48 +020086RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64
Zack Williamse940c7a2019-08-21 14:25:39 -070087
Kent Hagerman813cb902020-02-20 10:01:38 -050088# tool containers
balaji.nagarajan8a2a7ee2026-06-19 22:31:13 +053089VOLTHA_TOOLS_VERSION ?= 3.2.2
Zack Williamse940c7a2019-08-21 14:25:39 -070090
Joey Armstrongf863afb2023-03-29 12:08:38 -040091docker-run = docker run --rm --user $$(id -u):$$(id -g)# # Docker command stem
92docker-run-app = $(docker-run) -v ${CURDIR}:/app# # w/filesystem mount
93
94GO = $(docker-run-app) $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
95GO_SH = $(docker-run-app) $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang sh -c
96GO_JUNIT_REPORT = $(docker-run) -v ${CURDIR}:/appecho -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
97GOCOVER_COBERTURA = $(docker-run-app) -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
98GOLANGCI_LINT = $(docker-run-app) $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg -e GOGC=10 voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
Zack Williamse940c7a2019-08-21 14:25:39 -070099
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500100## -----------------------------------------------------------------------
101## Why is docker an implicit dependency for "make lint" (?)
102## o A fixed version is required for jenkins build/release jobs.
103## o Devs should have the option of using whatever is available
104## including bleeding edge software and tool upgrades w/o overhead.
105## -----------------------------------------------------------------------
106## Usage:
107## % export LOCAL_DEV_MODE=1
108## % make lint
109## % make check
110## -----------------------------------------------------------------------
111ifdef LOCAL_DEV_MODE
112 GO := $(clean-env) go
113 GOLANGCI_LINT := golangci-lint
114endif
115
Joey Armstrongf863afb2023-03-29 12:08:38 -0400116## -----------------------------------------------------------------------
117## Intent: Cross-compile binaries for release
118## -----------------------------------------------------------------------
119release: release-build
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500120
Joey Armstrongf863afb2023-03-29 12:08:38 -0400121## -----------------------------------------------------------------------
divyadesaid3317312020-04-08 09:43:11 +0000122## Local Development Helpers
Joey Armstrongf863afb2023-03-29 12:08:38 -0400123## -----------------------------------------------------------------------
divyadesaid3317312020-04-08 09:43:11 +0000124local-lib-go:
125ifdef LOCAL_LIB_GO
Joey Armstrongf863afb2023-03-29 12:08:38 -0400126 $(RM) -r vendor/github.com/opencord/voltha-lib-go/v7/pkg
David K. Bainbridgebd6b2882021-08-26 13:31:02 +0000127 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
128 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
divyadesaid3317312020-04-08 09:43:11 +0000129endif
Zack Williamse940c7a2019-08-21 14:25:39 -0700130
Joey Armstrongf863afb2023-03-29 12:08:38 -0400131## -----------------------------------------------------------------------
132## Itent:
133## -----------------------------------------------------------------------
divyadesaid3317312020-04-08 09:43:11 +0000134build: local-lib-go
David Bainbridge86971522019-09-26 22:09:39 +0000135 go build -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go
Zack Williamse940c7a2019-08-21 14:25:39 -0700136
Joey Armstrongf863afb2023-03-29 12:08:38 -0400137## -----------------------------------------------------------------------
138## Itent:
139## -----------------------------------------------------------------------
David Bainbridge86971522019-09-26 22:09:39 +0000140install:
141 go install -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go
Zack Williamse940c7a2019-08-21 14:25:39 -0700142
Joey Armstrongf863afb2023-03-29 12:08:38 -0400143## -----------------------------------------------------------------------
144## Itent:
145## -----------------------------------------------------------------------
David Bainbridge86971522019-09-26 22:09:39 +0000146run:
147 go run -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go $(CMD)
Zack Williamse940c7a2019-08-21 14:25:39 -0700148
Joey Armstrongf863afb2023-03-29 12:08:38 -0400149## -----------------------------------------------------------------------
150## Itent:
151## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000152lint-mod:
Scott Baker4a35a702019-11-26 08:17:33 -0800153 @echo "Running dependency check..."
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500154 @$(GO) mod verify
Scott Baker4a35a702019-11-26 08:17:33 -0800155 @echo "Dependency check OK. Running vendor check..."
156 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500157 @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)
158 @[[ `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 Armstrong903c69d2024-02-01 19:46:39 -0500159
160 $(HIDE)$(MAKE) --no-print-directory mod-update
161
Scott Baker4a35a702019-11-26 08:17:33 -0800162 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500163 @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)
164 @[[ `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 Baker4a35a702019-11-26 08:17:33 -0800165 @echo "Vendor check OK."
David Bainbridge12f036f2019-10-15 22:09:04 +0000166
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500167ifndef LOCAL_LINT
168 lint : lint-mod
169endif
David Bainbridge12f036f2019-10-15 22:09:04 +0000170
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500171## -----------------------------------------------------------------------
Joey Armstrong903c69d2024-02-01 19:46:39 -0500172## -----------------------------------------------------------------------
173# lint: lint-mod lint-dockerfile ## Run all lint targets
174
175## -----------------------------------------------------------------------
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500176## Intent: Syntax check golang source
177## See Also: makefilles/lint/golang/sca.mk
178## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000179sca:
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500180 @$(RM) -r ./sca-report
David Bainbridge12f036f2019-10-15 22:09:04 +0000181 @mkdir -p ./sca-report
Kent Hagerman813cb902020-02-20 10:01:38 -0500182 @echo "Running static code analysis..."
balaji.nagarajan8a2a7ee2026-06-19 22:31:13 +0530183 @${GOLANGCI_LINT} run --output.text.path=stdout --output.junit-xml.path=./sca-report/sca-report.xml ./...
Kent Hagerman813cb902020-02-20 10:01:38 -0500184 @echo ""
185 @echo "Static code analysis OK"
David Bainbridge86971522019-09-26 22:09:39 +0000186
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500187## -----------------------------------------------------------------------
188## Intent: Evaluate test targets (docker required)
189## -----------------------------------------------------------------------
David Bainbridge86971522019-09-26 22:09:39 +0000190test:
Scott Baker2b0ad652019-08-21 14:57:07 -0700191 @mkdir -p ./tests/results
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500192 @$(GO) test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Scott Baker2b0ad652019-08-21 14:57:07 -0700193 RETURN=$$? ;\
Kent Hagerman813cb902020-02-20 10:01:38 -0500194 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
195 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker2b0ad652019-08-21 14:57:07 -0700196 exit $$RETURN
Zack Williamse940c7a2019-08-21 14:25:39 -0700197
Joey Armstrongf863afb2023-03-29 12:08:38 -0400198## -----------------------------------------------------------------------
199## -----------------------------------------------------------------------
Zack Williamse940c7a2019-08-21 14:25:39 -0700200view-coverage:
David Bainbridge86971522019-09-26 22:09:39 +0000201 go tool cover -html ./tests/results/go-test-coverage.out
Zack Williamse940c7a2019-08-21 14:25:39 -0700202
Joey Armstrongf863afb2023-03-29 12:08:38 -0400203## -----------------------------------------------------------------------
204## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000205check: lint sca test
206
Joey Armstrongf863afb2023-03-29 12:08:38 -0400207## -----------------------------------------------------------------------
208## -----------------------------------------------------------------------
Joey Armstrong903c69d2024-02-01 19:46:39 -0500209.PHONY: mod-update
210mod-update: go-version mod-tidy mod-vendor
211
212## -----------------------------------------------------------------------
213## -----------------------------------------------------------------------
214.PHONY: go-version
215go-version :
216 $(call banner-enter,Target $@)
217 ${GO} version
218 $(call banner-leave,Target $@)
219
220## -----------------------------------------------------------------------
221## -----------------------------------------------------------------------
222.PHONY: mod-tidy
223mod-tidy :
224 $(call banner-enter,Target $@)
225 ${GO} mod tidy
226 $(call banner-leave,Target $@)
227
228## -----------------------------------------------------------------------
229## -----------------------------------------------------------------------
230.PHONY: mod-vendor
231mod-vendor : mod-tidy
232mod-vendor :
233 $(call banner-enter,Target $@)
234 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
235 ${GO} mod vendor
236 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
237 $(call banner-leave,Target $@)
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500238
239## ---------------------------------------------------------
240## ---------------------------------------------------------
Joey Armstrongf863afb2023-03-29 12:08:38 -0400241clean ::
242 $(RM) -r voltctl voltctl.cp sca-report
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500243
244## ---------------------------------------------------------
245## This belongs in a library makefile: makefiles/go/clean.mk
246## ---------------------------------------------------------
247go-clean-cache += -cache
Joey Armstrongf863afb2023-03-29 12:08:38 -0400248# go-clean-cache += -fuzzcache
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500249go-clean-cache += -modcache
250go-clean-cache += -testcache
251
252go-clean-args += -i # installed binaries
253go-clean-args += -r # recursive
254go-clean-args += -x # verbose removal
255
Joey Armstrong903c69d2024-02-01 19:46:39 -0500256sterile :: clean
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500257 $(GO) clean $(go-clean-cache)
258 $(GO) clean $(go-clean-args)
259
260## [SEE ALSO]
261## -----------------------------------------------------------------------
262## o https://dave.cheney.net/tag/gogc
263## -----------------------------------------------------------------------
264
265$(if $(DEBUG),$(warning LEAVE))
266
267# [EOF]