Makefile 8.8 KB

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