blob: f53bb548316cf97a18d4b09163c4d6db7d0073f2 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001help:
David Bainbridgec4029aa2019-09-26 18:56:39 +00002 @echo "release - build binaries using cross compliing for the support architectures"
3 @echo "build - build the binary as a local executable"
4 @echo "install - build and install the binary into \$$GOPATH/bin"
5 @echo "run - runs voltctl using the command specified as \$$CMD"
David Bainbridge12f036f2019-10-15 22:09:04 +00006 @echo "lint-style - Verify code is properly gofmt-ed"
7 @echo "lint-sanity - Verify that 'go vet' doesn't report any issues"
8 @echo "lint-mod - Verify the integrity of the 'mod' files"
David Bainbridge86971522019-09-26 22:09:39 +00009 @echo "lint - run static code analysis"
David Bainbridge12f036f2019-10-15 22:09:04 +000010 @echo "sca - Runs various SCA through golangci-lint tool"
David Bainbridge86971522019-09-26 22:09:39 +000011 @echo "test - run unity tests"
David Bainbridge12f036f2019-10-15 22:09:04 +000012 @echo "check - runs targets that should be run before a commit"
David Bainbridgec4029aa2019-09-26 18:56:39 +000013 @echo "clean - remove temporary and generated files"
Zack Williamse940c7a2019-08-21 14:25:39 -070014
kesavand12cd8eb2020-01-20 22:25:22 -050015internal/pkg/commands/%_pb.go: assets/protosets/%.pb
Scott Baker2b0ad652019-08-21 14:57:07 -070016 @echo "/*" > $@
17 @echo " * Copyright 2019-present Open Networking Foundation" >> $@
18 @echo " *" >> $@
19 @echo " * Licensed under the Apache License, Version 2.0 (the "License");" >> $@
20 @echo " * you may not use this file except in compliance with the License." >> $@
21 @echo " * You may obtain a copy of the License at" >> $@
22 @echo " *" >> $@
23 @echo " * http://www.apache.org/licenses/LICENSE-2.0" >> $@
24 @echo " *" >> $@
25 @echo " * Unless required by applicable law or agreed to in writing, software" >> $@
26 @echo " * distributed under the License is distributed on an "AS IS" BASIS," >> $@
27 @echo " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." >> $@
28 @echo " * See the License for the specific language governing permissions and" >> $@
29 @echo " * limitations under the License." >> $@
30 @echo " */" >> $@
31 @echo "package commands" >> $@
Zack Williamse940c7a2019-08-21 14:25:39 -070032 @echo "" >> $@
kesavand12cd8eb2020-01-20 22:25:22 -050033 @echo "var $(shell echo $(subst .pb,,$(subst assets/protosets/voltha_,,$<)) |tr '[:lower:]' '[:upper:]')Descriptor = []byte{" >> $@
34 hexdump -ve '1/1 "0x%02x,"' $< | fold -w 60 -s >> $@
Zack Williamse940c7a2019-08-21 14:25:39 -070035 @echo "}" >> $@
36 @go fmt $@
37
kesavand12cd8eb2020-01-20 22:25:22 -050038encode-protosets: internal/pkg/commands/voltha_v1_pb.go internal/pkg/commands/voltha_v2_pb.go internal/pkg/commands/voltha_v3_pb.go
Zack Williamse940c7a2019-08-21 14:25:39 -070039
David Bainbridge12f036f2019-10-15 22:09:04 +000040SHELL=bash -e -o pipefail
41
David Bainbridge86971522019-09-26 22:09:39 +000042VERSION=$(shell cat ./VERSION)
Zack Williamse940c7a2019-08-21 14:25:39 -070043GITCOMMIT=$(shell git rev-parse HEAD)
44ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
45GITDIRTY=false
46else
47GITDIRTY=true
48endif
49GOVERSION=$(shell go version 2>&1 | sed -E 's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
50HOST_OS=$(shell uname -s | tr A-Z a-z)
51ifeq ($(shell uname -m),x86_64)
52 HOST_ARCH ?= amd64
53else
54 HOST_ARCH ?= $(shell uname -m)
55endif
56BUILDTIME=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
57
58LDFLAGS=-ldflags \
Kent Hagerman813cb902020-02-20 10:01:38 -050059 "-X \"github.com/opencord/voltctl/internal/pkg/cli/version.Version=$(VERSION)\" \
60 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.VcsRef=$(GITCOMMIT)\" \
61 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.VcsDirty=$(GITDIRTY)\" \
62 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.GoVersion=$(GOVERSION)\" \
63 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.Os=$(HOST_OS)\" \
64 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.Arch=$(HOST_ARCH)\" \
65 -X \"github.com/opencord/voltctl/internal/pkg/cli/version.BuildTime=$(BUILDTIME)\""
Zack Williamse940c7a2019-08-21 14:25:39 -070066
67# Release related items
68# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
69# Inspired by: https://github.com/kubernetes/minikube/releases
70RELEASE_DIR ?= release
71RELEASE_NAME ?= voltctl
Ciprian Barbu72bdf892020-01-29 13:42:48 +020072RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64
Zack Williamse940c7a2019-08-21 14:25:39 -070073
Kent Hagerman813cb902020-02-20 10:01:38 -050074# tool containers
75VOLTHA_TOOLS_VERSION ?= 2.0.0
Zack Williamse940c7a2019-08-21 14:25:39 -070076
Kent Hagerman813cb902020-02-20 10:01:38 -050077GO = 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
78GO_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
79GO_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
80GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
81GOLANGCI_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 -070082
Kent Hagerman813cb902020-02-20 10:01:38 -050083release:
84 @mkdir -p $(RELEASE_DIR)
85 @${GO_SH} ' \
86 set -e -o pipefail; \
87 for x in ${RELEASE_OS_ARCH}; do \
88 OUT_PATH="$(RELEASE_DIR)/$(RELEASE_NAME)-$(subst -dev,_dev,$(VERSION))-$$x"; \
89 echo "$$OUT_PATH"; \
90 GOOS=$${x%-*} GOARCH=$${x#*-} go build -mod=vendor -v $(LDFLAGS) -o "$$OUT_PATH" cmd/voltctl/voltctl.go; \
91 done'
divyadesaid3317312020-04-08 09:43:11 +000092## Local Development Helpers
93local-lib-go:
94ifdef LOCAL_LIB_GO
95 mkdir -p vendor/github.com/opencord/voltha-lib-go/v3/pkg
96 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v3/pkg/
97endif
Zack Williamse940c7a2019-08-21 14:25:39 -070098
divyadesaid3317312020-04-08 09:43:11 +000099build: local-lib-go
David Bainbridge86971522019-09-26 22:09:39 +0000100 go build -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go
Zack Williamse940c7a2019-08-21 14:25:39 -0700101
David Bainbridge86971522019-09-26 22:09:39 +0000102install:
103 go install -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go
Zack Williamse940c7a2019-08-21 14:25:39 -0700104
David Bainbridge86971522019-09-26 22:09:39 +0000105run:
106 go run -mod=vendor $(LDFLAGS) cmd/voltctl/voltctl.go $(CMD)
Zack Williamse940c7a2019-08-21 14:25:39 -0700107
David Bainbridge12f036f2019-10-15 22:09:04 +0000108lint-mod:
Scott Baker4a35a702019-11-26 08:17:33 -0800109 @echo "Running dependency check..."
Kent Hagerman813cb902020-02-20 10:01:38 -0500110 @${GO} mod verify
Scott Baker4a35a702019-11-26 08:17:33 -0800111 @echo "Dependency check OK. Running vendor check..."
112 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500113 @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)
114 @[[ `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)
115 ${GO} mod tidy
116 ${GO} mod vendor
Scott Baker4a35a702019-11-26 08:17:33 -0800117 @git status > /dev/null
Kent Hagerman813cb902020-02-20 10:01:38 -0500118 @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)
119 @[[ `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 -0800120 @echo "Vendor check OK."
David Bainbridge12f036f2019-10-15 22:09:04 +0000121
Kent Hagerman813cb902020-02-20 10:01:38 -0500122lint: lint-mod
David Bainbridge12f036f2019-10-15 22:09:04 +0000123
David Bainbridge12f036f2019-10-15 22:09:04 +0000124sca:
David Bainbridge12f036f2019-10-15 22:09:04 +0000125 @rm -rf ./sca-report
126 @mkdir -p ./sca-report
Kent Hagerman813cb902020-02-20 10:01:38 -0500127 @echo "Running static code analysis..."
128 @${GOLANGCI_LINT} run --deadline=20m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
129 @echo ""
130 @echo "Static code analysis OK"
David Bainbridge86971522019-09-26 22:09:39 +0000131
132test:
Scott Baker2b0ad652019-08-21 14:57:07 -0700133 @mkdir -p ./tests/results
Kent Hagerman813cb902020-02-20 10:01:38 -0500134 @${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 -0700135 RETURN=$$? ;\
Kent Hagerman813cb902020-02-20 10:01:38 -0500136 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
137 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker2b0ad652019-08-21 14:57:07 -0700138 exit $$RETURN
Zack Williamse940c7a2019-08-21 14:25:39 -0700139
140view-coverage:
David Bainbridge86971522019-09-26 22:09:39 +0000141 go tool cover -html ./tests/results/go-test-coverage.out
Zack Williamse940c7a2019-08-21 14:25:39 -0700142
David Bainbridge12f036f2019-10-15 22:09:04 +0000143check: lint sca test
144
Zack Williamse940c7a2019-08-21 14:25:39 -0700145clean:
David Bainbridge12f036f2019-10-15 22:09:04 +0000146 rm -rf voltctl voltctl.cp release sca-report
Kent Hagerman813cb902020-02-20 10:01:38 -0500147
148mod-update:
149 ${GO} mod tidy
150 ${GO} mod vendor