blob: 1f7edc56839e78afed18add95ccf335a7894ac99 [file] [log] [blame]
Zack Williamsf354d5c2019-05-20 16:56:45 -07001# Makefile for cordctl
2
3# Set bash for fail quickly
4SHELL = bash -eu -o pipefail
5
Scott Baker2c0ebda2019-05-06 16:55:47 -07006ifeq ($(GOPATH),)
Zack Williamsf354d5c2019-05-20 16:56:45 -07007 $(error Please set your GOPATH)
Scott Baker2c0ebda2019-05-06 16:55:47 -07008endif
9
Zack Williamsf354d5c2019-05-20 16:56:45 -070010VERSION ?= $(shell cat $(GOPATH)/src/github.com/opencord/cordctl/VERSION)
11GOVERSION = $(shell go version 2>&1 | sed -E 's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
12
13GITCOMMIT ?= $(shell git log --pretty=format:"%h" -n 1)
Scott Baker63ce82e2019-05-15 09:01:42 -070014ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
Zack Williamsf354d5c2019-05-20 16:56:45 -070015 GITDIRTY := false
Scott Baker63ce82e2019-05-15 09:01:42 -070016else
Zack Williamsf354d5c2019-05-20 16:56:45 -070017 GITDIRTY := true
Scott Baker63ce82e2019-05-15 09:01:42 -070018endif
Scott Baker2c0ebda2019-05-06 16:55:47 -070019
Zack Williamsf354d5c2019-05-20 16:56:45 -070020# build target creates binaries for host OS and arch
21HOST_OS ?= $(shell uname -s | tr A-Z a-z)
22
23# uname and golang disagree on name of CPU architecture
24ifeq ($(shell uname -m),x86_64)
25 HOST_ARCH ?= amd64
26else
27 HOST_ARCH ?= $(shell uname -m)
28endif
29
30BUILDTIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
31
32LDFLAGS = -ldflags \
Scott Baker2c0ebda2019-05-06 16:55:47 -070033 '-X "github.com/opencord/cordctl/cli/version.Version=$(VERSION)" \
34 -X "github.com/opencord/cordctl/cli/version.GitCommit=$(GITCOMMIT)" \
Scott Baker63ce82e2019-05-15 09:01:42 -070035 -X "github.com/opencord/cordctl/cli/version.GitDirty=$(GITDIRTY)" \
Scott Baker2c0ebda2019-05-06 16:55:47 -070036 -X "github.com/opencord/cordctl/cli/version.GoVersion=$(GOVERSION)" \
Zack Williamsf354d5c2019-05-20 16:56:45 -070037 -X "github.com/opencord/cordctl/cli/version.Os=$$GOOS" \
38 -X "github.com/opencord/cordctl/cli/version.Arch=$$GOARCH" \
Scott Baker63ce82e2019-05-15 09:01:42 -070039 -X "github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"'
Scott Baker2c0ebda2019-05-06 16:55:47 -070040
41help:
42
43build: dependencies
Zack Williamsf354d5c2019-05-20 16:56:45 -070044 GOOS=$(HOST_OS) GOARCH=$(HOST_ARCH) go build $(LDFLAGS) cmd/cordctl.go
Scott Baker2c0ebda2019-05-06 16:55:47 -070045
46dependencies:
Scott Baker6cf525a2019-05-09 12:25:08 -070047 [ -d "vendor" ] || dep ensure
Scott Baker2c0ebda2019-05-06 16:55:47 -070048
49lint: dependencies
50 find $(GOPATH)/src/github.com/opencord/cordctl -name "*.go" -not -path '$(GOPATH)/src/github.com/opencord/cordctl/vendor/*' | xargs gofmt -l
51 go vet ./...
52 dep check
53
54test: dependencies
55 @mkdir -p ./tests/results
56 @go test -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
57 RETURN=$$? ;\
58 go-junit-report < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
59 gocover-cobertura < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
60 exit $$RETURN
Scott Baker6cf525a2019-05-09 12:25:08 -070061
Zack Williamsf354d5c2019-05-20 16:56:45 -070062
63# Release related items
64# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
65# Inspired by: https://github.com/kubernetes/minikube/releases
66RELEASE_DIR ?= release
67RELEASE_NAME ?= cordctl
68RELEASE_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64
69RELEASE_BINS := $(foreach rel,$(RELEASE_OS_ARCH),$(RELEASE_DIR)/$(RELEASE_NAME)-$(rel))
70
71# Functions to extract the OS/ARCH
72rel_os = $(word 2, $(subst -, ,$(notdir $@)))
73rel_arch = $(word 3, $(subst -, ,$(notdir $@)))
74
75$(RELEASE_BINS): dependencies
76 GOOS=$(rel_os) GOARCH=$(rel_arch) go build -v $(LDFLAGS) -o "$@" cmd/cordctl.go
77
78release: $(RELEASE_BINS)
79
Scott Baker6cf525a2019-05-09 12:25:08 -070080clean:
Zack Williamsf354d5c2019-05-20 16:56:45 -070081 rm -f cordctl $(RELEASE_BINS)
Scott Baker6cf525a2019-05-09 12:25:08 -070082 rm -rf vendor
83 rm -f Gopkg.lock
Zack Williamsf354d5c2019-05-20 16:56:45 -070084