Makefile 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. PKG?=github.com/smallstep/certificates/cmd/step-ca
  2. BINNAME?=step-ca
  3. CLOUDKMS_BINNAME?=step-cloudkms-init
  4. CLOUDKMS_PKG?=github.com/smallstep/certificates/cmd/step-cloudkms-init
  5. AWSKMS_BINNAME?=step-awskms-init
  6. AWSKMS_PKG?=github.com/smallstep/certificates/cmd/step-awskms-init
  7. YUBIKEY_BINNAME?=step-yubikey-init
  8. YUBIKEY_PKG?=github.com/smallstep/certificates/cmd/step-yubikey-init
  9. PKCS11_BINNAME?=step-pkcs11-init
  10. PKCS11_PKG?=github.com/smallstep/certificates/cmd/step-pkcs11-init
  11. # Set V to 1 for verbose output from the Makefile
  12. Q=$(if $V,,@)
  13. PREFIX?=
  14. SRC=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
  15. GOOS_OVERRIDE ?=
  16. OUTPUT_ROOT=output/
  17. all: lint test build
  18. travis: lintcgo testcgo build
  19. .PHONY: all travis
  20. #########################################
  21. # Bootstrapping
  22. #########################################
  23. bootstra%:
  24. # Using a released version of golangci-lint to take into account custom replacements in their go.mod
  25. $Q GO111MODULE=on go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.24.0
  26. .PHONY: bootstra%
  27. #################################################
  28. # Determine the type of `push` and `version`
  29. #################################################
  30. # If TRAVIS_TAG is set then we know this ref has been tagged.
  31. ifdef TRAVIS_TAG
  32. VERSION := $(TRAVIS_TAG)
  33. NOT_RC := $(shell echo $(VERSION) | grep -v -e -rc)
  34. ifeq ($(NOT_RC),)
  35. PUSHTYPE := release-candidate
  36. else
  37. PUSHTYPE := release
  38. endif
  39. else
  40. VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
  41. # If we are not in an active git dir then try reading the version from .VERSION.
  42. # .VERSION contains a slug populated by `git archive`.
  43. VERSION := $(or $(VERSION),$(shell ./.version.sh .VERSION))
  44. ifeq ($(TRAVIS_BRANCH),master)
  45. PUSHTYPE := master
  46. else
  47. PUSHTYPE := branch
  48. endif
  49. endif
  50. VERSION := $(shell echo $(VERSION) | sed 's/^v//')
  51. DEB_VERSION := $(shell echo $(VERSION) | sed 's/-/~/g')
  52. ifdef V
  53. $(info TRAVIS_TAG is $(TRAVIS_TAG))
  54. $(info VERSION is $(VERSION))
  55. $(info DEB_VERSION is $(DEB_VERSION))
  56. $(info PUSHTYPE is $(PUSHTYPE))
  57. endif
  58. include make/docker.mk
  59. #########################################
  60. # Build
  61. #########################################
  62. DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
  63. LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  64. GOFLAGS := CGO_ENABLED=0
  65. download:
  66. $Q go mod download
  67. build: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(PREFIX)bin/$(AWSKMS_BINNAME) $(PREFIX)bin/$(YUBIKEY_BINNAME) $(PREFIX)bin/$(PKCS11_BINNAME)
  68. @echo "Build Complete!"
  69. $(PREFIX)bin/$(BINNAME): download $(call rwildcard,*.go)
  70. $Q mkdir -p $(@D)
  71. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  72. $(PREFIX)bin/$(CLOUDKMS_BINNAME): download $(call rwildcard,*.go)
  73. $Q mkdir -p $(@D)
  74. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(LDFLAGS) $(CLOUDKMS_PKG)
  75. $(PREFIX)bin/$(AWSKMS_BINNAME): download $(call rwildcard,*.go)
  76. $Q mkdir -p $(@D)
  77. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(AWSKMS_BINNAME) $(LDFLAGS) $(AWSKMS_PKG)
  78. $(PREFIX)bin/$(YUBIKEY_BINNAME): download $(call rwildcard,*.go)
  79. $Q mkdir -p $(@D)
  80. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(YUBIKEY_BINNAME) $(LDFLAGS) $(YUBIKEY_PKG)
  81. $(PREFIX)bin/$(PKCS11_BINNAME): download $(call rwildcard,*.go)
  82. $Q mkdir -p $(@D)
  83. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(PKCS11_BINNAME) $(LDFLAGS) $(PKCS11_PKG)
  84. # Target to force a build of step-ca without running tests
  85. simple: build
  86. .PHONY: download build simple
  87. #########################################
  88. # Go generate
  89. #########################################
  90. generate:
  91. $Q go generate ./...
  92. .PHONY: generate
  93. #########################################
  94. # Test
  95. #########################################
  96. test:
  97. $Q $(GOFLAGS) go test -short -coverprofile=coverage.out ./...
  98. testcgo:
  99. $Q go test -short -coverprofile=coverage.out ./...
  100. .PHONY: test testcgo
  101. integrate: integration
  102. integration: bin/$(BINNAME)
  103. $Q $(GOFLAGS) go test -tags=integration ./integration/...
  104. .PHONY: integrate integration
  105. #########################################
  106. # Linting
  107. #########################################
  108. fmt:
  109. $Q gofmt -l -w $(SRC)
  110. lint:
  111. $Q $(GOFLAGS) LOG_LEVEL=error golangci-lint run --timeout=30m
  112. lintcgo:
  113. $Q LOG_LEVEL=error golangci-lint run --timeout=30m
  114. .PHONY: fmt lint lintcgo
  115. #########################################
  116. # Install
  117. #########################################
  118. INSTALL_PREFIX?=/usr/
  119. install: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(PREFIX)bin/$(AWSKMS_BINNAME)
  120. $Q install -D $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
  121. $Q install -D $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(CLOUDKMS_BINNAME)
  122. $Q install -D $(PREFIX)bin/$(AWSKMS_BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(AWSKMS_BINNAME)
  123. uninstall:
  124. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
  125. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(CLOUDKMS_BINNAME)
  126. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(AWSKMS_BINNAME)
  127. .PHONY: install uninstall
  128. #########################################
  129. # Clean
  130. #########################################
  131. clean:
  132. ifneq ($(BINNAME),"")
  133. $Q rm -f bin/$(BINNAME)
  134. endif
  135. ifneq ($(CLOUDKMS_BINNAME),"")
  136. $Q rm -f bin/$(CLOUDKMS_BINNAME)
  137. endif
  138. ifneq ($(AWSKMS_BINNAME),"")
  139. $Q rm -f bin/$(AWSKMS_BINNAME)
  140. endif
  141. ifneq ($(YUBIKEY_BINNAME),"")
  142. $Q rm -f bin/$(YUBIKEY_BINNAME)
  143. endif
  144. ifneq ($(PKCS11_BINNAME),"")
  145. $Q rm -f bin/$(PKCS11_BINNAME)
  146. endif
  147. .PHONY: clean
  148. #########################################
  149. # Dev
  150. #########################################
  151. run:
  152. $Q go run cmd/step-ca/main.go $(shell step path)/config/ca.json
  153. .PHONY: run
  154. #########################################
  155. # Debian
  156. #########################################
  157. changelog:
  158. $Q echo "step-certificates ($(DEB_VERSION)) unstable; urgency=medium" > debian/changelog
  159. $Q echo >> debian/changelog
  160. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  161. $Q echo >> debian/changelog
  162. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  163. debian: changelog
  164. $Q mkdir -p $(RELEASE); \
  165. OUTPUT=../step-certificates_*.deb; \
  166. rm $$OUTPUT; \
  167. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  168. distclean: clean
  169. .PHONY: changelog debian distclean
  170. #################################################
  171. # Build statically compiled step binary for various operating systems
  172. #################################################
  173. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  174. RELEASE=./.travis-releases
  175. define BUNDLE_MAKE
  176. # $(1) -- Go Operating System (e.g. linux, darwin, windows, etc.)
  177. # $(2) -- Go Architecture (e.g. amd64, arm, arm64, etc.)
  178. # $(3) -- Go ARM architectural family (e.g. 7, 8, etc.)
  179. # $(4) -- Parent directory for executables generated by 'make'.
  180. $(q) GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2) GOARM=$(3)' PREFIX=$(4) make $(4)bin/$(BINNAME) $(4)bin/$(CLOUDKMS_BINNAME) $(4)bin/$(AWSKMS_BINNAME)
  181. endef
  182. binary-linux:
  183. $(call BUNDLE_MAKE,linux,amd64,,$(BINARY_OUTPUT)linux/)
  184. binary-linux-arm64:
  185. $(call BUNDLE_MAKE,linux,arm64,,$(BINARY_OUTPUT)linux.arm64/)
  186. binary-linux-armv7:
  187. $(call BUNDLE_MAKE,linux,arm,7,$(BINARY_OUTPUT)linux.armv7/)
  188. binary-darwin:
  189. $(call BUNDLE_MAKE,darwin,amd64,,$(BINARY_OUTPUT)darwin/)
  190. define BUNDLE
  191. # $(1) -- Binary Output Dir Name
  192. # $(2) -- Step Platform Name
  193. # $(3) -- Step Binary Architecture
  194. # $(4) -- Step Binary Name (For Windows Comaptibility)
  195. $(q) ./make/bundle.sh "$(BINARY_OUTPUT)$(1)" "$(RELEASE)" "$(VERSION)" "$(2)" "$(3)" "$(4)" "$(5)" "$(6)"
  196. endef
  197. bundle-linux: binary-linux binary-linux-arm64 binary-linux-armv7
  198. $(call BUNDLE,linux,linux,amd64,$(BINNAME),$(CLOUDKMS_BINNAME),$(AWSKMS_BINNAME))
  199. $(call BUNDLE,linux.arm64,linux,arm64,$(BINNAME),$(CLOUDKMS_BINNAME),$(AWSKMS_BINNAME))
  200. $(call BUNDLE,linux.armv7,linux,armv7,$(BINNAME),$(CLOUDKMS_BINNAME),$(AWSKMS_BINNAME))
  201. bundle-darwin: binary-darwin
  202. $(call BUNDLE,darwin,darwin,amd64,$(BINNAME),$(CLOUDKMS_BINNAME),$(AWSKMS_BINNAME))
  203. .PHONY: binary-linux binary-darwin bundle-linux bundle-darwin
  204. #################################################
  205. # Targets for creating step artifacts
  206. #################################################
  207. # This command is called by travis directly *after* a successful build
  208. docker-artifacts: docker-$(PUSHTYPE)
  209. .PHONY: docker-artifacts