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