Makefile 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. ci: testcgo build
  19. .PHONY: all ci
  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 curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.39.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. # GITHUB Actions
  40. else ifdef GITHUB_REF
  41. VERSION ?= $(shell echo $(GITHUB_REF) | sed 's/^refs\/tags\///')
  42. NOT_RC := $(shell echo $(VERSION) | grep -v -e -rc)
  43. ifeq ($(NOT_RC),)
  44. PUSHTYPE := release-candidate
  45. else
  46. PUSHTYPE := release
  47. endif
  48. else
  49. VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
  50. # If we are not in an active git dir then try reading the version from .VERSION.
  51. # .VERSION contains a slug populated by `git archive`.
  52. VERSION := $(or $(VERSION),$(shell ./.version.sh .VERSION))
  53. ifeq ($(TRAVIS_BRANCH),master)
  54. PUSHTYPE := master
  55. else
  56. PUSHTYPE := branch
  57. endif
  58. endif
  59. VERSION := $(shell echo $(VERSION) | sed 's/^v//')
  60. DEB_VERSION := $(shell echo $(VERSION) | sed 's/-/~/g')
  61. ifdef V
  62. $(info TRAVIS_TAG is $(TRAVIS_TAG))
  63. $(info GITHUB_REF is $(GITHUB_REF))
  64. $(info VERSION is $(VERSION))
  65. $(info DEB_VERSION is $(DEB_VERSION))
  66. $(info PUSHTYPE is $(PUSHTYPE))
  67. endif
  68. include make/docker.mk
  69. #########################################
  70. # Build
  71. #########################################
  72. DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
  73. LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  74. GOFLAGS := CGO_ENABLED=0
  75. download:
  76. $Q go mod download
  77. build: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(PREFIX)bin/$(AWSKMS_BINNAME) $(PREFIX)bin/$(YUBIKEY_BINNAME) $(PREFIX)bin/$(PKCS11_BINNAME)
  78. @echo "Build Complete!"
  79. $(PREFIX)bin/$(BINNAME): download $(call rwildcard,*.go)
  80. $Q mkdir -p $(@D)
  81. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
  82. $(PREFIX)bin/$(CLOUDKMS_BINNAME): download $(call rwildcard,*.go)
  83. $Q mkdir -p $(@D)
  84. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(LDFLAGS) $(CLOUDKMS_PKG)
  85. $(PREFIX)bin/$(AWSKMS_BINNAME): download $(call rwildcard,*.go)
  86. $Q mkdir -p $(@D)
  87. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(AWSKMS_BINNAME) $(LDFLAGS) $(AWSKMS_PKG)
  88. $(PREFIX)bin/$(YUBIKEY_BINNAME): download $(call rwildcard,*.go)
  89. $Q mkdir -p $(@D)
  90. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(YUBIKEY_BINNAME) $(LDFLAGS) $(YUBIKEY_PKG)
  91. $(PREFIX)bin/$(PKCS11_BINNAME): download $(call rwildcard,*.go)
  92. $Q mkdir -p $(@D)
  93. $Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(PKCS11_BINNAME) $(LDFLAGS) $(PKCS11_PKG)
  94. # Target to force a build of step-ca without running tests
  95. simple: build
  96. .PHONY: download build simple
  97. #########################################
  98. # Go generate
  99. #########################################
  100. generate:
  101. $Q go generate ./...
  102. .PHONY: generate
  103. #########################################
  104. # Test
  105. #########################################
  106. test:
  107. $Q $(GOFLAGS) go test -short -coverprofile=coverage.out ./...
  108. testcgo:
  109. $Q go test -short -coverprofile=coverage.out ./...
  110. .PHONY: test testcgo
  111. integrate: integration
  112. integration: bin/$(BINNAME)
  113. $Q $(GOFLAGS) go test -tags=integration ./integration/...
  114. .PHONY: integrate integration
  115. #########################################
  116. # Linting
  117. #########################################
  118. fmt:
  119. $Q gofmt -l -w $(SRC)
  120. lint:
  121. $Q $(GOFLAGS) LOG_LEVEL=error golangci-lint run --timeout=30m
  122. lintcgo:
  123. $Q LOG_LEVEL=error golangci-lint run --timeout=30m
  124. .PHONY: fmt lint lintcgo
  125. #########################################
  126. # Install
  127. #########################################
  128. INSTALL_PREFIX?=/usr/
  129. install: $(PREFIX)bin/$(BINNAME) $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(PREFIX)bin/$(AWSKMS_BINNAME)
  130. $Q install -D $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
  131. $Q install -D $(PREFIX)bin/$(CLOUDKMS_BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(CLOUDKMS_BINNAME)
  132. $Q install -D $(PREFIX)bin/$(AWSKMS_BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(AWSKMS_BINNAME)
  133. uninstall:
  134. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
  135. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(CLOUDKMS_BINNAME)
  136. $Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(AWSKMS_BINNAME)
  137. .PHONY: install uninstall
  138. #########################################
  139. # Clean
  140. #########################################
  141. clean:
  142. ifneq ($(BINNAME),"")
  143. $Q rm -f bin/$(BINNAME)
  144. endif
  145. ifneq ($(CLOUDKMS_BINNAME),"")
  146. $Q rm -f bin/$(CLOUDKMS_BINNAME)
  147. endif
  148. ifneq ($(AWSKMS_BINNAME),"")
  149. $Q rm -f bin/$(AWSKMS_BINNAME)
  150. endif
  151. ifneq ($(YUBIKEY_BINNAME),"")
  152. $Q rm -f bin/$(YUBIKEY_BINNAME)
  153. endif
  154. ifneq ($(PKCS11_BINNAME),"")
  155. $Q rm -f bin/$(PKCS11_BINNAME)
  156. endif
  157. .PHONY: clean
  158. #########################################
  159. # Dev
  160. #########################################
  161. run:
  162. $Q go run cmd/step-ca/main.go $(shell step path)/config/ca.json
  163. .PHONY: run
  164. #########################################
  165. # Debian
  166. #########################################
  167. changelog:
  168. $Q echo "step-ca ($(DEB_VERSION)) unstable; urgency=medium" > debian/changelog
  169. $Q echo >> debian/changelog
  170. $Q echo " * See https://github.com/smallstep/certificates/releases" >> debian/changelog
  171. $Q echo >> debian/changelog
  172. $Q echo " -- Smallstep Labs, Inc. <techadmin@smallstep.com> $(shell date -uR)" >> debian/changelog
  173. debian: changelog
  174. $Q mkdir -p $(RELEASE); \
  175. OUTPUT=../step-ca*.deb; \
  176. rm $$OUTPUT; \
  177. dpkg-buildpackage -b -rfakeroot -us -uc && cp $$OUTPUT $(RELEASE)/
  178. distclean: clean
  179. .PHONY: changelog debian distclean
  180. #################################################
  181. # Build statically compiled step binary for various operating systems
  182. #################################################
  183. BINARY_OUTPUT=$(OUTPUT_ROOT)binary/
  184. RELEASE=./.releases
  185. define BUNDLE_MAKE
  186. # $(1) -- Go Operating System (e.g. linux, darwin, windows, etc.)
  187. # $(2) -- Go Architecture (e.g. amd64, arm, arm64, etc.)
  188. # $(3) -- Go ARM architectural family (e.g. 7, 8, etc.)
  189. # $(4) -- Parent directory for executables generated by 'make'.
  190. $(q) GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2) GOARM=$(3)' PREFIX=$(4) make $(4)bin/$(BINNAME) $(4)bin/$(CLOUDKMS_BINNAME) $(4)bin/$(AWSKMS_BINNAME)
  191. endef
  192. binary-linux:
  193. $(call BUNDLE_MAKE,linux,amd64,,$(BINARY_OUTPUT)linux/)
  194. binary-linux-arm64:
  195. $(call BUNDLE_MAKE,linux,arm64,,$(BINARY_OUTPUT)linux.arm64/)
  196. binary-linux-armv7:
  197. $(call BUNDLE_MAKE,linux,arm,7,$(BINARY_OUTPUT)linux.armv7/)
  198. binary-darwin:
  199. $(call BUNDLE_MAKE,darwin,amd64,,$(BINARY_OUTPUT)darwin/)
  200. .PHONY: binary-linux binary-linux-arm64 binary-linux-armv7 binary-darwin
  201. #################################################
  202. # Targets for creating step artifacts
  203. #################################################
  204. docker-artifacts: docker-$(PUSHTYPE)
  205. .PHONY: docker-artifacts