Makefile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. # List special make targets that are not associated with files
  12. .PHONY: help all test format fmtcheck vet lint coverage cyclo misspell errcheck staticcheck astscan qa deps clean nuke
  13. # Use bash as shell (Note: Ubuntu now uses dash which doesn't support PIPESTATUS).
  14. SHELL=/bin/bash
  15. # CVS path (path to the parent dir containing the project)
  16. CVSPATH=github.com/dgryski
  17. # Project owner
  18. OWNER=dgryski
  19. # Project vendor
  20. VENDOR=dgryski
  21. # Project name
  22. PROJECT=go-farm
  23. # Project version
  24. VERSION=$(shell cat VERSION)
  25. # Name of RPM or DEB package
  26. PKGNAME=${VENDOR}-${PROJECT}
  27. # Current directory
  28. CURRENTDIR=$(shell pwd)
  29. # GO lang path
  30. ifneq ($(GOPATH),)
  31. ifeq ($(findstring $(GOPATH),$(CURRENTDIR)),)
  32. # the defined GOPATH is not valid
  33. GOPATH=
  34. endif
  35. endif
  36. ifeq ($(GOPATH),)
  37. # extract the GOPATH
  38. GOPATH=$(firstword $(subst /src/, ,$(CURRENTDIR)))
  39. endif
  40. # --- MAKE TARGETS ---
  41. # Display general help about this command
  42. help:
  43. @echo ""
  44. @echo "$(PROJECT) Makefile."
  45. @echo "GOPATH=$(GOPATH)"
  46. @echo "The following commands are available:"
  47. @echo ""
  48. @echo " make qa : Run all the tests"
  49. @echo " make test : Run the unit tests"
  50. @echo ""
  51. @echo " make format : Format the source code"
  52. @echo " make fmtcheck : Check if the source code has been formatted"
  53. @echo " make vet : Check for suspicious constructs"
  54. @echo " make lint : Check for style errors"
  55. @echo " make coverage : Generate the coverage report"
  56. @echo " make cyclo : Generate the cyclomatic complexity report"
  57. @echo " make misspell : Detect commonly misspelled words in source files"
  58. @echo " make staticcheck : Run staticcheck
  59. @echo " make errcheck : Check that error return values are used"
  60. @echo " make astscan : GO AST scanner"
  61. @echo ""
  62. @echo " make docs : Generate source code documentation"
  63. @echo ""
  64. @echo " make deps : Get the dependencies"
  65. @echo " make clean : Remove any build artifact"
  66. @echo " make nuke : Deletes any intermediate file"
  67. @echo ""
  68. # Alias for help target
  69. all: help
  70. # Run the unit tests
  71. test:
  72. @mkdir -p target/test
  73. @mkdir -p target/report
  74. GOPATH=$(GOPATH) \
  75. go test \
  76. -covermode=atomic \
  77. -bench=. \
  78. -race \
  79. -cpuprofile=target/report/cpu.out \
  80. -memprofile=target/report/mem.out \
  81. -mutexprofile=target/report/mutex.out \
  82. -coverprofile=target/report/coverage.out \
  83. -v ./... | \
  84. tee >(PATH=$(GOPATH)/bin:$(PATH) go-junit-report > target/test/report.xml); \
  85. test $${PIPESTATUS[0]} -eq 0
  86. # Format the source code
  87. format:
  88. @find . -type f -name "*.go" -exec gofmt -s -w {} \;
  89. # Check if the source code has been formatted
  90. fmtcheck:
  91. @mkdir -p target
  92. @find . -type f -name "*.go" -exec gofmt -s -d {} \; | tee target/format.diff
  93. @test ! -s target/format.diff || { echo "ERROR: the source code has not been formatted - please use 'make format' or 'gofmt'"; exit 1; }
  94. # Check for syntax errors
  95. vet:
  96. GOPATH=$(GOPATH) go vet .
  97. # Check for style errors
  98. lint:
  99. GOPATH=$(GOPATH) PATH=$(GOPATH)/bin:$(PATH) golint .
  100. # Generate the coverage report
  101. coverage:
  102. @mkdir -p target/report
  103. GOPATH=$(GOPATH) \
  104. go tool cover -html=target/report/coverage.out -o target/report/coverage.html
  105. # Report cyclomatic complexity
  106. cyclo:
  107. @mkdir -p target/report
  108. GOPATH=$(GOPATH) gocyclo -avg ./ | tee target/report/cyclo.txt ; test $${PIPESTATUS[0]} -eq 0
  109. # Detect commonly misspelled words in source files
  110. misspell:
  111. @mkdir -p target/report
  112. GOPATH=$(GOPATH) misspell -error ./ | tee target/report/misspell.txt ; test $${PIPESTATUS[0]} -eq 0
  113. # Check that error return values are used
  114. errcheck:
  115. @mkdir -p target/report
  116. GOPATH=$(GOPATH) errcheck ./ | tee target/report/errcheck.txt
  117. # staticcheck
  118. staticcheck:
  119. @mkdir -p target/report
  120. GOPATH=$(GOPATH) staticcheck ./... | tee target/report/staticcheck.txt
  121. # AST scanner
  122. astscan:
  123. @mkdir -p target/report
  124. GOPATH=$(GOPATH) gas .//*.go | tee target/report/astscan.txt
  125. # Generate source docs
  126. docs:
  127. @mkdir -p target/docs
  128. nohup sh -c 'GOPATH=$(GOPATH) godoc -http=127.0.0.1:6060' > target/godoc_server.log 2>&1 &
  129. 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`
  130. @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
  131. # Alias to run all quality-assurance checks
  132. qa: fmtcheck test vet lint coverage cyclo misspell errcheck astscan
  133. # --- INSTALL ---
  134. # Get the dependencies
  135. deps:
  136. GOPATH=$(GOPATH) go get ./...
  137. GOPATH=$(GOPATH) go get golang.org/x/lint/golint
  138. GOPATH=$(GOPATH) go get github.com/jstemmer/go-junit-report
  139. GOPATH=$(GOPATH) go get github.com/axw/gocov/gocov
  140. GOPATH=$(GOPATH) go get github.com/fzipp/gocyclo
  141. GOPATH=$(GOPATH) go get github.com/gordonklaus/ineffassign
  142. GOPATH=$(GOPATH) go get github.com/client9/misspell/cmd/misspell
  143. GOPATH=$(GOPATH) go get github.com/opennota/check/cmd/structcheck
  144. GOPATH=$(GOPATH) go get github.com/opennota/check/cmd/varcheck
  145. GOPATH=$(GOPATH) go get github.com/kisielk/errcheck
  146. GOPATH=$(GOPATH) go get honnef.co/go/tools/cmd/staticcheck
  147. GOPATH=$(GOPATH) go get github.com/GoASTScanner/gas
  148. # Remove any build artifact
  149. clean:
  150. GOPATH=$(GOPATH) go clean ./...
  151. # Deletes any intermediate file
  152. nuke:
  153. rm -rf ./target
  154. GOPATH=$(GOPATH) go clean -i ./...