Makefile 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 '+%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. CF_GO_PATH := /tmp/go
  51. PATH := $(CF_GO_PATH)/bin:$(PATH)
  52. LOCAL_ARCH ?= $(shell uname -m)
  53. ifneq ($(GOARCH),)
  54. TARGET_ARCH ?= $(GOARCH)
  55. else ifeq ($(LOCAL_ARCH),x86_64)
  56. TARGET_ARCH ?= amd64
  57. else ifeq ($(LOCAL_ARCH),amd64)
  58. TARGET_ARCH ?= amd64
  59. else ifeq ($(LOCAL_ARCH),i686)
  60. TARGET_ARCH ?= amd64
  61. else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 5),armv8)
  62. TARGET_ARCH ?= arm64
  63. else ifeq ($(LOCAL_ARCH),aarch64)
  64. TARGET_ARCH ?= arm64
  65. else ifeq ($(LOCAL_ARCH),arm64)
  66. TARGET_ARCH ?= arm64
  67. else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 4),armv)
  68. TARGET_ARCH ?= arm
  69. else ifeq ($(LOCAL_ARCH),s390x)
  70. TARGET_ARCH ?= s390x
  71. else
  72. $(error This system's architecture $(LOCAL_ARCH) isn't supported)
  73. endif
  74. LOCAL_OS ?= $(shell go env GOOS)
  75. ifeq ($(LOCAL_OS),linux)
  76. TARGET_OS ?= linux
  77. else ifeq ($(LOCAL_OS),darwin)
  78. TARGET_OS ?= darwin
  79. else ifeq ($(LOCAL_OS),windows)
  80. TARGET_OS ?= windows
  81. else ifeq ($(LOCAL_OS),freebsd)
  82. TARGET_OS ?= freebsd
  83. else ifeq ($(LOCAL_OS),openbsd)
  84. TARGET_OS ?= openbsd
  85. else
  86. $(error This system's OS $(LOCAL_OS) isn't supported)
  87. endif
  88. ifeq ($(TARGET_OS), windows)
  89. EXECUTABLE_PATH=./$(BINARY_NAME).exe
  90. else
  91. EXECUTABLE_PATH=./$(BINARY_NAME)
  92. endif
  93. ifeq ($(FLAVOR), centos-7)
  94. TARGET_PUBLIC_REPO ?= el7
  95. else
  96. TARGET_PUBLIC_REPO ?= $(FLAVOR)
  97. endif
  98. ifneq ($(TARGET_ARM), )
  99. ARM_COMMAND := GOARM=$(TARGET_ARM)
  100. endif
  101. ifeq ($(TARGET_ARM), 7)
  102. PACKAGE_ARCH := armhf
  103. else
  104. PACKAGE_ARCH := $(TARGET_ARCH)
  105. endif
  106. #for FIPS compliance, FPM defaults to MD5.
  107. RPM_DIGEST := --rpm-digest sha256
  108. .PHONY: all
  109. all: cloudflared test
  110. .PHONY: clean
  111. clean:
  112. go clean
  113. .PHONY: cloudflared
  114. cloudflared:
  115. ifeq ($(FIPS), true)
  116. $(info Building cloudflared with go-fips)
  117. endif
  118. GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) $(ARM_COMMAND) go build -mod=vendor $(GO_BUILD_TAGS) $(LDFLAGS) $(IMPORT_PATH)/cmd/cloudflared
  119. ifeq ($(FIPS), true)
  120. ./check-fips.sh cloudflared
  121. endif
  122. .PHONY: container
  123. container:
  124. docker build --build-arg=TARGET_ARCH=$(TARGET_ARCH) --build-arg=TARGET_OS=$(TARGET_OS) -t cloudflare/cloudflared-$(TARGET_OS)-$(TARGET_ARCH):"$(VERSION)" .
  125. .PHONY: generate-docker-version
  126. generate-docker-version:
  127. echo latest $(VERSION) > versions
  128. .PHONY: test
  129. test: vet
  130. ifndef CI
  131. go test -v -mod=vendor -race $(LDFLAGS) ./...
  132. else
  133. @mkdir -p .cover
  134. go test -v -mod=vendor -race $(LDFLAGS) -coverprofile=".cover/c.out" ./...
  135. endif
  136. .PHONY: cover
  137. cover:
  138. @echo ""
  139. @echo "=====> Total test coverage: <====="
  140. @echo ""
  141. # Print the overall coverage here for quick access.
  142. $Q go tool cover -func ".cover/c.out" | grep "total:" | awk '{print $$3}'
  143. # Generate the HTML report that can be viewed from the browser in CI.
  144. $Q go tool cover -html ".cover/c.out" -o .cover/all.html
  145. .PHONY: fuzz
  146. fuzz:
  147. @go test -fuzz=FuzzIPDecoder -fuzztime=600s ./packet
  148. @go test -fuzz=FuzzICMPDecoder -fuzztime=600s ./packet
  149. @go test -fuzz=FuzzSessionWrite -fuzztime=600s ./quic/v3
  150. @go test -fuzz=FuzzSessionServe -fuzztime=600s ./quic/v3
  151. @go test -fuzz=FuzzRegistrationDatagram -fuzztime=600s ./quic/v3
  152. @go test -fuzz=FuzzPayloadDatagram -fuzztime=600s ./quic/v3
  153. @go test -fuzz=FuzzRegistrationResponseDatagram -fuzztime=600s ./quic/v3
  154. @go test -fuzz=FuzzNewIdentity -fuzztime=600s ./tracing
  155. @go test -fuzz=FuzzNewAccessValidator -fuzztime=600s ./validation
  156. .PHONY: install-go
  157. install-go:
  158. rm -rf ${CF_GO_PATH}
  159. ./.teamcity/install-cloudflare-go.sh
  160. .PHONY: cleanup-go
  161. cleanup-go:
  162. rm -rf ${CF_GO_PATH}
  163. cloudflared.1: cloudflared_man_template
  164. sed -e 's/\$${VERSION}/$(VERSION)/; s/\$${DATE}/$(DATE)/' cloudflared_man_template > cloudflared.1
  165. install: install-go cloudflared cloudflared.1 cleanup-go
  166. mkdir -p $(DESTDIR)$(INSTALL_BINDIR) $(DESTDIR)$(INSTALL_MANDIR)
  167. install -m755 cloudflared $(DESTDIR)$(INSTALL_BINDIR)/cloudflared
  168. install -m644 cloudflared.1 $(DESTDIR)$(INSTALL_MANDIR)/cloudflared.1
  169. # When we build packages, the package name will be FIPS-aware.
  170. # But we keep the binary installed by it to be named "cloudflared" regardless.
  171. define build_package
  172. mkdir -p $(PACKAGE_DIR)
  173. cp cloudflared $(PACKAGE_DIR)/cloudflared
  174. cp cloudflared.1 $(PACKAGE_DIR)/cloudflared.1
  175. fpm -C $(PACKAGE_DIR) -s dir -t $(1) \
  176. --description 'Cloudflare Tunnel daemon' \
  177. --vendor 'Cloudflare' \
  178. --license 'Apache License Version 2.0' \
  179. --url 'https://github.com/cloudflare/cloudflared' \
  180. -m 'Cloudflare <support@cloudflare.com>' \
  181. -a $(PACKAGE_ARCH) -v $(VERSION) -n $(DEB_PACKAGE_NAME) $(RPM_DIGEST) $(NIGHTLY_FLAGS) --after-install postinst.sh --after-remove postrm.sh \
  182. cloudflared=$(INSTALL_BINDIR) cloudflared.1=$(INSTALL_MANDIR)
  183. endef
  184. .PHONY: cloudflared-deb
  185. cloudflared-deb: cloudflared cloudflared.1
  186. $(call build_package,deb)
  187. .PHONY: cloudflared-rpm
  188. cloudflared-rpm: cloudflared cloudflared.1
  189. $(call build_package,rpm)
  190. .PHONY: cloudflared-pkg
  191. cloudflared-pkg: cloudflared cloudflared.1
  192. $(call build_package,osxpkg)
  193. .PHONY: cloudflared-msi
  194. cloudflared-msi:
  195. wixl --define Version=$(VERSION) --define Path=$(EXECUTABLE_PATH) --output cloudflared-$(VERSION)-$(TARGET_ARCH).msi cloudflared.wxs
  196. .PHONY: github-release-dryrun
  197. github-release-dryrun:
  198. python3 github_release.py --path $(PWD)/built_artifacts --release-version $(VERSION) --dry-run
  199. .PHONY: github-release
  200. github-release:
  201. python3 github_release.py --path $(PWD)/built_artifacts --release-version $(VERSION)
  202. python3 github_message.py --release-version $(VERSION)
  203. .PHONY: r2-linux-release
  204. r2-linux-release:
  205. python3 ./release_pkgs.py
  206. .PHONY: capnp
  207. capnp:
  208. which capnp # https://capnproto.org/install.html
  209. which capnpc-go # go install zombiezen.com/go/capnproto2/capnpc-go@latest
  210. capnp compile -ogo tunnelrpc/proto/tunnelrpc.capnp tunnelrpc/proto/quic_metadata_protocol.capnp
  211. .PHONY: vet
  212. vet:
  213. go vet -mod=vendor github.com/cloudflare/cloudflared/...
  214. .PHONY: fmt
  215. fmt:
  216. @goimports -l -w -local github.com/cloudflare/cloudflared $$(go list -mod=vendor -f '{{.Dir}}' -a ./... | fgrep -v tunnelrpc/proto)
  217. @go fmt $$(go list -mod=vendor -f '{{.Dir}}' -a ./... | fgrep -v tunnelrpc/proto)
  218. .PHONY: fmt-check
  219. fmt-check:
  220. @./fmt-check.sh
  221. .PHONY: lint
  222. lint:
  223. @golangci-lint run
  224. .PHONY: mocks
  225. mocks:
  226. go generate mocks/mockgen.go