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) |
Scott Baker | 85e3690 | 2019-07-01 10:36:23 -0700 | [diff] [blame] | 11 | GOVERSION = $(shell go version 2>&1 | awk '{print $$3;}' ) |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame] | 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 \ |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 33 | "-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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 40 | |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 41 | # Settings for running with mock server |
| 42 | TEST_PROTOSET = $(shell pwd)/mock/xos-core.protoset |
| 43 | TEST_MOCK_DIR = $(shell pwd)/mock |
| 44 | TEST_SERVER = localhost:50051 |
| 45 | TEST_USERNAME = admin@opencord.org |
| 46 | TEST_PASSWORD = letmein |
| 47 | |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 48 | # 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). |
| 53 | export GO111MODULE=on |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 54 | |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 55 | help: |
| 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 | |
| 64 | build: |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 65 | export GOOS=$(HOST_OS) ;\ |
| 66 | export GOARCH=$(HOST_ARCH) ;\ |
Scott Baker | 28a3956 | 2019-07-12 09:34:15 -0700 | [diff] [blame] | 67 | go build $(LDFLAGS) cmd/cordctl/cordctl.go |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 68 | |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 69 | install: |
| 70 | export GOOS=$(HOST_OS) ;\ |
| 71 | export GOARCH=$(HOST_ARCH) ;\ |
| 72 | go install -mod=vendor $(LDFLAGS) cmd/cordctl/cordctl.go |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 73 | |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 74 | run: |
| 75 | export GOOS=$(HOST_OS) ;\ |
| 76 | export GOARCH=$(HOST_ARCH) ;\ |
| 77 | go run -mod=vendor $(LDFLAGS) cmd/cordctl/cordctl.go $(CMD) |
| 78 | |
Scott Baker | 40876a0 | 2019-11-26 08:22:41 -0800 | [diff] [blame] | 79 | lint-style: |
| 80 | ifeq (,$(shell which gofmt)) |
| 81 | go get -u github.com/golang/go/src/cmd/gofmt |
| 82 | endif |
| 83 | @echo -n "Running style check ... " |
| 84 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 85 | if [ ! -z "$$gofmt_out" ]; then \ |
| 86 | echo "$$gofmt_out" ;\ |
| 87 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 88 | exit 1 ;\ |
| 89 | fi |
| 90 | @echo "OK" |
| 91 | |
| 92 | lint-sanity: |
| 93 | @echo -n "Running sanity check ... " |
| 94 | @go vet -mod=vendor ./... |
| 95 | @echo "OK" |
| 96 | |
| 97 | lint-mod: |
| 98 | @echo "Running dependency check..." |
| 99 | @go mod verify |
| 100 | @echo "Dependency check OK. Running vendor check..." |
| 101 | @git status > /dev/null |
| 102 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1) |
| 103 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1) |
| 104 | go mod tidy |
| 105 | go mod vendor |
| 106 | @git status > /dev/null |
| 107 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 108 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1) |
| 109 | @echo "Vendor check OK." |
| 110 | |
| 111 | lint: lint-style lint-sanity lint-mod |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 112 | |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 113 | test: |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 114 | @mkdir -p ./tests/results |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 115 | @set +e; \ |
| 116 | CORDCTL_PROTOSET=$(TEST_PROTOSET)\ |
| 117 | CORDCTL_SERVER=$(TEST_SERVER) \ |
| 118 | CORDCTL_MOCK_DIR=$(TEST_MOCK_DIR) \ |
| 119 | CORDCTL_USERNAME=$(TEST_USERNAME) \ |
| 120 | CORDCTL_PASSWORD=$(TEST_PASSWORD) \ |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 121 | go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 122 | RETURN=$$? ;\ |
| 123 | go-junit-report < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 124 | gocover-cobertura < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 125 | cd mock; \ |
| 126 | docker-compose down; \ |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 127 | exit $$RETURN |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 128 | |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame] | 129 | # Release related items |
| 130 | # Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH |
| 131 | # Inspired by: https://github.com/kubernetes/minikube/releases |
| 132 | RELEASE_DIR ?= release |
| 133 | RELEASE_NAME ?= cordctl |
Zack Williams | d5cf75a | 2019-07-08 08:02:54 -0700 | [diff] [blame] | 134 | RELEASE_OS_ARCH ?= linux-amd64 linux-arm64 windows-amd64 darwin-amd64 |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame] | 135 | RELEASE_BINS := $(foreach rel,$(RELEASE_OS_ARCH),$(RELEASE_DIR)/$(RELEASE_NAME)-$(rel)) |
| 136 | |
| 137 | # Functions to extract the OS/ARCH |
| 138 | rel_os = $(word 2, $(subst -, ,$(notdir $@))) |
| 139 | rel_arch = $(word 3, $(subst -, ,$(notdir $@))) |
| 140 | |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 141 | $(RELEASE_BINS): |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 142 | export GOOS=$(rel_os) ;\ |
| 143 | export GOARCH=$(rel_arch) ;\ |
Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 144 | go build -mod=vendor -v $(LDFLAGS) -o "$@" cmd/cordctl/cordctl.go |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame] | 145 | |
| 146 | release: $(RELEASE_BINS) |
| 147 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 148 | clean: |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame] | 149 | rm -f cordctl $(RELEASE_BINS) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 150 | rm -rf vendor |
| 151 | rm -f Gopkg.lock |
Zack Williams | f354d5c | 2019-05-20 16:56:45 -0700 | [diff] [blame] | 152 | |