|
| 1 | +# Set an output prefix, which is the local directory if not specified |
| 2 | +PREFIX?=$(shell pwd) |
| 3 | + |
| 4 | +# Set the build dir, where built cross-compiled binaries will be output |
| 5 | +BUILDDIR := ${PREFIX}/cross |
| 6 | + |
| 7 | +# Populate version variables |
| 8 | +# Add to compile time flags |
| 9 | +VERSION := $(shell cat VERSION.txt) |
| 10 | +GITCOMMIT := $(shell git rev-parse --short HEAD) |
| 11 | +GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) |
| 12 | +ifneq ($(GITUNTRACKEDCHANGES),) |
| 13 | + GITCOMMIT := $(GITCOMMIT)-dirty |
| 14 | +endif |
| 15 | +ifeq ($(GITCOMMIT),) |
| 16 | + GITCOMMIT := ${GITHUB_SHA} |
| 17 | +endif |
| 18 | +CTIMEVAR=-X $(PKG)/version.GITCOMMIT=$(GITCOMMIT) -X $(PKG)/version.VERSION=$(VERSION) |
| 19 | +GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)" |
| 20 | +GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static" |
| 21 | + |
| 22 | +# Set our default go compiler |
| 23 | +GO := go |
| 24 | + |
| 25 | +# List the GOOS and GOARCH to build |
| 26 | +GOOSARCHES = $(shell cat .goosarch) |
| 27 | + |
| 28 | +# Set the graph driver as the current graphdriver if not set. |
| 29 | +DOCKER_GRAPHDRIVER := $(if $(DOCKER_GRAPHDRIVER),$(DOCKER_GRAPHDRIVER),$(shell docker info 2>&1 | grep "Storage Driver" | sed 's/.*: //')) |
| 30 | +export DOCKER_GRAPHDRIVER |
| 31 | + |
| 32 | +# If this session isn't interactive, then we don't want to allocate a |
| 33 | +# TTY, which would fail, but if it is interactive, we do want to attach |
| 34 | +# so that the user can send e.g. ^C through. |
| 35 | +INTERACTIVE := $(shell [ -t 0 ] && echo 1 || echo 0) |
| 36 | +ifeq ($(INTERACTIVE), 1) |
| 37 | + DOCKER_FLAGS += -t |
| 38 | +endif |
| 39 | + |
| 40 | +.PHONY: build |
| 41 | +build: prebuild $(NAME) ## Builds a dynamic executable or package. |
| 42 | + |
| 43 | +$(NAME): $(wildcard *.go) $(wildcard */*.go) VERSION.txt |
| 44 | + @echo "+ $@" |
| 45 | + $(GO) build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) . |
| 46 | + |
| 47 | +.PHONY: static |
| 48 | +static: prebuild ## Builds a static executable. |
| 49 | + @echo "+ $@" |
| 50 | + CGO_ENABLED=$(CGO_ENABLED) $(GO) build \ |
| 51 | + -tags "$(BUILDTAGS) static_build" \ |
| 52 | + ${GO_LDFLAGS_STATIC} -o $(NAME) . |
| 53 | + |
| 54 | +all: clean build fmt lint test staticcheck vet install ## Runs a clean, build, fmt, lint, test, staticcheck, vet and install. |
| 55 | + |
| 56 | +.PHONY: fmt |
| 57 | +fmt: ## Verifies all files have been `gofmt`ed. |
| 58 | + @echo "+ $@" |
| 59 | + @gofmt -s -l . | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr |
| 60 | + |
| 61 | +.PHONY: lint |
| 62 | +lint: ## Verifies `golint` passes. |
| 63 | + @echo "+ $@" |
| 64 | + @golint ./... | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr |
| 65 | + |
| 66 | +.PHONY: test |
| 67 | +test: prebuild ## Runs the go tests. |
| 68 | + @echo "+ $@" |
| 69 | + @$(GO) test -v -tags "$(BUILDTAGS) cgo" $(shell $(GO) list ./... | grep -v vendor) |
| 70 | + |
| 71 | +.PHONY: vet |
| 72 | +vet: ## Verifies `go vet` passes. |
| 73 | + @echo "+ $@" |
| 74 | + @$(GO) vet $(shell $(GO) list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr |
| 75 | + |
| 76 | +.PHONY: staticcheck |
| 77 | +staticcheck: ## Verifies `staticcheck` passes. |
| 78 | + @echo "+ $@" |
| 79 | + @staticcheck $(shell $(GO) list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr |
| 80 | + |
| 81 | +.PHONY: cover |
| 82 | +cover: prebuild ## Runs go test with coverage. |
| 83 | + @echo "" > coverage.txt |
| 84 | + @for d in $(shell $(GO) list ./... | grep -v vendor); do \ |
| 85 | + $(GO) test -race -coverprofile=profile.out -covermode=atomic "$$d"; \ |
| 86 | + if [ -f profile.out ]; then \ |
| 87 | + cat profile.out >> coverage.txt; \ |
| 88 | + rm profile.out; \ |
| 89 | + fi; \ |
| 90 | + done; |
| 91 | + |
| 92 | +.PHONY: install |
| 93 | +install: prebuild ## Installs the executable or package. |
| 94 | + @echo "+ $@" |
| 95 | + $(GO) install -a -tags "$(BUILDTAGS)" ${GO_LDFLAGS} . |
| 96 | + |
| 97 | +define buildpretty |
| 98 | +mkdir -p $(BUILDDIR)/$(1)/$(2); |
| 99 | +GOOS=$(1) GOARCH=$(2) CGO_ENABLED=$(CGO_ENABLED) $(GO) build \ |
| 100 | + -o $(BUILDDIR)/$(1)/$(2)/$(NAME) \ |
| 101 | + -a -tags "$(BUILDTAGS) static_build netgo" \ |
| 102 | + -installsuffix netgo ${GO_LDFLAGS_STATIC} .; |
| 103 | +md5sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).md5; |
| 104 | +sha256sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).sha256; |
| 105 | +endef |
| 106 | + |
| 107 | +.PHONY: cross |
| 108 | +cross: *.go VERSION.txt prebuild ## Builds the cross-compiled binaries, creating a clean directory structure (eg. GOOS/GOARCH/binary). |
| 109 | + @echo "+ $@" |
| 110 | + $(foreach GOOSARCH,$(GOOSARCHES), $(call buildpretty,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH)))) |
| 111 | + |
| 112 | +define buildrelease |
| 113 | +GOOS=$(1) GOARCH=$(2) CGO_ENABLED=$(CGO_ENABLED) $(GO) build \ |
| 114 | + -o $(BUILDDIR)/$(NAME)-$(1)-$(2) \ |
| 115 | + -a -tags "$(BUILDTAGS) static_build netgo" \ |
| 116 | + -installsuffix netgo ${GO_LDFLAGS_STATIC} .; |
| 117 | +md5sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).md5; |
| 118 | +sha256sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).sha256; |
| 119 | +endef |
| 120 | + |
| 121 | +.PHONY: release |
| 122 | +release: *.go VERSION.txt prebuild ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-GOOS-GOARCH). |
| 123 | + @echo "+ $@" |
| 124 | + $(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH)))) |
| 125 | + |
| 126 | +.PHONY: bump-version |
| 127 | +BUMP := patch |
| 128 | +bump-version: ## Bump the version in the version file. Set BUMP to [ patch | major | minor ]. |
| 129 | + @$(GO) get -u github.com/jessfraz/junk/sembump # update sembump tool |
| 130 | + $(eval NEW_VERSION = $(shell sembump --kind $(BUMP) $(VERSION))) |
| 131 | + @echo "Bumping VERSION.txt from $(VERSION) to $(NEW_VERSION)" |
| 132 | + echo $(NEW_VERSION) > VERSION.txt |
| 133 | + @echo "Updating links to download binaries in README.md" |
| 134 | + sed -i s/$(VERSION)/$(NEW_VERSION)/g README.md |
| 135 | + git add VERSION.txt README.md |
| 136 | + git commit -vsam "Bump version to $(NEW_VERSION)" |
| 137 | + @echo "Run make tag to create and push the tag for new version $(NEW_VERSION)" |
| 138 | + |
| 139 | +.PHONY: tag |
| 140 | +tag: ## Create a new git tag to prepare to build a release. |
| 141 | + git tag -sa $(VERSION) -m "$(VERSION)" |
| 142 | + @echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build." |
| 143 | + |
| 144 | +REGISTRY := r.j3ss.co |
| 145 | +.PHONY: image |
| 146 | +image: ## Create the docker image from the Dockerfile. |
| 147 | + @docker build --rm --force-rm -t $(REGISTRY)/$(NAME) . |
| 148 | + |
| 149 | +.PHONY: image-dev |
| 150 | +image-dev: |
| 151 | + @docker build --rm --force-rm -f Dockerfile.dev -t $(REGISTRY)/$(NAME):dev . |
| 152 | + |
| 153 | +.PHONY: AUTHORS |
| 154 | +AUTHORS: |
| 155 | + @$(file >$@,# This file lists all individuals having contributed content to the repository.) |
| 156 | + @$(file >>$@,# For how it is generated, see `make AUTHORS`.) |
| 157 | + @echo "$(shell git log --format='\n%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf)" >> $@ |
| 158 | + |
| 159 | +.PHONY: vendor |
| 160 | +vendor: ## Updates the vendoring directory. |
| 161 | + @$(RM) go.sum |
| 162 | + @$(RM) -r vendor |
| 163 | + GO111MODULE=on $(GO) mod init || true |
| 164 | + GO111MODULE=on $(GO) mod tidy |
| 165 | + GO111MODULE=on $(GO) mod vendor |
| 166 | + @$(RM) Gopkg.toml Gopkg.lock |
| 167 | + |
| 168 | +.PHONY: clean |
| 169 | +clean: ## Cleanup any build binaries or packages. |
| 170 | + @echo "+ $@" |
| 171 | + $(RM) $(NAME) |
| 172 | + $(RM) -r $(BUILDDIR) |
| 173 | + |
| 174 | +.PHONY: help |
| 175 | +help: |
| 176 | + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | sed 's/^[^:]*://g' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' |
| 177 | + |
| 178 | +check_defined = \ |
| 179 | + $(strip $(foreach 1,$1, \ |
| 180 | + $(call __check_defined,$1,$(strip $(value 2))))) |
| 181 | + |
| 182 | +__check_defined = \ |
| 183 | + $(if $(value $1),, \ |
| 184 | + $(error Undefined $1$(if $2, ($2))$(if $(value @), \ |
| 185 | + required by target `$@'))) |
0 commit comments