Makefile 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. PKG?=github.com/smallstep/certificates/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 -coverprofile=coverage.out ./...
  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=coverage.out $$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. changelog:
  118. $Q echo "step-certificates ($(VERSION)) unstable; urgency=medium" > debian/changelog
  119. $Q echo >> debian/changelog
  120. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  121. $Q echo >> debian/changelog
  122. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  123. debian: changelog
  124. $Q mkdir -p $(RELEASE); \
  125. OUTPUT=../step-certificates_*.deb; \
  126. rm $$OUTPUT; \
  127. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  128. distclean: clean
  129. .PHONY: changelog debian distclean
  130. #################################################
  131. # Build statically compiled step binary for various operating systems
  132. #################################################
  133. OUTPUT_ROOT=output/
  134. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  135. BUNDLE_MAKE=v=$v GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2)' PREFIX=$(3) make $(3)bin/$(BINNAME)
  136. RELEASE=./.travis-releases
  137. binary-linux:
  138. $(call BUNDLE_MAKE,linux,amd64,$(BINARY_OUTPUT)linux/)
  139. binary-darwin:
  140. $(call BUNDLE_MAKE,darwin,amd64,$(BINARY_OUTPUT)darwin/)
  141. define BUNDLE
  142. $(q)BUNDLE_DIR=$(BINARY_OUTPUT)$(1)/bundle; \
  143. stepName=step-certificates_$(2); \
  144. mkdir -p $$BUNDLE_DIR $(RELEASE); \
  145. TMP=$$(mktemp -d $$BUNDLE_DIR/tmp.XXXX); \
  146. trap "rm -rf $$TMP" EXIT INT QUIT TERM; \
  147. newdir=$$TMP/$$stepName; \
  148. mkdir -p $$newdir/bin; \
  149. cp $(BINARY_OUTPUT)$(1)/bin/$(BINNAME) $$newdir/bin/; \
  150. cp README.md $$newdir/; \
  151. NEW_BUNDLE=$(RELEASE)/step-certificates_$(2)_$(1)_$(3).tar.gz; \
  152. rm -f $$NEW_BUNDLE; \
  153. tar -zcvf $$NEW_BUNDLE -C $$TMP $$stepName;
  154. endef
  155. bundle-linux: binary-linux
  156. $(call BUNDLE,linux,$(VERSION),amd64)
  157. bundle-darwin: binary-darwin
  158. $(call BUNDLE,darwin,$(VERSION),amd64)
  159. .PHONY: binary-linux binary-darwin bundle-linux bundle-darwin
  160. #################################################
  161. # Targets for creating OS specific artifacts
  162. #################################################
  163. artifacts-linux-tag: bundle-linux debian
  164. artifacts-darwin-tag: bundle-darwin
  165. artifacts-tag: artifacts-linux-tag artifacts-darwin-tag
  166. .PHONY: artifacts-linux-tag artifacts-darwin-tag artifacts-tag
  167. #################################################
  168. # Targets for creating step artifacts
  169. #################################################
  170. # For all builds that are not tagged
  171. artifacts-master:
  172. # For all builds with a release tag
  173. artifacts-release: artifacts-tag
  174. # This command is called by travis directly *after* a successful build
  175. artifacts: artifacts-$(PUSHTYPE)
  176. .PHONY: artifacts-master artifacts-release artifacts
  177. #########################################
  178. # Clean
  179. #########################################
  180. clean:
  181. @echo "You will need to run 'make bootstrap' or 'dep ensure' directly to re-download any dependencies."
  182. $Q rm -rf vendor
  183. ifneq ($(BINNAME),"")
  184. $Q rm -f bin/$(BINNAME)
  185. endif
  186. .PHONY: clean