blob: ffb5650c2456f4ebbc1912a7fc128ea9824183f5 [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
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 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..."
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500183 @${GOLANGCI_LINT} run --deadline=20m --out-format junit-xml ./... \
184 | tee ./sca-report/sca-report.xml
Kent Hagerman813cb902020-02-20 10:01:38 -0500185 @echo ""
186 @echo "Static code analysis OK"
David Bainbridge86971522019-09-26 22:09:39 +0000187
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500188## -----------------------------------------------------------------------
189## Intent: Evaluate test targets (docker required)
190## -----------------------------------------------------------------------
David Bainbridge86971522019-09-26 22:09:39 +0000191test:
Scott Baker2b0ad652019-08-21 14:57:07 -0700192 @mkdir -p ./tests/results
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500193 @$(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 -0700194 RETURN=$$? ;\
Kent Hagerman813cb902020-02-20 10:01:38 -0500195 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
196 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker2b0ad652019-08-21 14:57:07 -0700197 exit $$RETURN
Zack Williamse940c7a2019-08-21 14:25:39 -0700198
Joey Armstrongf863afb2023-03-29 12:08:38 -0400199## -----------------------------------------------------------------------
200## -----------------------------------------------------------------------
Zack Williamse940c7a2019-08-21 14:25:39 -0700201view-coverage:
David Bainbridge86971522019-09-26 22:09:39 +0000202 go tool cover -html ./tests/results/go-test-coverage.out
Zack Williamse940c7a2019-08-21 14:25:39 -0700203
Joey Armstrongf863afb2023-03-29 12:08:38 -0400204## -----------------------------------------------------------------------
205## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000206check: lint sca test
207
Joey Armstrongf863afb2023-03-29 12:08:38 -0400208## -----------------------------------------------------------------------
209## -----------------------------------------------------------------------
Joey Armstrong903c69d2024-02-01 19:46:39 -0500210.PHONY: mod-update
211mod-update: go-version mod-tidy mod-vendor
212
213## -----------------------------------------------------------------------
214## -----------------------------------------------------------------------
215.PHONY: go-version
216go-version :
217 $(call banner-enter,Target $@)
218 ${GO} version
219 $(call banner-leave,Target $@)
220
221## -----------------------------------------------------------------------
222## -----------------------------------------------------------------------
223.PHONY: mod-tidy
224mod-tidy :
225 $(call banner-enter,Target $@)
226 ${GO} mod tidy
227 $(call banner-leave,Target $@)
228
229## -----------------------------------------------------------------------
230## -----------------------------------------------------------------------
231.PHONY: mod-vendor
232mod-vendor : mod-tidy
233mod-vendor :
234 $(call banner-enter,Target $@)
235 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
236 ${GO} mod vendor
237 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
238 $(call banner-leave,Target $@)
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500239
240## ---------------------------------------------------------
241## ---------------------------------------------------------
Joey Armstrongf863afb2023-03-29 12:08:38 -0400242clean ::
243 $(RM) -r voltctl voltctl.cp sca-report
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500244
245## ---------------------------------------------------------
246## This belongs in a library makefile: makefiles/go/clean.mk
247## ---------------------------------------------------------
248go-clean-cache += -cache
Joey Armstrongf863afb2023-03-29 12:08:38 -0400249# go-clean-cache += -fuzzcache
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500250go-clean-cache += -modcache
251go-clean-cache += -testcache
252
253go-clean-args += -i # installed binaries
254go-clean-args += -r # recursive
255go-clean-args += -x # verbose removal
256
Joey Armstrong903c69d2024-02-01 19:46:39 -0500257sterile :: clean
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500258 $(GO) clean $(go-clean-cache)
259 $(GO) clean $(go-clean-args)
260
261## [SEE ALSO]
262## -----------------------------------------------------------------------
263## o https://dave.cheney.net/tag/gogc
264## -----------------------------------------------------------------------
265
266$(if $(DEBUG),$(warning LEAVE))
267
268# [EOF]