SEBA-682 Release builds for cordctl

Change-Id: Icf72ffa5355956e333c769f50c6fbd7c66f8a7d8
diff --git a/Makefile b/Makefile
index c7ca017..1f7edc5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,32 +1,47 @@
+# Makefile for cordctl
+
+# Set bash for fail quickly
+SHELL = bash -eu -o pipefail
+
 ifeq ($(GOPATH),)
-$(error Please set your GOPATH)
+  $(error Please set your GOPATH)
 endif
 
-VERSION=$(shell cat $(GOPATH)/src/github.com/opencord/cordctl/VERSION)
-GITCOMMIT=$(shell git log --pretty=format:"%h" -n 1)
+VERSION     ?= $(shell cat $(GOPATH)/src/github.com/opencord/cordctl/VERSION)
+GOVERSION    = $(shell go version 2>&1 | sed -E  's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
+
+GITCOMMIT   ?= $(shell git log --pretty=format:"%h" -n 1)
 ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
-GITDIRTY=false
+	GITDIRTY  := false
 else
-GITDIRTY=true
+	GITDIRTY  := true
 endif
-GOVERSION=$(shell go version 2>&1 | sed -E  's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g')
-OSTYPE=$(shell uname -s | tr A-Z a-z)
-OSARCH=$(shell uname -p | tr A-Z a-z)
-BUILDTIME=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
 
-LDFLAGS=-ldflags \
+# build target creates binaries for host OS and arch
+HOST_OS     ?= $(shell uname -s | tr A-Z a-z)
+
+# uname and golang disagree on name of CPU architecture
+ifeq ($(shell uname -m),x86_64)
+	HOST_ARCH ?= amd64
+else
+	HOST_ARCH ?= $(shell uname -m)
+endif
+
+BUILDTIME    = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
+
+LDFLAGS      = -ldflags \
 	'-X "github.com/opencord/cordctl/cli/version.Version=$(VERSION)"  \
 	 -X "github.com/opencord/cordctl/cli/version.GitCommit=$(GITCOMMIT)"  \
 	 -X "github.com/opencord/cordctl/cli/version.GitDirty=$(GITDIRTY)"  \
 	 -X "github.com/opencord/cordctl/cli/version.GoVersion=$(GOVERSION)"  \
-	 -X "github.com/opencord/cordctl/cli/version.Os=$(OSTYPE)" \
-	 -X "github.com/opencord/cordctl/cli/version.Arch=$(OSARCH)" \
+	 -X "github.com/opencord/cordctl/cli/version.Os=$$GOOS" \
+	 -X "github.com/opencord/cordctl/cli/version.Arch=$$GOARCH" \
 	 -X "github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"'
 
 help:
 
 build: dependencies
-	go build $(LDFLAGS) cmd/cordctl.go
+	GOOS=$(HOST_OS) GOARCH=$(HOST_ARCH) go build $(LDFLAGS) cmd/cordctl.go
 
 dependencies:
 	[ -d "vendor" ] || dep ensure
@@ -44,7 +59,26 @@
 	gocover-cobertura < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
 	exit $$RETURN
 
+
+# Release related items
+# Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
+# Inspired by: https://github.com/kubernetes/minikube/releases
+RELEASE_DIR     ?= release
+RELEASE_NAME    ?= cordctl
+RELEASE_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64
+RELEASE_BINS    := $(foreach rel,$(RELEASE_OS_ARCH),$(RELEASE_DIR)/$(RELEASE_NAME)-$(rel))
+
+# Functions to extract the OS/ARCH
+rel_os    = $(word 2, $(subst -, ,$(notdir $@)))
+rel_arch  = $(word 3, $(subst -, ,$(notdir $@)))
+
+$(RELEASE_BINS): dependencies
+	GOOS=$(rel_os) GOARCH=$(rel_arch) go build -v $(LDFLAGS) -o "$@" cmd/cordctl.go
+
+release: $(RELEASE_BINS)
+
 clean:
-	rm -f cordctl
+	rm -f cordctl $(RELEASE_BINS)
 	rm -rf vendor
 	rm -f Gopkg.lock
+