|
| 1 | +BINDIR := $(CURDIR)/bin |
| 2 | +INSTALL_PATH ?= /usr/local/bin |
| 3 | +DIST_DIRS := find * -type d -exec |
| 4 | +TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/386 linux/arm linux/arm64 linux/ppc64le linux/s390x windows/amd64 |
| 5 | + |
| 6 | +BINNAME ?= iter8 |
| 7 | +ITER8_IMG ?= iter8/iter8:latest |
| 8 | + |
| 9 | +GOBIN = $(shell go env GOBIN) |
| 10 | +ifeq ($(GOBIN),) |
| 11 | +GOBIN = $(shell go env GOPATH)/bin |
| 12 | +endif |
| 13 | +GOX = $(GOBIN)/gox |
| 14 | + |
| 15 | +# go options |
| 16 | +TAGS := |
| 17 | +LDFLAGS := -w -s |
| 18 | +GOFLAGS := |
| 19 | + |
| 20 | +# Rebuild the binary if any of these files change |
| 21 | +SRC := $(shell find . -type f -name '*.go' -print) go.mod go.sum |
| 22 | + |
| 23 | +# Required for globs to work correctly |
| 24 | +SHELL = /usr/bin/env bash |
| 25 | + |
| 26 | +GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null) |
| 27 | + |
| 28 | +ifdef VERSION |
| 29 | + BINARY_VERSION = $(VERSION) |
| 30 | +endif |
| 31 | +BINARY_VERSION ?= ${GIT_TAG} |
| 32 | + |
| 33 | +# Only set Version if building a tag or VERSION is set |
| 34 | +ifneq ($(BINARY_VERSION),) |
| 35 | + LDFLAGS += -X github.com/iter8-tools/iter8/basecli/RootCmd.Version=${BINARY_VERSION} |
| 36 | +endif |
| 37 | + |
| 38 | +LDFLAGS += $(EXT_LDFLAGS) |
| 39 | + |
| 40 | +.PHONY: all |
| 41 | +all: build |
| 42 | + |
| 43 | +# ------------------------------------------------------------------------------ |
| 44 | +# build |
| 45 | + |
| 46 | +.PHONY: build |
| 47 | +build: $(BINDIR)/$(BINNAME) |
| 48 | + |
| 49 | +$(BINDIR)/$(BINNAME): $(SRC) |
| 50 | + GO111MODULE=on go build $(GOFLAGS) -trimpath -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o '$(BINDIR)'/$(BINNAME) ./ |
| 51 | + |
| 52 | +.PHONY: cmddocs |
| 53 | +cmddocs: |
| 54 | + go run cmd/docs/main.go |
| 55 | + |
| 56 | +# ------------------------------------------------------------------------------ |
| 57 | +# install |
| 58 | + |
| 59 | +.PHONY: install |
| 60 | +install: build |
| 61 | + @install "$(BINDIR)/$(BINNAME)" "$(INSTALL_PATH)/$(BINNAME)" |
| 62 | + |
| 63 | +# ------------------------------------------------------------------------------ |
| 64 | +# dependencies |
| 65 | + |
| 66 | +# If go get is run from inside the project directory it will add the dependencies |
| 67 | +# to the go.mod file. To avoid that we change to a directory without a go.mod file |
| 68 | +# when downloading the following dependencies |
| 69 | + |
| 70 | +$(GOX): |
| 71 | + (cd /; GO111MODULE=on go get -u github.com/mitchellh/gox) |
| 72 | + |
| 73 | +# ------------------------------------------------------------------------------ |
| 74 | +# release |
| 75 | + |
| 76 | +.PHONY: build-cross |
| 77 | +build-cross: LDFLAGS += -extldflags "-static" |
| 78 | +build-cross: $(GOX) |
| 79 | + GOFLAGS="-trimpath" GO111MODULE=on CGO_ENABLED=0 $(GOX) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./ |
| 80 | + |
| 81 | +.PHONY: dist |
| 82 | +dist: build-cross |
| 83 | + ( \ |
| 84 | + cd _dist && \ |
| 85 | + $(DIST_DIRS) cp ../LICENSE {} \; && \ |
| 86 | + $(DIST_DIRS) cp ../README.md {} \; && \ |
| 87 | + $(DIST_DIRS) tar -zcf iter8-${VERSION}-{}.tar.gz {} \; && \ |
| 88 | + $(DIST_DIRS) zip -r iter8-${VERSION}-{}.zip {} \; \ |
| 89 | + ) |
| 90 | + |
| 91 | +.PHONY: clean |
| 92 | +clean: |
| 93 | + @rm -rf '$(BINDIR)' ./_dist |
| 94 | + |
| 95 | +.PHONY: docker-build |
| 96 | +docker-build: |
| 97 | + docker build -f Dockerfile -t $(ITER8_IMG) . |
| 98 | + |
| 99 | +.PHONY: docker-push |
| 100 | +docker-push: |
| 101 | + docker push $(ITER8_IMG) |
| 102 | + |
| 103 | +# ------------------------------------------------------------------------------ |
| 104 | +# test |
| 105 | + |
| 106 | +.PHONY: fmt |
1 | 107 | fmt: ## Run go fmt against code. |
2 | 108 | go fmt ./... |
3 | 109 |
|
| 110 | +.PHONY: vet |
4 | 111 | vet: ## Run go vet against code |
5 | 112 | go vet ./... |
6 | 113 |
|
| 114 | +.PHONY: staticcheck |
7 | 115 | staticcheck: |
8 | 116 | staticcheck ./... |
9 | 117 |
|
| 118 | +.PHONY: test |
10 | 119 | test: fmt vet ## Run tests. |
11 | 120 | go test ./... -race -coverprofile=coverage.out -covermode=atomic |
12 | 121 |
|
| 122 | +.PHONY: coverage |
13 | 123 | coverage: test |
14 | 124 | @echo "test coverage: $(shell go tool cover -func coverage.out | grep total | awk '{print substr($$3, 1, length($$3)-1)}')" |
15 | 125 |
|
16 | | -htmlcov: |
| 126 | +.PHONY: htmlcov |
| 127 | +htmlcov: coverage |
17 | 128 | go tool cover -html=coverage.out |
18 | | - |
19 | | -cmddocs: |
20 | | - go run cmd/docs/main.go |
21 | | - |
22 | | -# complete path to iter8 binary |
23 | | -ITER8_BIN ?= /usr/local/bin/iter8 |
24 | | -build: |
25 | | - go build -o $(ITER8_BIN) main.go |
26 | | - |
27 | | - |
28 | | -ITER8_IMG ?= iter8/iter8:latest |
29 | | -docker-build: |
30 | | - docker build -f Dockerfile -t $(ITER8_IMG) . |
31 | | - |
32 | | -docker-push: |
33 | | - docker push $(ITER8_IMG) |
|
0 commit comments