Makefile 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # The targets cannot be run in parallel
  2. .NOTPARALLEL:
  3. VERSION := $(shell git describe --tags --always --match "[0-9][0-9][0-9][0-9].*.*")
  4. MSI_VERSION := $(shell git tag -l --sort=v:refname | grep "w" | tail -1 | cut -c2-)
  5. #MSI_VERSION expects the format of the tag to be: (wX.X.X). Starts with the w character to not break cfsetup.
  6. #e.g. w3.0.1 or w4.2.10. It trims off the w character when creating the MSI.
  7. ifeq ($(ORIGINAL_NAME), true)
  8. # Used for builds that want FIPS compilation but want the artifacts generated to still have the original name.
  9. BINARY_NAME := cloudflared
  10. else ifeq ($(FIPS), true)
  11. # Used for FIPS compliant builds that do not match the case above.
  12. BINARY_NAME := cloudflared-fips
  13. else
  14. # Used for all other (non-FIPS) builds.
  15. BINARY_NAME := cloudflared
  16. endif
  17. ifeq ($(NIGHTLY), true)
  18. DEB_PACKAGE_NAME := $(BINARY_NAME)-nightly
  19. NIGHTLY_FLAGS := --conflicts cloudflared --replaces cloudflared
  20. else
  21. DEB_PACKAGE_NAME := $(BINARY_NAME)
  22. endif
  23. DATE := $(shell date -u -r RELEASE_NOTES '+%Y-%m-%d-%H%M UTC')
  24. VERSION_FLAGS := -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"
  25. ifdef PACKAGE_MANAGER
  26. VERSION_FLAGS := $(VERSION_FLAGS) -X "github.com/cloudflare/cloudflared/cmd/cloudflared/updater.BuiltForPackageManager=$(PACKAGE_MANAGER)"
  27. endif
  28. ifdef CONTAINER_BUILD
  29. VERSION_FLAGS := $(VERSION_FLAGS) -X "github.com/cloudflare/cloudflared/metrics.Runtime=virtual"
  30. endif
  31. LINK_FLAGS :=
  32. ifeq ($(FIPS), true)
  33. LINK_FLAGS := -linkmode=external -extldflags=-static $(LINK_FLAGS)
  34. # Prevent linking with libc regardless of CGO enabled or not.
  35. GO_BUILD_TAGS := $(GO_BUILD_TAGS) osusergo netgo fips
  36. VERSION_FLAGS := $(VERSION_FLAGS) -X "main.BuildType=FIPS"
  37. endif
  38. LDFLAGS := -ldflags='$(VERSION_FLAGS) $(LINK_FLAGS)'
  39. ifneq ($(GO_BUILD_TAGS),)
  40. GO_BUILD_TAGS := -tags "$(GO_BUILD_TAGS)"
  41. endif
  42. ifeq ($(debug), 1)
  43. GO_BUILD_TAGS += -gcflags="all=-N -l"
  44. endif
  45. IMPORT_PATH := github.com/cloudflare/cloudflared
  46. PACKAGE_DIR := $(CURDIR)/packaging
  47. PREFIX := /usr
  48. INSTALL_BINDIR := $(PREFIX)/bin/
  49. INSTALL_MANDIR := $(PREFIX)/share/man/man1/
  50. LOCAL_ARCH ?= $(shell uname -m)
  51. ifneq ($(GOARCH),)
  52. TARGET_ARCH ?= $(GOARCH)
  53. else ifeq ($(LOCAL_ARCH),x86_64)
  54. TARGET_ARCH ?= amd64
  55. else ifeq ($(LOCAL_ARCH),amd64)
  56. TARGET_ARCH ?= amd64
  57. else ifeq ($(LOCAL_ARCH),i686)
  58. TARGET_ARCH ?= amd64
  59. else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 5),armv8)
  60. TARGET_ARCH ?= arm64
  61. else ifeq ($(LOCAL_ARCH),aarch64)
  62. TARGET_ARCH ?= arm64
  63. else ifeq ($(LOCAL_ARCH),arm64)
  64. TARGET_ARCH ?= arm64
  65. else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 4),armv)
  66. TARGET_ARCH ?= arm
  67. else ifeq ($(LOCAL_ARCH),s390x)
  68. TARGET_ARCH ?= s390x
  69. else
  70. $(error This system's architecture $(LOCAL_ARCH) isn't supported)
  71. endif
  72. LOCAL_OS ?= $(shell go env GOOS)
  73. ifeq ($(LOCAL_OS),linux)
  74. TARGET_OS ?= linux
  75. else ifeq ($(LOCAL_OS),darwin)
  76. TARGET_OS ?= darwin
  77. else ifeq ($(LOCAL_OS),windows)
  78. TARGET_OS ?= windows
  79. else ifeq ($(LOCAL_OS),freebsd)
  80. TARGET_OS ?= freebsd
  81. else ifeq ($(LOCAL_OS),openbsd)
  82. TARGET_OS ?= openbsd
  83. else
  84. $(error This system's OS $(LOCAL_OS) isn't supported)
  85. endif
  86. ifeq ($(TARGET_OS), windows)
  87. EXECUTABLE_PATH=./$(BINARY_NAME).exe
  88. else
  89. EXECUTABLE_PATH=./$(BINARY_NAME)
  90. endif
  91. ifeq ($(FLAVOR), centos-7)
  92. TARGET_PUBLIC_REPO ?= el7
  93. else
  94. TARGET_PUBLIC_REPO ?= $(FLAVOR)
  95. endif
  96. ifneq ($(TARGET_ARM), )
  97. ARM_COMMAND := GOARM=$(TARGET_ARM)
  98. endif
  99. ifeq ($(TARGET_ARM), 7)
  100. PACKAGE_ARCH := armhf
  101. else
  102. PACKAGE_ARCH := $(TARGET_ARCH)
  103. endif
  104. #for FIPS compliance, FPM defaults to MD5.
  105. RPM_DIGEST := --rpm-digest sha256
  106. .PHONY: all
  107. all: cloudflared test
  108. .PHONY: clean
  109. clean:
  110. go clean
  111. .PHONY: vulncheck
  112. vulncheck:
  113. @govulncheck ./...
  114. .PHONY: cloudflared
  115. cloudflared:
  116. ifeq ($(FIPS), true)
  117. $(info Building cloudflared with go-fips)
  118. endif
  119. GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) $(ARM_COMMAND) go build -mod=vendor $(GO_BUILD_TAGS) $(LDFLAGS) $(IMPORT_PATH)/cmd/cloudflared
  120. ifeq ($(FIPS), true)
  121. ./check-fips.sh cloudflared
  122. endif
  123. .PHONY: container
  124. container:
  125. docker build --build-arg=TARGET_ARCH=$(TARGET_ARCH) --build-arg=TARGET_OS=$(TARGET_OS) -t cloudflare/cloudflared-$(TARGET_OS)-$(TARGET_ARCH):"$(VERSION)" .
  126. .PHONY: generate-docker-version
  127. generate-docker-version:
  128. echo latest $(VERSION) > versions
  129. .PHONY: test
  130. test: vet
  131. ifndef CI
  132. go test -v -mod=vendor -race $(LDFLAGS) ./...
  133. else
  134. @mkdir -p .cover
  135. go test -v -mod=vendor -race $(LDFLAGS) -coverprofile=".cover/c.out" ./...
  136. endif
  137. .PHONY: cover
  138. cover:
  139. @echo ""
  140. @echo "=====> Total test coverage: <====="
  141. @echo ""
  142. # Print the overall coverage here for quick access.
  143. $Q go tool cover -func ".cover/c.out" | grep "total:" | awk '{print $$3}'
  144. # Generate the HTML report that can be viewed from the browser in CI.
  145. $Q go tool cover -html ".cover/c.out" -o .cover/all.html
  146. .PHONY: fuzz
  147. fuzz:
  148. @go test -fuzz=FuzzIPDecoder -fuzztime=600s ./packet
  149. @go test -fuzz=FuzzICMPDecoder -fuzztime=600s ./packet
  150. @go test -fuzz=FuzzSessionWrite -fuzztime=600s ./quic/v3
  151. @go test -fuzz=FuzzSessionServe -fuzztime=600s ./quic/v3
  152. @go test -fuzz=FuzzRegistrationDatagram -fuzztime=600s ./quic/v3
  153. @go test -fuzz=FuzzPayloadDatagram -fuzztime=600s ./quic/v3
  154. @go test -fuzz=FuzzRegistrationResponseDatagram -fuzztime=600s ./quic/v3
  155. @go test -fuzz=FuzzNewIdentity -fuzztime=600s ./tracing
  156. @go test -fuzz=FuzzNewAccessValidator -fuzztime=600s ./validation
  157. cloudflared.1: cloudflared_man_template
  158. sed -e 's/\$${VERSION}/$(VERSION)/; s/\$${DATE}/$(DATE)/' cloudflared_man_template > cloudflared.1
  159. install: cloudflared cloudflared.1
  160. mkdir -p $(DESTDIR)$(INSTALL_BINDIR) $(DESTDIR)$(INSTALL_MANDIR)
  161. install -m755 cloudflared $(DESTDIR)$(INSTALL_BINDIR)/cloudflared
  162. install -m644 cloudflared.1 $(DESTDIR)$(INSTALL_MANDIR)/cloudflared.1
  163. # When we build packages, the package name will be FIPS-aware.
  164. # But we keep the binary installed by it to be named "cloudflared" regardless.
  165. define build_package
  166. mkdir -p $(PACKAGE_DIR)
  167. cp cloudflared $(PACKAGE_DIR)/cloudflared
  168. cp cloudflared.1 $(PACKAGE_DIR)/cloudflared.1
  169. fpm -C $(PACKAGE_DIR) -s dir -t $(1) \
  170. --description 'Cloudflare Tunnel daemon' \
  171. --vendor 'Cloudflare' \
  172. --license 'Apache License Version 2.0' \
  173. --url 'https://github.com/cloudflare/cloudflared' \
  174. -m 'Cloudflare <support@cloudflare.com>' \
  175. -a $(PACKAGE_ARCH) -v $(VERSION) -n $(DEB_PACKAGE_NAME) $(RPM_DIGEST) $(NIGHTLY_FLAGS) --after-install postinst.sh --after-remove postrm.sh \
  176. cloudflared=$(INSTALL_BINDIR) cloudflared.1=$(INSTALL_MANDIR)
  177. endef
  178. .PHONY: cloudflared-deb
  179. cloudflared-deb: cloudflared cloudflared.1
  180. $(call build_package,deb)
  181. .PHONY: cloudflared-rpm
  182. cloudflared-rpm: cloudflared cloudflared.1
  183. $(call build_package,rpm)
  184. .PHONY: cloudflared-pkg
  185. cloudflared-pkg: cloudflared cloudflared.1
  186. $(call build_package,osxpkg)
  187. .PHONY: cloudflared-msi
  188. cloudflared-msi:
  189. wixl --define Version=$(VERSION) --define Path=$(EXECUTABLE_PATH) --output cloudflared-$(VERSION)-$(TARGET_ARCH).msi cloudflared.wxs
  190. .PHONY: github-release-dryrun
  191. github-release-dryrun:
  192. python3 github_release.py --path $(PWD)/built_artifacts --release-version $(VERSION) --dry-run
  193. .PHONY: github-release
  194. github-release:
  195. python3 github_release.py --path $(PWD)/built_artifacts --release-version $(VERSION)
  196. python3 github_message.py --release-version $(VERSION)
  197. .PHONY: macos-release
  198. macos-release:
  199. python3 github_release.py --path $(PWD)/artifacts/ --release-version $(VERSION)
  200. .PHONY: r2-linux-release
  201. r2-linux-release:
  202. python3 ./release_pkgs.py
  203. .PHONY: capnp
  204. capnp:
  205. which capnp # https://capnproto.org/install.html
  206. which capnpc-go # go install zombiezen.com/go/capnproto2/capnpc-go@latest
  207. capnp compile -ogo tunnelrpc/proto/tunnelrpc.capnp tunnelrpc/proto/quic_metadata_protocol.capnp
  208. .PHONY: vet
  209. vet:
  210. go vet -mod=vendor github.com/cloudflare/cloudflared/...
  211. .PHONY: fmt
  212. fmt:
  213. @goimports -l -w -local github.com/cloudflare/cloudflared $$(go list -mod=vendor -f '{{.Dir}}' -a ./... | fgrep -v tunnelrpc/proto)
  214. @go fmt $$(go list -mod=vendor -f '{{.Dir}}' -a ./... | fgrep -v tunnelrpc/proto)
  215. .PHONY: fmt-check
  216. fmt-check:
  217. @./fmt-check.sh
  218. .PHONY: lint
  219. lint:
  220. @golangci-lint run
  221. .PHONY: mocks
  222. mocks:
  223. go generate mocks/mockgen.go