blob: a8c4483b77975176b1cca53b43fa4eeab8632b51 [file] [log] [blame]
Joey Armstrong9fc4fa82022-12-19 18:38:40 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2019-2023 Open Networking Foundation
4#
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
David K. Bainbridge3e83a5b2021-04-09 16:18:04 +000089VOLTHA_TOOLS_VERSION ?= 2.4.0
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 Armstrong9fc4fa82022-12-19 18:38:40 -0500159 $(GO) mod tidy
160 $(GO) mod vendor
Scott Baker4a35a702019-11-26 08:17:33 -0800161 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500162 @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)
163 @[[ `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 -0800164 @echo "Vendor check OK."
David Bainbridge12f036f2019-10-15 22:09:04 +0000165
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500166ifndef LOCAL_LINT
167 lint : lint-mod
168endif
David Bainbridge12f036f2019-10-15 22:09:04 +0000169
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500170## -----------------------------------------------------------------------
171## Intent: Syntax check golang source
172## See Also: makefilles/lint/golang/sca.mk
173## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000174sca:
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500175 @$(RM) -r ./sca-report
David Bainbridge12f036f2019-10-15 22:09:04 +0000176 @mkdir -p ./sca-report
Kent Hagerman813cb902020-02-20 10:01:38 -0500177 @echo "Running static code analysis..."
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500178 @${GOLANGCI_LINT} run --deadline=20m --out-format junit-xml ./... \
179 | tee ./sca-report/sca-report.xml
Kent Hagerman813cb902020-02-20 10:01:38 -0500180 @echo ""
181 @echo "Static code analysis OK"
David Bainbridge86971522019-09-26 22:09:39 +0000182
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500183## -----------------------------------------------------------------------
184## Intent: Evaluate test targets (docker required)
185## -----------------------------------------------------------------------
David Bainbridge86971522019-09-26 22:09:39 +0000186test:
Scott Baker2b0ad652019-08-21 14:57:07 -0700187 @mkdir -p ./tests/results
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500188 @$(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 -0700189 RETURN=$$? ;\
Kent Hagerman813cb902020-02-20 10:01:38 -0500190 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
191 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker2b0ad652019-08-21 14:57:07 -0700192 exit $$RETURN
Zack Williamse940c7a2019-08-21 14:25:39 -0700193
Joey Armstrongf863afb2023-03-29 12:08:38 -0400194## -----------------------------------------------------------------------
195## -----------------------------------------------------------------------
Zack Williamse940c7a2019-08-21 14:25:39 -0700196view-coverage:
David Bainbridge86971522019-09-26 22:09:39 +0000197 go tool cover -html ./tests/results/go-test-coverage.out
Zack Williamse940c7a2019-08-21 14:25:39 -0700198
Joey Armstrongf863afb2023-03-29 12:08:38 -0400199## -----------------------------------------------------------------------
200## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000201check: lint sca test
202
Joey Armstrongf863afb2023-03-29 12:08:38 -0400203## -----------------------------------------------------------------------
204## -----------------------------------------------------------------------
Kent Hagerman813cb902020-02-20 10:01:38 -0500205mod-update:
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500206 $(GO) mod tidy
207 $(GO) mod vendor
208
209## ---------------------------------------------------------
210## ---------------------------------------------------------
Joey Armstrongf863afb2023-03-29 12:08:38 -0400211clean ::
212 $(RM) -r voltctl voltctl.cp sca-report
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500213
214## ---------------------------------------------------------
215## This belongs in a library makefile: makefiles/go/clean.mk
216## ---------------------------------------------------------
217go-clean-cache += -cache
Joey Armstrongf863afb2023-03-29 12:08:38 -0400218# go-clean-cache += -fuzzcache
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500219go-clean-cache += -modcache
220go-clean-cache += -testcache
221
222go-clean-args += -i # installed binaries
223go-clean-args += -r # recursive
224go-clean-args += -x # verbose removal
225
226sterile: clean
227 $(GO) clean $(go-clean-cache)
228 $(GO) clean $(go-clean-args)
229
230## [SEE ALSO]
231## -----------------------------------------------------------------------
232## o https://dave.cheney.net/tag/gogc
233## -----------------------------------------------------------------------
234
235$(if $(DEBUG),$(warning LEAVE))
236
237# [EOF]