1# MAKEFILE
2#
3# @author      Nicola Asuni <info@tecnick.com>
4# @link        https://github.com/dgryski/go-farm
5#
6# This file is intended to be executed in a Linux-compatible system.
7# It also assumes that the project has been cloned in the right path under GOPATH:
8# $GOPATH/src/github.com/dgryski/go-farm
9#
10# ------------------------------------------------------------------------------
11
12# List special make targets that are not associated with files
13.PHONY: help all test format fmtcheck vet lint coverage cyclo ineffassign misspell structcheck varcheck errcheck gosimple astscan qa deps clean nuke
14
15# Use bash as shell (Note: Ubuntu now uses dash which doesn't support PIPESTATUS).
16SHELL=/bin/bash
17
18# CVS path (path to the parent dir containing the project)
19CVSPATH=github.com/dgryski
20
21# Project owner
22OWNER=dgryski
23
24# Project vendor
25VENDOR=dgryski
26
27# Project name
28PROJECT=go-farm
29
30# Project version
31VERSION=$(shell cat VERSION)
32
33# Name of RPM or DEB package
34PKGNAME=${VENDOR}-${PROJECT}
35
36# Current directory
37CURRENTDIR=$(shell pwd)
38
39# GO lang path
40ifneq ($(GOPATH),)
41	ifeq ($(findstring $(GOPATH),$(CURRENTDIR)),)
42		# the defined GOPATH is not valid
43		GOPATH=
44	endif
45endif
46ifeq ($(GOPATH),)
47	# extract the GOPATH
48	GOPATH=$(firstword $(subst /src/, ,$(CURRENTDIR)))
49endif
50
51# --- MAKE TARGETS ---
52
53# Display general help about this command
54help:
55	@echo ""
56	@echo "$(PROJECT) Makefile."
57	@echo "GOPATH=$(GOPATH)"
58	@echo "The following commands are available:"
59	@echo ""
60	@echo "    make qa          : Run all the tests"
61	@echo "    make test        : Run the unit tests"
62	@echo ""
63	@echo "    make format      : Format the source code"
64	@echo "    make fmtcheck    : Check if the source code has been formatted"
65	@echo "    make vet         : Check for suspicious constructs"
66	@echo "    make lint        : Check for style errors"
67	@echo "    make coverage    : Generate the coverage report"
68	@echo "    make cyclo       : Generate the cyclomatic complexity report"
69	@echo "    make ineffassign : Detect ineffectual assignments"
70	@echo "    make misspell    : Detect commonly misspelled words in source files"
71	@echo "    make structcheck : Find unused struct fields"
72	@echo "    make varcheck    : Find unused global variables and constants"
73	@echo "    make errcheck    : Check that error return values are used"
74	@echo "    make gosimple    : Suggest code simplifications"
75	@echo "    make astscan     : GO AST scanner"
76	@echo ""
77	@echo "    make docs        : Generate source code documentation"
78	@echo ""
79	@echo "    make deps        : Get the dependencies"
80	@echo "    make clean       : Remove any build artifact"
81	@echo "    make nuke        : Deletes any intermediate file"
82	@echo ""
83
84
85# Alias for help target
86all: help
87
88# Run the unit tests
89test:
90	@mkdir -p target/test
91	@mkdir -p target/report
92	GOPATH=$(GOPATH) \
93	go test \
94	-covermode=atomic \
95	-bench=. \
96	-race \
97	-cpuprofile=target/report/cpu.out \
98	-memprofile=target/report/mem.out \
99	-mutexprofile=target/report/mutex.out \
100	-coverprofile=target/report/coverage.out \
101	-v ./... | \
102	tee >(PATH=$(GOPATH)/bin:$(PATH) go-junit-report > target/test/report.xml); \
103	test $${PIPESTATUS[0]} -eq 0
104
105# Format the source code
106format:
107	@find . -type f -name "*.go" -exec gofmt -s -w {} \;
108
109# Check if the source code has been formatted
110fmtcheck:
111	@mkdir -p target
112	@find . -type f -name "*.go" -exec gofmt -s -d {} \; | tee target/format.diff
113	@test ! -s target/format.diff || { echo "ERROR: the source code has not been formatted - please use 'make format' or 'gofmt'"; exit 1; }
114
115# Check for syntax errors
116vet:
117	GOPATH=$(GOPATH) go vet .
118
119# Check for style errors
120lint:
121	GOPATH=$(GOPATH) PATH=$(GOPATH)/bin:$(PATH) golint .
122
123# Generate the coverage report
124coverage:
125	@mkdir -p target/report
126	GOPATH=$(GOPATH) \
127	go tool cover -html=target/report/coverage.out -o target/report/coverage.html
128
129# Report cyclomatic complexity
130cyclo:
131	@mkdir -p target/report
132	GOPATH=$(GOPATH) gocyclo -avg ./ | tee target/report/cyclo.txt ; test $${PIPESTATUS[0]} -eq 0
133
134# Detect ineffectual assignments
135ineffassign:
136	@mkdir -p target/report
137	GOPATH=$(GOPATH) ineffassign ./ | tee target/report/ineffassign.txt ; test $${PIPESTATUS[0]} -eq 0
138
139# Detect commonly misspelled words in source files
140misspell:
141	@mkdir -p target/report
142	GOPATH=$(GOPATH) misspell -error ./  | tee target/report/misspell.txt ; test $${PIPESTATUS[0]} -eq 0
143
144# Find unused struct fields
145structcheck:
146	@mkdir -p target/report
147	GOPATH=$(GOPATH) structcheck -a ./  | tee target/report/structcheck.txt
148
149# Find unused global variables and constants
150varcheck:
151	@mkdir -p target/report
152	GOPATH=$(GOPATH) varcheck -e ./  | tee target/report/varcheck.txt
153
154# Check that error return values are used
155errcheck:
156	@mkdir -p target/report
157	GOPATH=$(GOPATH) errcheck ./  | tee target/report/errcheck.txt
158
159# Suggest code simplifications
160gosimple:
161	@mkdir -p target/report
162	GOPATH=$(GOPATH) gosimple ./  | tee target/report/gosimple.txt
163
164# AST scanner
165astscan:
166	@mkdir -p target/report
167	GOPATH=$(GOPATH) gas .//*.go | tee target/report/astscan.txt
168
169# Generate source docs
170docs:
171	@mkdir -p target/docs
172	nohup sh -c 'GOPATH=$(GOPATH) godoc -http=127.0.0.1:6060' > target/godoc_server.log 2>&1 &
173	wget --directory-prefix=target/docs/ --execute robots=off --retry-connrefused --recursive --no-parent --adjust-extension --page-requisites --convert-links http://127.0.0.1:6060/pkg/github.com/${VENDOR}/${PROJECT}/ ; kill -9 `lsof -ti :6060`
174	@echo '<html><head><meta http-equiv="refresh" content="0;./127.0.0.1:6060/pkg/'${CVSPATH}'/'${PROJECT}'/index.html"/></head><a href="./127.0.0.1:6060/pkg/'${CVSPATH}'/'${PROJECT}'/index.html">'${PKGNAME}' Documentation ...</a></html>' > target/docs/index.html
175
176# Alias to run all quality-assurance checks
177qa: fmtcheck test vet lint coverage cyclo ineffassign misspell structcheck varcheck errcheck gosimple astscan
178
179# --- INSTALL ---
180
181# Get the dependencies
182deps:
183	GOPATH=$(GOPATH) go get ./...
184	GOPATH=$(GOPATH) go get github.com/golang/lint/golint
185	GOPATH=$(GOPATH) go get github.com/jstemmer/go-junit-report
186	GOPATH=$(GOPATH) go get github.com/axw/gocov/gocov
187	GOPATH=$(GOPATH) go get github.com/fzipp/gocyclo
188	GOPATH=$(GOPATH) go get github.com/gordonklaus/ineffassign
189	GOPATH=$(GOPATH) go get github.com/client9/misspell/cmd/misspell
190	GOPATH=$(GOPATH) go get github.com/opennota/check/cmd/structcheck
191	GOPATH=$(GOPATH) go get github.com/opennota/check/cmd/varcheck
192	GOPATH=$(GOPATH) go get github.com/kisielk/errcheck
193	GOPATH=$(GOPATH) go get honnef.co/go/tools/cmd/gosimple
194	GOPATH=$(GOPATH) go get github.com/GoASTScanner/gas
195
196# Remove any build artifact
197clean:
198	GOPATH=$(GOPATH) go clean ./...
199
200# Deletes any intermediate file
201nuke:
202	rm -rf ./target
203	GOPATH=$(GOPATH) go clean -i ./...
204