blob: 32e92835668cf2c1b50d4307186b102d8f6c0fb3 [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
25$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
26
27##--------------------##
28##---] INCLUDES [---##
29##--------------------##
30include $(MAKEDIR)/include.mk
31ifdef LOCAL_LINT
32 include $(MAKEDIR)/lint/golang/sca.mk
33endif
34
35## Are lint-style and lint-sanity targets defined in docker ?
36help ::
37 @echo
David Bainbridgec4029aa2019-09-26 18:56:39 +000038 @echo "release - build binaries using cross compliing for the support architectures"
39 @echo "build - build the binary as a local executable"
40 @echo "install - build and install the binary into \$$GOPATH/bin"
41 @echo "run - runs voltctl using the command specified as \$$CMD"
David Bainbridge12f036f2019-10-15 22:09:04 +000042 @echo "lint-style - Verify code is properly gofmt-ed"
43 @echo "lint-sanity - Verify that 'go vet' doesn't report any issues"
44 @echo "lint-mod - Verify the integrity of the 'mod' files"
David Bainbridge86971522019-09-26 22:09:39 +000045 @echo "lint - run static code analysis"
David Bainbridge12f036f2019-10-15 22:09:04 +000046 @echo "sca - Runs various SCA through golangci-lint tool"
David Bainbridge86971522019-09-26 22:09:39 +000047 @echo "test - run unity tests"
David Bainbridge12f036f2019-10-15 22:09:04 +000048 @echo "check - runs targets that should be run before a commit"
David Bainbridgec4029aa2019-09-26 18:56:39 +000049 @echo "clean - remove temporary and generated files"
Zack Williamse940c7a2019-08-21 14:25:39 -070050
David Bainbridge12f036f2019-10-15 22:09:04 +000051SHELL=bash -e -o pipefail
52
David Bainbridge86971522019-09-26 22:09:39 +000053VERSION=$(shell cat ./VERSION)
Zack Williamse940c7a2019-08-21 14:25:39 -070054GITCOMMIT=$(shell git rev-parse HEAD)
55ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
56GITDIRTY=false
57else
58GITDIRTY=true
59endif
60GOVERSION=$(shell go version 2>&1 | sed -E 's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
61HOST_OS=$(shell uname -s | tr A-Z a-z)
62ifeq ($(shell uname -m),x86_64)
63 HOST_ARCH ?= amd64
64else
65 HOST_ARCH ?= $(shell uname -m)
66endif
67BUILDTIME=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
68
69LDFLAGS=-ldflags \
Kent Hagerman813cb902020-02-20 10:01:38 -050070 "-X \"github.com/opencord/voltctl/internal/pkg/cli/version.Version=$(VERSION)\" \
71 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.VcsRef=$(GITCOMMIT)\" \
72 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.VcsDirty=$(GITDIRTY)\" \
73 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.GoVersion=$(GOVERSION)\" \
74 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.Os=$(HOST_OS)\" \
75 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.Arch=$(HOST_ARCH)\" \
76 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.BuildTime=$(BUILDTIME)\""
Zack Williamse940c7a2019-08-21 14:25:39 -070077
78# Release related items
79# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
80# Inspired by: https://github.com/kubernetes/minikube/releases
81RELEASE_DIR ?= release
82RELEASE_NAME ?= voltctl
Ciprian Barbu72bdf892020-01-29 13:42:48 +020083RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64
Zack Williamse940c7a2019-08-21 14:25:39 -070084
Kent Hagerman813cb902020-02-20 10:01:38 -050085# tool containers
David K. Bainbridge3e83a5b2021-04-09 16:18:04 +000086VOLTHA_TOOLS_VERSION ?= 2.4.0
Zack Williamse940c7a2019-08-21 14:25:39 -070087
Kent Hagerman813cb902020-02-20 10:01:38 -050088GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/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
89GO_SH = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/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
90GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/appecho -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
91GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
92GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/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 -070093
Joey Armstrong9fc4fa82022-12-19 18:38:40 -050094## -----------------------------------------------------------------------
95## Why is docker an implicit dependency for "make lint" (?)
96## o A fixed version is required for jenkins build/release jobs.
97## o Devs should have the option of using whatever is available
98## including bleeding edge software and tool upgrades w/o overhead.
99## -----------------------------------------------------------------------
100## Usage:
101## % export LOCAL_DEV_MODE=1
102## % make lint
103## % make check
104## -----------------------------------------------------------------------
105ifdef LOCAL_DEV_MODE
106 GO := $(clean-env) go
107 GOLANGCI_LINT := golangci-lint
108endif
109
Kent Hagerman813cb902020-02-20 10:01:38 -0500110release:
111 @mkdir -p $(RELEASE_DIR)
112 @${GO_SH} ' \
113 set -e -o pipefail; \
114 for x in ${RELEASE_OS_ARCH}; do \
115 OUT_PATH="$(RELEASE_DIR)/$(RELEASE_NAME)-$(subst -dev,_dev,$(VERSION))-$$x"; \
116 echo "$$OUT_PATH"; \
117 GOOS=$${x%-*} GOARCH=$${x#*-} go build -mod=vendor -v $(LDFLAGS) -o "$$OUT_PATH" cmd/voltctl/voltctl.go; \
118 done'
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500119
divyadesaid3317312020-04-08 09:43:11 +0000120## Local Development Helpers
121local-lib-go:
122ifdef LOCAL_LIB_GO
David K. Bainbridgebd6b2882021-08-26 13:31:02 +0000123 rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg
124 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
125 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
divyadesaid3317312020-04-08 09:43:11 +0000126endif
Zack Williamse940c7a2019-08-21 14:25:39 -0700127
divyadesaid3317312020-04-08 09:43:11 +0000128build: local-lib-go
David Bainbridge86971522019-09-26 22:09:39 +0000129 go build -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go
Zack Williamse940c7a2019-08-21 14:25:39 -0700130
David Bainbridge86971522019-09-26 22:09:39 +0000131install:
132 go install -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go
Zack Williamse940c7a2019-08-21 14:25:39 -0700133
David Bainbridge86971522019-09-26 22:09:39 +0000134run:
135 go run -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go $(CMD)
Zack Williamse940c7a2019-08-21 14:25:39 -0700136
David Bainbridge12f036f2019-10-15 22:09:04 +0000137lint-mod:
Scott Baker4a35a702019-11-26 08:17:33 -0800138 @echo "Running dependency check..."
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500139 @$(GO) mod verify
Scott Baker4a35a702019-11-26 08:17:33 -0800140 @echo "Dependency check OK. Running vendor check..."
141 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500142 @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)
143 @[[ `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 -0500144 $(GO) mod tidy
145 $(GO) mod vendor
Scott Baker4a35a702019-11-26 08:17:33 -0800146 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500147 @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)
148 @[[ `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 -0800149 @echo "Vendor check OK."
David Bainbridge12f036f2019-10-15 22:09:04 +0000150
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500151ifndef LOCAL_LINT
152 lint : lint-mod
153endif
David Bainbridge12f036f2019-10-15 22:09:04 +0000154
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500155## -----------------------------------------------------------------------
156## Intent: Syntax check golang source
157## See Also: makefilles/lint/golang/sca.mk
158## -----------------------------------------------------------------------
David Bainbridge12f036f2019-10-15 22:09:04 +0000159sca:
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500160 @$(RM) -r ./sca-report
David Bainbridge12f036f2019-10-15 22:09:04 +0000161 @mkdir -p ./sca-report
Kent Hagerman813cb902020-02-20 10:01:38 -0500162 @echo "Running static code analysis..."
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500163 @${GOLANGCI_LINT} run --deadline=20m --out-format junit-xml ./... \
164 | tee ./sca-report/sca-report.xml
Kent Hagerman813cb902020-02-20 10:01:38 -0500165 @echo ""
166 @echo "Static code analysis OK"
David Bainbridge86971522019-09-26 22:09:39 +0000167
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500168## -----------------------------------------------------------------------
169## Intent: Evaluate test targets (docker required)
170## -----------------------------------------------------------------------
David Bainbridge86971522019-09-26 22:09:39 +0000171test:
Scott Baker2b0ad652019-08-21 14:57:07 -0700172 @mkdir -p ./tests/results
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500173 @$(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 -0700174 RETURN=$$? ;\
Kent Hagerman813cb902020-02-20 10:01:38 -0500175 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
176 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker2b0ad652019-08-21 14:57:07 -0700177 exit $$RETURN
Zack Williamse940c7a2019-08-21 14:25:39 -0700178
179view-coverage:
David Bainbridge86971522019-09-26 22:09:39 +0000180 go tool cover -html ./tests/results/go-test-coverage.out
Zack Williamse940c7a2019-08-21 14:25:39 -0700181
David Bainbridge12f036f2019-10-15 22:09:04 +0000182check: lint sca test
183
Kent Hagerman813cb902020-02-20 10:01:38 -0500184mod-update:
Joey Armstrong9fc4fa82022-12-19 18:38:40 -0500185 $(GO) mod tidy
186 $(GO) mod vendor
187
188## ---------------------------------------------------------
189## ---------------------------------------------------------
190clean:
191 $(RM) -r voltctl voltctl.cp release sca-report
192
193## ---------------------------------------------------------
194## This belongs in a library makefile: makefiles/go/clean.mk
195## ---------------------------------------------------------
196go-clean-cache += -cache
197go-clean-cache += -fuzzcache
198go-clean-cache += -modcache
199go-clean-cache += -testcache
200
201go-clean-args += -i # installed binaries
202go-clean-args += -r # recursive
203go-clean-args += -x # verbose removal
204
205sterile: clean
206 $(GO) clean $(go-clean-cache)
207 $(GO) clean $(go-clean-args)
208
209## [SEE ALSO]
210## -----------------------------------------------------------------------
211## o https://dave.cheney.net/tag/gogc
212## -----------------------------------------------------------------------
213
214$(if $(DEBUG),$(warning LEAVE))
215
216# [EOF]