1# The import path is where your repository can be found.
2# To import subpackages, always prepend the full import path.
3# If you change this, run `make clean`. Read more: https://git.io/vM7zV
4IMPORT_PATH := github.com/FiloSottile/b2
5
6V := 1 # When V is set, print commands and build progress.
7
8# Space separated patterns of packages to skip in list, test, format.
9IGNORED_PACKAGES := /vendor/
10
11.PHONY: build
12build: .GOPATH/.ok
13	$Q go install $(if $V,-v) $(VERSION_FLAGS) $(IMPORT_PATH)
14
15##### =====> Utility targets <===== #####
16
17.PHONY: clean test list cover format
18
19clean:
20	$Q rm -rf bin .GOPATH
21
22test: .GOPATH/.ok
23	$Q go test $(if $V,-v) -i -race $(allpackages) # install -race libs to speed up next run
24ifndef CI
25	$Q go vet $(allpackages)
26	$Q GODEBUG=cgocheck=2 go test -race $(allpackages)
27else
28	$Q ( go vet $(allpackages); echo $$? ) | \
29	    tee .GOPATH/test/vet.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/vet.txt)
30	$Q ( GODEBUG=cgocheck=2 go test -v -race $(allpackages); echo $$? ) | \
31	    tee .GOPATH/test/output.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/output.txt)
32endif
33
34list: .GOPATH/.ok
35	@echo $(allpackages)
36
37cover: bin/gocovmerge .GOPATH/.ok
38	@echo "NOTE: make cover does not exit 1 on failure, don't use it to check for tests success!"
39	$Q rm -f .GOPATH/cover/*.out .GOPATH/cover/all.merged
40	$(if $V,@echo "-- go test -coverpkg=./... -coverprofile=.GOPATH/cover/... ./...")
41	@for MOD in $(allpackages); do \
42		go test -coverpkg=`echo $(allpackages)|tr " " ","` \
43			-coverprofile=.GOPATH/cover/unit-`echo $$MOD|tr "/" "_"`.out \
44			$$MOD 2>&1 | grep -v "no packages being tested depend on"; \
45	done
46	$Q ./bin/gocovmerge .GOPATH/cover/*.out > .GOPATH/cover/all.merged
47ifndef CI
48	$Q go tool cover -html .GOPATH/cover/all.merged
49else
50	$Q go tool cover -html .GOPATH/cover/all.merged -o .GOPATH/cover/all.html
51endif
52	@echo ""
53	@echo "=====> Total test coverage: <====="
54	@echo ""
55	$Q go tool cover -func .GOPATH/cover/all.merged
56
57format: bin/goimports .GOPATH/.ok
58	$Q find .GOPATH/src/$(IMPORT_PATH)/ -iname \*.go | grep -v \
59	    -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) | xargs ./bin/goimports -w
60
61##### =====> Internals <===== #####
62
63.PHONY: setup
64setup: clean .GOPATH/.ok
65	@if ! grep "/.GOPATH" .gitignore > /dev/null 2>&1; then \
66	    echo "/.GOPATH" >> .gitignore; \
67	    echo "/bin" >> .gitignore; \
68	fi
69	go get -u github.com/FiloSottile/gvt
70	- ./bin/gvt fetch golang.org/x/tools/cmd/goimports
71	- ./bin/gvt fetch github.com/wadey/gocovmerge
72
73VERSION          := $(shell git describe --tags --always --dirty="-dev")
74DATE             := $(shell date -u '+%Y-%m-%d-%H%M UTC')
75VERSION_FLAGS    := -ldflags='-X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
76
77# cd into the GOPATH to workaround ./... not following symlinks
78_allpackages = $(shell ( cd $(CURDIR)/.GOPATH/src/$(IMPORT_PATH) && \
79    GOPATH=$(CURDIR)/.GOPATH go list ./... 2>&1 1>&3 | \
80    grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) 1>&2 ) 3>&1 | \
81    grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)))
82
83# memoize allpackages, so that it's executed only once and only if used
84allpackages = $(if $(__allpackages),,$(eval __allpackages := $$(_allpackages)))$(__allpackages)
85
86export GOPATH := $(CURDIR)/.GOPATH
87unexport GOBIN
88
89Q := $(if $V,,@)
90
91.GOPATH/.ok:
92	$Q mkdir -p "$(dir .GOPATH/src/$(IMPORT_PATH))"
93	$Q ln -s ../../../.. ".GOPATH/src/$(IMPORT_PATH)"
94	$Q mkdir -p .GOPATH/test .GOPATH/cover
95	$Q mkdir -p bin
96	$Q ln -s ../bin .GOPATH/bin
97	$Q touch $@
98
99.PHONY: bin/gocovmerge bin/goimports
100bin/gocovmerge: .GOPATH/.ok
101	@test -d ./vendor/github.com/wadey/gocovmerge || \
102	    { echo "Vendored gocovmerge not found, try running 'make setup'..."; exit 1; }
103	$Q go install $(IMPORT_PATH)/vendor/github.com/wadey/gocovmerge
104bin/goimports: .GOPATH/.ok
105	@test -d ./vendor/golang.org/x/tools/cmd/goimports || \
106	    { echo "Vendored goimports not found, try running 'make setup'..."; exit 1; }
107	$Q go install $(IMPORT_PATH)/vendor/golang.org/x/tools/cmd/goimports
108
109# Based on https://github.com/cloudflare/hellogopher - v1.1 - MIT License
110#
111# Copyright (c) 2017 Cloudflare
112#
113# Permission is hereby granted, free of charge, to any person obtaining a copy
114# of this software and associated documentation files (the "Software"), to deal
115# in the Software without restriction, including without limitation the rights
116# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
117# copies of the Software, and to permit persons to whom the Software is
118# furnished to do so, subject to the following conditions:
119#
120# The above copyright notice and this permission notice shall be included in all
121# copies or substantial portions of the Software.
122#
123# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
124# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
125# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
126# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
127# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
128# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
129# SOFTWARE.
130