Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 1 | # Makefile for cordctl |
| 2 | |
| 3 | # Set bash for fail quickly |
| 4 | SHELL = bash -eu -o pipefail |
| 5 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 6 | ifeq ($(GOPATH),) |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 7 | $(error Please set your GOPATH) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 8 | endif |
| 9 | |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 10 | VERSION ?= $(shell cat $(GOPATH)/src/github.com/opencord/cordctl/VERSION) |
| 11 | GOVERSION = $(shell go version 2>&1 | sed -E 's/.*(go[0-9]+\.[0-9]+\.[0-9]+).*/\1/g') |
| 12 | |
| 13 | GITCOMMIT ?= $(shell git log --pretty=format:"%h" -n 1) |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 14 | ifeq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 15 | GITDIRTY := false |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 16 | else |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 17 | GITDIRTY := true |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 18 | endif |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 19 | |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 20 | # build target creates binaries for host OS and arch |
| 21 | HOST_OS ?= $(shell uname -s | tr A-Z a-z) |
| 22 | |
| 23 | # uname and golang disagree on name of CPU architecture |
| 24 | ifeq ($(shell uname -m),x86_64) |
| 25 | HOST_ARCH ?= amd64 |
| 26 | else |
| 27 | HOST_ARCH ?= $(shell uname -m) |
| 28 | endif |
| 29 | |
| 30 | BUILDTIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 31 | |
| 32 | LDFLAGS = -ldflags \ |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 33 | '-X "github.com/opencord/cordctl/cli/version.Version=$(VERSION)" \ |
| 34 | -X "github.com/opencord/cordctl/cli/version.GitCommit=$(GITCOMMIT)" \ |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 35 | -X "github.com/opencord/cordctl/cli/version.GitDirty=$(GITDIRTY)" \ |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 36 | -X "github.com/opencord/cordctl/cli/version.GoVersion=$(GOVERSION)" \ |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 37 | -X "github.com/opencord/cordctl/cli/version.Os=$$GOOS" \ |
| 38 | -X "github.com/opencord/cordctl/cli/version.Arch=$$GOARCH" \ |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 39 | -X "github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"' |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 40 | |
| 41 | help: |
| 42 | |
| 43 | build: dependencies |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 44 | GOOS=$(HOST_OS) GOARCH=$(HOST_ARCH) go build $(LDFLAGS) cmd/cordctl.go |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 45 | |
| 46 | dependencies: |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 47 | [ -d "vendor" ] || dep ensure |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 48 | |
| 49 | lint: 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 | |
| 54 | test: 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 61 | |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 62 | |
| 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 |
| 66 | RELEASE_DIR ?= release |
| 67 | RELEASE_NAME ?= cordctl |
| 68 | RELEASE_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64 |
| 69 | RELEASE_BINS := $(foreach rel,$(RELEASE_OS_ARCH),$(RELEASE_DIR)/$(RELEASE_NAME)-$(rel)) |
| 70 | |
| 71 | # Functions to extract the OS/ARCH |
| 72 | rel_os = $(word 2, $(subst -, ,$(notdir $@))) |
| 73 | rel_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 | |
| 78 | release: $(RELEASE_BINS) |
| 79 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 80 | clean: |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 81 | rm -f cordctl $(RELEASE_BINS) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 82 | rm -rf vendor |
| 83 | rm -f Gopkg.lock |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame^] | 84 | |