Makefile 9.2 KB

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