Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 1 | # -*- 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 | |
| 22 | TOP ?= . |
| 23 | MAKEDIR ?= $(TOP)/makefiles |
| 24 | |
| 25 | $(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s) |
| 26 | |
| 27 | ##--------------------## |
| 28 | ##---] INCLUDES [---## |
| 29 | ##--------------------## |
| 30 | include $(MAKEDIR)/include.mk |
| 31 | ifdef LOCAL_LINT |
| 32 | include $(MAKEDIR)/lint/golang/sca.mk |
| 33 | endif |
| 34 | |
| 35 | ## Are lint-style and lint-sanity targets defined in docker ? |
| 36 | help :: |
| 37 | @echo |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 38 | @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 Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 42 | @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 Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 45 | @echo "lint - run static code analysis" |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 46 | @echo "sca - Runs various SCA through golangci-lint tool" |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 47 | @echo "test - run unity tests" |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 48 | @echo "check - runs targets that should be run before a commit" |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 49 | @echo "clean - remove temporary and generated files" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 50 | |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 51 | SHELL=bash -e -o pipefail |
| 52 | |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 53 | VERSION=$(shell cat ./VERSION) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 54 | GITCOMMIT=$(shell git rev-parse HEAD) |
| 55 | ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 56 | GITDIRTY=false |
| 57 | else |
| 58 | GITDIRTY=true |
| 59 | endif |
| 60 | GOVERSION=$(shell go version 2>&1 | sed -E 's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g') |
| 61 | HOST_OS=$(shell uname -s | tr A-Z a-z) |
| 62 | ifeq ($(shell uname -m),x86_64) |
| 63 | HOST_ARCH ?= amd64 |
| 64 | else |
| 65 | HOST_ARCH ?= $(shell uname -m) |
| 66 | endif |
| 67 | BUILDTIME=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 68 | |
| 69 | LDFLAGS=-ldflags \ |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 70 | "-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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 77 | |
| 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 |
| 81 | RELEASE_DIR ?= release |
| 82 | RELEASE_NAME ?= voltctl |
Ciprian Barbu | 72bdf89 | 2020-01-29 13:42:48 +0200 | [diff] [blame] | 83 | RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64 |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 84 | |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 85 | # tool containers |
David K. Bainbridge | 3e83a5b | 2021-04-09 16:18:04 +0000 | [diff] [blame] | 86 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 87 | |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 88 | GO = 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 |
| 89 | GO_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 |
| 90 | GO_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 |
| 91 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 92 | GOLANGCI_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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 93 | |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 94 | ## ----------------------------------------------------------------------- |
| 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 | ## ----------------------------------------------------------------------- |
| 105 | ifdef LOCAL_DEV_MODE |
| 106 | GO := $(clean-env) go |
| 107 | GOLANGCI_LINT := golangci-lint |
| 108 | endif |
| 109 | |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 110 | release: |
| 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 Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 119 | |
divyadesai | d331731 | 2020-04-08 09:43:11 +0000 | [diff] [blame] | 120 | ## Local Development Helpers |
| 121 | local-lib-go: |
| 122 | ifdef LOCAL_LIB_GO |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 123 | 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/ |
divyadesai | d331731 | 2020-04-08 09:43:11 +0000 | [diff] [blame] | 126 | endif |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 127 | |
divyadesai | d331731 | 2020-04-08 09:43:11 +0000 | [diff] [blame] | 128 | build: local-lib-go |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 129 | go build -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 130 | |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 131 | install: |
| 132 | go install -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 133 | |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 134 | run: |
| 135 | go run -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go $(CMD) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 136 | |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 137 | lint-mod: |
Scott Baker | 4a35a70 | 2019-11-26 08:17:33 -0800 | [diff] [blame] | 138 | @echo "Running dependency check..." |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 139 | @$(GO) mod verify |
Scott Baker | 4a35a70 | 2019-11-26 08:17:33 -0800 | [diff] [blame] | 140 | @echo "Dependency check OK. Running vendor check..." |
| 141 | @git status > /dev/null |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 142 | @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 Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 144 | $(GO) mod tidy |
| 145 | $(GO) mod vendor |
Scott Baker | 4a35a70 | 2019-11-26 08:17:33 -0800 | [diff] [blame] | 146 | @git status > /dev/null |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 147 | @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 Baker | 4a35a70 | 2019-11-26 08:17:33 -0800 | [diff] [blame] | 149 | @echo "Vendor check OK." |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 150 | |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 151 | ifndef LOCAL_LINT |
| 152 | lint : lint-mod |
| 153 | endif |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 154 | |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 155 | ## ----------------------------------------------------------------------- |
| 156 | ## Intent: Syntax check golang source |
| 157 | ## See Also: makefilles/lint/golang/sca.mk |
| 158 | ## ----------------------------------------------------------------------- |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 159 | sca: |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 160 | @$(RM) -r ./sca-report |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 161 | @mkdir -p ./sca-report |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 162 | @echo "Running static code analysis..." |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 163 | @${GOLANGCI_LINT} run --deadline=20m --out-format junit-xml ./... \ |
| 164 | | tee ./sca-report/sca-report.xml |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 165 | @echo "" |
| 166 | @echo "Static code analysis OK" |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 167 | |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 168 | ## ----------------------------------------------------------------------- |
| 169 | ## Intent: Evaluate test targets (docker required) |
| 170 | ## ----------------------------------------------------------------------- |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 171 | test: |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 172 | @mkdir -p ./tests/results |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 173 | @$(GO) test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 174 | RETURN=$$? ;\ |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 175 | ${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 Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 177 | exit $$RETURN |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 178 | |
| 179 | view-coverage: |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 180 | go tool cover -html ./tests/results/go-test-coverage.out |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 181 | |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 182 | check: lint sca test |
| 183 | |
Kent Hagerman | 813cb90 | 2020-02-20 10:01:38 -0500 | [diff] [blame] | 184 | mod-update: |
Joey Armstrong | 9fc4fa8 | 2022-12-19 18:38:40 -0500 | [diff] [blame^] | 185 | $(GO) mod tidy |
| 186 | $(GO) mod vendor |
| 187 | |
| 188 | ## --------------------------------------------------------- |
| 189 | ## --------------------------------------------------------- |
| 190 | clean: |
| 191 | $(RM) -r voltctl voltctl.cp release sca-report |
| 192 | |
| 193 | ## --------------------------------------------------------- |
| 194 | ## This belongs in a library makefile: makefiles/go/clean.mk |
| 195 | ## --------------------------------------------------------- |
| 196 | go-clean-cache += -cache |
| 197 | go-clean-cache += -fuzzcache |
| 198 | go-clean-cache += -modcache |
| 199 | go-clean-cache += -testcache |
| 200 | |
| 201 | go-clean-args += -i # installed binaries |
| 202 | go-clean-args += -r # recursive |
| 203 | go-clean-args += -x # verbose removal |
| 204 | |
| 205 | sterile: 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] |