blob: 1095dcc24ca9e15a22260f072ce994e27dd2bedb [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)
Scott Baker85e36902019-07-01 10:36:23 -070011GOVERSION = $(shell go version 2>&1 | awk '{print $$3;}' )
Zack Williamsf354d5c2019-05-20 16:56:45 -070012
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 \
Matteo Scandolo722930f2019-05-21 15:55:31 -070033 "-X github.com/opencord/cordctl/cli/version.Version=$(VERSION) \
34 -X github.com/opencord/cordctl/cli/version.GitCommit=$(GITCOMMIT) \
35 -X github.com/opencord/cordctl/cli/version.GitDirty=$(GITDIRTY) \
36 -X github.com/opencord/cordctl/cli/version.GoVersion=$(GOVERSION) \
37 -X github.com/opencord/cordctl/cli/version.Os=$$GOOS \
38 -X github.com/opencord/cordctl/cli/version.Arch=$$GOARCH \
39 -X github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"
Scott Baker2c0ebda2019-05-06 16:55:47 -070040
Scott Baker14c8f182019-05-22 18:05:29 -070041# Settings for running with mock server
42TEST_PROTOSET = $(shell pwd)/mock/xos-core.protoset
43TEST_MOCK_DIR = $(shell pwd)/mock
44TEST_SERVER = localhost:50051
45TEST_USERNAME = admin@opencord.org
46TEST_PASSWORD = letmein
47
Scott Baker1fe72732019-10-21 10:58:51 -070048# Default is GO111MODULE=auto, which will refuse to use go mod if running
49# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
50# force module usage. This affects commands executed from this Makefile, but
51# not the environment inside the Docker build (which does not build from
52# inside a GOPATH).
53export GO111MODULE=on
Scott Baker2c0ebda2019-05-06 16:55:47 -070054
Scott Baker1fe72732019-10-21 10:58:51 -070055help:
56 @echo "release - build binaries using cross compliing for the support architectures"
57 @echo "build - build the binary as a local executable"
58 @echo "install - build and install the binary into \$$GOPATH/bin"
59 @echo "run - runs cordctl using the command specified as \$$CMD"
60 @echo "lint - run static code analysis"
61 @echo "test - run unity tests"
62 @echo "clean - remove temporary and generated files"
63
64build:
Matteo Scandolo722930f2019-05-21 15:55:31 -070065 export GOOS=$(HOST_OS) ;\
66 export GOARCH=$(HOST_ARCH) ;\
Scott Baker28a39562019-07-12 09:34:15 -070067 go build $(LDFLAGS) cmd/cordctl/cordctl.go
Scott Baker2c0ebda2019-05-06 16:55:47 -070068
Scott Baker1fe72732019-10-21 10:58:51 -070069install:
70 export GOOS=$(HOST_OS) ;\
71 export GOARCH=$(HOST_ARCH) ;\
72 go install -mod=vendor $(LDFLAGS) cmd/cordctl/cordctl.go
Scott Baker2c0ebda2019-05-06 16:55:47 -070073
Scott Baker1fe72732019-10-21 10:58:51 -070074run:
75 export GOOS=$(HOST_OS) ;\
76 export GOARCH=$(HOST_ARCH) ;\
77 go run -mod=vendor $(LDFLAGS) cmd/cordctl/cordctl.go $(CMD)
78
79lint:
Scott Baker2c0ebda2019-05-06 16:55:47 -070080 find $(GOPATH)/src/github.com/opencord/cordctl -name "*.go" -not -path '$(GOPATH)/src/github.com/opencord/cordctl/vendor/*' | xargs gofmt -l
Scott Baker1fe72732019-10-21 10:58:51 -070081 go vet -mod=vendor ./...
Scott Baker2c0ebda2019-05-06 16:55:47 -070082
Scott Baker1fe72732019-10-21 10:58:51 -070083test:
Scott Baker2c0ebda2019-05-06 16:55:47 -070084 @mkdir -p ./tests/results
Scott Baker14c8f182019-05-22 18:05:29 -070085 @set +e; \
86 CORDCTL_PROTOSET=$(TEST_PROTOSET)\
87 CORDCTL_SERVER=$(TEST_SERVER) \
88 CORDCTL_MOCK_DIR=$(TEST_MOCK_DIR) \
89 CORDCTL_USERNAME=$(TEST_USERNAME) \
90 CORDCTL_PASSWORD=$(TEST_PASSWORD) \
Scott Baker1fe72732019-10-21 10:58:51 -070091 go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Scott Baker2c0ebda2019-05-06 16:55:47 -070092 RETURN=$$? ;\
93 go-junit-report < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
94 gocover-cobertura < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Scott Baker14c8f182019-05-22 18:05:29 -070095 cd mock; \
96 docker-compose down; \
Scott Baker2c0ebda2019-05-06 16:55:47 -070097 exit $$RETURN
Scott Baker6cf525a2019-05-09 12:25:08 -070098
Zack Williamsf354d5c2019-05-20 16:56:45 -070099# Release related items
100# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
101# Inspired by: https://github.com/kubernetes/minikube/releases
102RELEASE_DIR ?= release
103RELEASE_NAME ?= cordctl
Zack Williamsd5cf75a2019-07-08 08:02:54 -0700104RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64
Zack Williamsf354d5c2019-05-20 16:56:45 -0700105RELEASE_BINS := $(foreach rel,$(RELEASE_OS_ARCH),$(RELEASE_DIR)/$(RELEASE_NAME)-$(rel))
106
107# Functions to extract the OS/ARCH
108rel_os = $(word 2, $(subst -, ,$(notdir $@)))
109rel_arch = $(word 3, $(subst -, ,$(notdir $@)))
110
Scott Baker1fe72732019-10-21 10:58:51 -0700111$(RELEASE_BINS):
Matteo Scandolo722930f2019-05-21 15:55:31 -0700112 export GOOS=$(rel_os) ;\
113 export GOARCH=$(rel_arch) ;\
Scott Baker1fe72732019-10-21 10:58:51 -0700114 go build -mod=vendor -v $(LDFLAGS) -o "$@" cmd/cordctl/cordctl.go
Zack Williamsf354d5c2019-05-20 16:56:45 -0700115
116release: $(RELEASE_BINS)
117
Scott Baker6cf525a2019-05-09 12:25:08 -0700118clean:
Zack Williamsf354d5c2019-05-20 16:56:45 -0700119 rm -f cordctl $(RELEASE_BINS)
Scott Baker6cf525a2019-05-09 12:25:08 -0700120 rm -rf vendor
121 rm -f Gopkg.lock
Zack Williamsf354d5c2019-05-20 16:56:45 -0700122