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