Makefile 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. PKG?=github.com/smallstep/ca-component/cmd/step-ca
  2. BINNAME?=step-ca
  3. # Set V to 1 for verbose output from the Makefile
  4. Q=$(if $V,,@)
  5. PREFIX?=
  6. SRC=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
  7. GOOS_OVERRIDE ?=
  8. # Set shell to bash for `echo -e`
  9. SHELL := /bin/bash
  10. all: build lint test
  11. .PHONY: all
  12. #########################################
  13. # Bootstrapping
  14. #########################################
  15. bootstra%:
  16. $Q which dep || go get github.com/golang/dep/cmd/dep
  17. $Q dep ensure
  18. vendor: Gopkg.lock
  19. $Q dep ensure
  20. BOOTSTRAP=\
  21. github.com/golang/lint/golint \
  22. github.com/client9/misspell/cmd/misspell \
  23. github.com/gordonklaus/ineffassign \
  24. github.com/tsenart/deadcode \
  25. github.com/alecthomas/gometalinter
  26. define VENDOR_BIN_TMPL
  27. vendor/bin/$(notdir $(1)): vendor
  28. $Q go build -o $$@ ./vendor/$(1)
  29. VENDOR_BINS += vendor/bin/$(notdir $(1))
  30. endef
  31. $(foreach pkg,$(BOOTSTRAP),$(eval $(call VENDOR_BIN_TMPL,$(pkg))))
  32. .PHONY: bootstra% vendor
  33. #################################################
  34. # Determine the type of `push` and `version`
  35. #################################################
  36. # Version flags to embed in the binaries
  37. VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
  38. VERSION := $(shell echo $(VERSION) | sed 's/^v//')
  39. # If TRAVIS_TAG is set then we know this ref has been tagged.
  40. ifdef TRAVIS_TAG
  41. PUSHTYPE=release
  42. else
  43. PUSHTYPE=master
  44. endif
  45. #########################################
  46. # Build
  47. #########################################
  48. DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
  49. LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  50. GOFLAGS := CGO_ENABLED=0
  51. build: $(PREFIX)bin/$(BINNAME)
  52. @echo "Build Complete!"
  53. $(PREFIX)bin/$(BINNAME): vendor $(call rwildcard,*.go)
  54. $Q mkdir -p $(@D)
  55. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  56. # Target for building without calling dep ensure
  57. simple:
  58. $Q mkdir -p bin/
  59. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o bin/$(BINNAME) $(LDFLAGS) $(PKG)
  60. @echo "Build Complete!"
  61. .PHONY: build simple
  62. #########################################
  63. # Go generate
  64. #########################################
  65. generate:
  66. $Q go generate ./...
  67. .PHONY: generate
  68. #########################################
  69. # Test
  70. #########################################
  71. test:
  72. $Q $(GOFLAGS) go test -short -cover ./...
  73. vtest:
  74. $(Q)for d in $$(go list ./... | grep -v vendor); do \
  75. echo -e "TESTS FOR: for \033[0;35m$$d\033[0m"; \
  76. $(GOFLAGS) go test -v -bench=. -run=. -short -coverprofile=profile.coverage.out -covermode=atomic $$d; \
  77. out=$$?; \
  78. if [[ $$out -ne 0 ]]; then ret=$$out; fi;\
  79. rm -f profile.coverage.out; \
  80. done; exit $$ret;
  81. .PHONY: test vtest
  82. integrate: integration
  83. integration: bin/$(BINNAME)
  84. $Q $(GOFLAGS) go test -tags=integration ./integration/...
  85. .PHONY: integrate integration
  86. #########################################
  87. # Linting
  88. #########################################
  89. LINTERS=\
  90. gofmt \
  91. golint \
  92. vet \
  93. misspell \
  94. ineffassign \
  95. deadcode
  96. $(patsubst %,%-bin,$(filter-out gofmt vet,$(LINTERS))): %-bin: vendor/bin/%
  97. gofmt-bin vet-bin:
  98. $(LINTERS): %: vendor/bin/gometalinter %-bin vendor
  99. $Q PATH=`pwd`/vendor/bin:$$PATH gometalinter --tests --disable-all --vendor \
  100. --deadline=5m -s data -s pkg --enable $@ ./...
  101. fmt:
  102. $Q gofmt -l -w $(SRC)
  103. lint: $(LINTERS)
  104. .PHONY: $(LINTERS) lint fmt
  105. #########################################
  106. # Install
  107. #########################################
  108. INSTALL_PREFIX?=/usr/
  109. install: $(PREFIX)bin/$(BINNAME)
  110. $Q install -D $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
  111. uninstall:
  112. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
  113. .PHONY: install uninstall
  114. #########################################
  115. # Debian
  116. #########################################
  117. debian:
  118. $Q mkdir -p $(RELEASE); \
  119. OUTPUT=../step-ca_*.deb; \
  120. rm $$OUTPUT; \
  121. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  122. distclean: clean
  123. .PHONY: debian distclean
  124. #################################################
  125. # Build statically compiled step binary for various operating systems
  126. #################################################
  127. OUTPUT_ROOT=output/
  128. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  129. BUNDLE_MAKE=v=$v GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2)' PREFIX=$(3) make $(3)bin/step
  130. RELEASE=./.travis-releases
  131. binary-linux:
  132. $(call BUNDLE_MAKE,linux,amd64,$(BINARY_OUTPUT)linux/)
  133. binary-darwin:
  134. $(call BUNDLE_MAKE,darwin,amd64,$(BINARY_OUTPUT)darwin/)
  135. define BUNDLE
  136. $(q)BUNDLE_DIR=$(BINARY_OUTPUT)$(1)/bundle; \
  137. stepName=step_$(2); \
  138. mkdir -p $$BUNDLE_DIR $(RELEASE); \
  139. TMP=$$(mktemp -d $$BUNDLE_DIR/tmp.XXXX); \
  140. trap "rm -rf $$TMP" EXIT INT QUIT TERM; \
  141. newdir=$$TMP/$$stepName; \
  142. mkdir -p $$newdir/bin; \
  143. cp $(BINARY_OUTPUT)$(1)/bin/step $$newdir/bin/; \
  144. cp README.md $$newdir/; \
  145. NEW_BUNDLE=$(RELEASE)/step_$(2)_$(1)_$(3).tar.gz; \
  146. rm -f $$NEW_BUNDLE; \
  147. tar -zcvf $$NEW_BUNDLE -C $$TMP $$stepName;
  148. endef
  149. bundle-linux: binary-linux
  150. $(call BUNDLE,linux,$(VERSION),amd64)
  151. bundle-darwin: binary-darwin
  152. $(call BUNDLE,darwin,$(VERSION),amd64)
  153. .PHONY: binary-linux binary-darwin bundle-linux bundle-darwin
  154. #################################################
  155. # Targets for creating OS specific artifacts
  156. #################################################
  157. artifacts-linux-tag: bundle-linux debian
  158. artifacts-darwin-tag: bundle-darwin
  159. artifacts-tag: artifacts-linux-tag artifacts-darwin-tag
  160. .PHONY: artifacts-linux-tag artifacts-darwin-tag artifacts-tag
  161. #################################################
  162. # Targets for creating step artifacts
  163. #################################################
  164. # For all builds that are not tagged
  165. artifacts-master:
  166. # For all builds with a release tag
  167. artifacts-release: artifacts-tag
  168. # This command is called by travis directly *after* a successful build
  169. artifacts: artifacts-$(PUSHTYPE)
  170. .PHONY: artifacts-master artifacts-release artifacts
  171. #########################################
  172. # Clean
  173. #########################################
  174. clean:
  175. @echo "You will need to run 'make bootstrap' or 'dep ensure' directly to re-download any dependencies."
  176. $Q rm -rf vendor
  177. ifneq ($(BINNAME),"")
  178. $Q rm -f bin/$(BINNAME)
  179. endif
  180. .PHONY: clean