4
0

Makefile 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. OUTPUT_ROOT=output/
  9. # Set shell to bash for `echo -e`
  10. SHELL := /bin/bash
  11. all: build lint test
  12. .PHONY: all
  13. #########################################
  14. # Bootstrapping
  15. #########################################
  16. bootstra%:
  17. $Q which dep || go get github.com/golang/dep/cmd/dep
  18. $Q dep ensure
  19. vendor: Gopkg.lock
  20. $Q dep ensure
  21. BOOTSTRAP=\
  22. github.com/golang/lint/golint \
  23. github.com/client9/misspell/cmd/misspell \
  24. github.com/gordonklaus/ineffassign \
  25. github.com/tsenart/deadcode \
  26. github.com/alecthomas/gometalinter
  27. define VENDOR_BIN_TMPL
  28. vendor/bin/$(notdir $(1)): vendor
  29. $Q go build -o $$@ ./vendor/$(1)
  30. VENDOR_BINS += vendor/bin/$(notdir $(1))
  31. endef
  32. $(foreach pkg,$(BOOTSTRAP),$(eval $(call VENDOR_BIN_TMPL,$(pkg))))
  33. .PHONY: bootstra% vendor
  34. #################################################
  35. # Determine the type of `push` and `version`
  36. #################################################
  37. # Version flags to embed in the binaries
  38. VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
  39. VERSION := $(shell echo $(VERSION) | sed 's/^v//')
  40. # If TRAVIS_TAG is set then we know this ref has been tagged.
  41. ifdef TRAVIS_TAG
  42. PUSHTYPE=release
  43. else
  44. PUSHTYPE=master
  45. endif
  46. #########################################
  47. # Build
  48. #########################################
  49. DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
  50. LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  51. GOFLAGS := CGO_ENABLED=0
  52. build: $(PREFIX)bin/$(BINNAME)
  53. @echo "Build Complete!"
  54. $(PREFIX)bin/$(BINNAME): vendor $(call rwildcard,*.go)
  55. $Q mkdir -p $(@D)
  56. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  57. # Target for building without calling dep ensure
  58. simple:
  59. $Q mkdir -p bin/
  60. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o bin/$(BINNAME) $(LDFLAGS) $(PKG)
  61. @echo "Build Complete!"
  62. .PHONY: build simple
  63. #########################################
  64. # Go generate
  65. #########################################
  66. generate:
  67. $Q go generate ./...
  68. .PHONY: generate
  69. #########################################
  70. # Test
  71. #########################################
  72. test:
  73. $Q $(GOFLAGS) go test -short -coverprofile=coverage.out ./...
  74. vtest:
  75. $(Q)for d in $$(go list ./... | grep -v vendor); do \
  76. echo -e "TESTS FOR: for \033[0;35m$$d\033[0m"; \
  77. $(GOFLAGS) go test -v -bench=. -run=. -short -coverprofile=coverage.out $$d; \
  78. out=$$?; \
  79. if [[ $$out -ne 0 ]]; then ret=$$out; fi;\
  80. rm -f profile.coverage.out; \
  81. done; exit $$ret;
  82. .PHONY: test vtest
  83. integrate: integration
  84. integration: bin/$(BINNAME)
  85. $Q $(GOFLAGS) go test -tags=integration ./integration/...
  86. .PHONY: integrate integration
  87. #########################################
  88. # Linting
  89. #########################################
  90. LINTERS=\
  91. gofmt \
  92. golint \
  93. vet \
  94. misspell \
  95. ineffassign \
  96. deadcode
  97. $(patsubst %,%-bin,$(filter-out gofmt vet,$(LINTERS))): %-bin: vendor/bin/%
  98. gofmt-bin vet-bin:
  99. $(LINTERS): %: vendor/bin/gometalinter %-bin vendor
  100. $Q PATH=`pwd`/vendor/bin:$$PATH gometalinter --tests --disable-all --vendor \
  101. --deadline=5m -s data -s pkg --enable $@ ./...
  102. fmt:
  103. $Q gofmt -l -w $(SRC)
  104. lint: $(LINTERS)
  105. .PHONY: $(LINTERS) lint fmt
  106. #########################################
  107. # Install
  108. #########################################
  109. INSTALL_PREFIX?=/usr/
  110. install: $(PREFIX)bin/$(BINNAME)
  111. $Q install -D $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
  112. uninstall:
  113. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
  114. .PHONY: install uninstall
  115. #########################################
  116. # Building Docker Image
  117. #
  118. # Builds a dockerfile for step by building a linux version of the step-cli and
  119. # then copying the specific binary when building the container.
  120. #
  121. # This ensures the container is as small as possible without having to deal
  122. # with getting access to private repositories inside the container during build
  123. # time.
  124. #########################################
  125. # XXX We put the output for the build in 'output' so we don't mess with how we
  126. # do rule overriding from the base Makefile (if you name it 'build' it messes up
  127. # the wildcarding).
  128. DOCKER_OUTPUT=$(OUTPUT_ROOT)docker/
  129. DOCKER_MAKE=V=$V GOOS_OVERRIDE='GOOS=linux GOARCH=amd64' PREFIX=$(1) make $(1)bin/$(2)
  130. DOCKER_BUILD=$Q docker build -t smallstep/$(1):latest -f docker/$(2) --build-arg BINPATH=$(DOCKER_OUTPUT)bin/$(1) .
  131. docker: docker-make docker/Dockerfile.step-ca
  132. $(call DOCKER_BUILD,step-ca,Dockerfile.step-ca)
  133. docker-make:
  134. mkdir -p $(DOCKER_OUTPUT)
  135. $(call DOCKER_MAKE,$(DOCKER_OUTPUT),step-ca)
  136. .PHONY: docker docker-make
  137. #################################################
  138. # Releasing Docker Images
  139. #
  140. # Using the docker build infrastructure, this section is responsible for
  141. # logging into docker hub and pushing the built docker containers up with the
  142. # appropriate tags.
  143. #################################################
  144. DOCKER_TAG=docker tag smallstep/$(1):latest smallstep/$(1):$(2)
  145. DOCKER_PUSH=docker push smallstep/$(1):$(2)
  146. docker-tag:
  147. $(call DOCKER_TAG,step-ca,$(VERSION))
  148. docker-push-tag: docker-tag
  149. $(call DOCKER_PUSH,step-ca,$(VERSION))
  150. # Rely on DOCKER_USERNAME and DOCKER_PASSWORD being set inside the CI or
  151. # equivalent environment
  152. docker-login:
  153. $Q docker login -u="$(DOCKER_USERNAME)" -p="$(DOCKER_PASSWORD)"
  154. .PHONY: docker-login docker-tag docker-push-tag
  155. #################################################
  156. # Targets for pushing the docker images
  157. #################################################
  158. # For all builds on the master branch, we actually build the container
  159. docker-master: docker
  160. # For all builds on the master branch with an rc tag
  161. docker-release: docker-master docker-login docker-push-tag
  162. .PHONY: docker-master docker-release
  163. #########################################
  164. # Debian
  165. #########################################
  166. changelog:
  167. $Q echo "step-certificates ($(VERSION)) unstable; urgency=medium" > debian/changelog
  168. $Q echo >> debian/changelog
  169. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  170. $Q echo >> debian/changelog
  171. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  172. debian: changelog
  173. $Q mkdir -p $(RELEASE); \
  174. OUTPUT=../step-certificates_*.deb; \
  175. rm $$OUTPUT; \
  176. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  177. distclean: clean
  178. .PHONY: changelog debian distclean
  179. #################################################
  180. # Build statically compiled step binary for various operating systems
  181. #################################################
  182. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  183. BUNDLE_MAKE=v=$v GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2)' PREFIX=$(3) make $(3)bin/$(BINNAME)
  184. RELEASE=./.travis-releases
  185. binary-linux:
  186. $(call BUNDLE_MAKE,linux,amd64,$(BINARY_OUTPUT)linux/)
  187. binary-darwin:
  188. $(call BUNDLE_MAKE,darwin,amd64,$(BINARY_OUTPUT)darwin/)
  189. define BUNDLE
  190. $(q)BUNDLE_DIR=$(BINARY_OUTPUT)$(1)/bundle; \
  191. stepName=step-certificates_$(2); \
  192. mkdir -p $$BUNDLE_DIR $(RELEASE); \
  193. TMP=$$(mktemp -d $$BUNDLE_DIR/tmp.XXXX); \
  194. trap "rm -rf $$TMP" EXIT INT QUIT TERM; \
  195. newdir=$$TMP/$$stepName; \
  196. mkdir -p $$newdir/bin; \
  197. cp $(BINARY_OUTPUT)$(1)/bin/$(BINNAME) $$newdir/bin/; \
  198. cp README.md $$newdir/; \
  199. NEW_BUNDLE=$(RELEASE)/step-certificates_$(2)_$(1)_$(3).tar.gz; \
  200. rm -f $$NEW_BUNDLE; \
  201. tar -zcvf $$NEW_BUNDLE -C $$TMP $$stepName;
  202. endef
  203. bundle-linux: binary-linux
  204. $(call BUNDLE,linux,$(VERSION),amd64)
  205. bundle-darwin: binary-darwin
  206. $(call BUNDLE,darwin,$(VERSION),amd64)
  207. .PHONY: binary-linux binary-darwin bundle-linux bundle-darwin
  208. #################################################
  209. # Targets for creating OS specific artifacts
  210. #################################################
  211. artifacts-linux-tag: bundle-linux debian
  212. artifacts-darwin-tag: bundle-darwin
  213. artifacts-tag: artifacts-linux-tag artifacts-darwin-tag
  214. .PHONY: artifacts-linux-tag artifacts-darwin-tag artifacts-tag
  215. #################################################
  216. # Targets for creating step artifacts
  217. #################################################
  218. # For all builds that are not tagged
  219. artifacts-master:
  220. # For all builds with a release tag
  221. artifacts-release: artifacts-tag
  222. # This command is called by travis directly *after* a successful build
  223. artifacts: artifacts-$(PUSHTYPE) docker-$(PUSHTYPE)
  224. .PHONY: artifacts-master artifacts-release artifacts
  225. #########################################
  226. # Clean
  227. #########################################
  228. clean:
  229. @echo "You will need to run 'make bootstrap' or 'dep ensure' directly to re-download any dependencies."
  230. $Q rm -rf vendor
  231. ifneq ($(BINNAME),"")
  232. $Q rm -f bin/$(BINNAME)
  233. endif
  234. .PHONY: clean