@@ -4,22 +4,33 @@ MSI_VERSION := $(shell git tag -l --sort=v:refname | grep "w" | tail -1 | cut
44# e.g. w3.0.1 or w4.2.10. It trims off the w character when creating the MSI.
55
66ifeq ($(FIPS ) , true)
7- GO_BUILD_TAGS := $(GO_BUILD_TAGS) fips
8- endif
9-
10- ifneq ($(GO_BUILD_TAGS ) ,)
11- GO_BUILD_TAGS := -tags $(GO_BUILD_TAGS)
7+ BINARY_NAME := cloudflared-fips
8+ else
9+ BINARY_NAME := cloudflared
1210endif
1311
1412ifeq ($(NIGHTLY ) , true)
13+ # We do not release FIPS in NIGHTLY, so no need to consider that case here.
1514 DEB_PACKAGE_NAME := cloudflared-nightly
1615 NIGHTLY_FLAGS := --conflicts cloudflared --replaces cloudflared
1716else
18- DEB_PACKAGE_NAME := cloudflared
17+ DEB_PACKAGE_NAME := $(BINARY_NAME)
1918endif
2019
2120DATE := $(shell date -u '+% Y-% m-% d-% H% M UTC')
22- VERSION_FLAGS := -ldflags='-X "main.Version=$(VERSION ) " -X "main.BuildTime=$(DATE ) "'
21+ VERSION_FLAGS := -X "main.Version=$(VERSION ) " -X "main.BuildTime=$(DATE ) "
22+
23+ LINK_FLAGS :=
24+ ifeq ($(FIPS ) , true)
25+ LINK_FLAGS := -linkmode=external -extldflags=-static $(LINK_FLAGS)
26+ # Prevent linking with libc regardless of CGO enabled or not.
27+ GO_BUILD_TAGS := $(GO_BUILD_TAGS) osusergo netgo fips
28+ endif
29+
30+ LDFLAGS := -ldflags='$(VERSION_FLAGS ) $(LINK_FLAGS ) '
31+ ifneq ($(GO_BUILD_TAGS ) ,)
32+ GO_BUILD_TAGS := -tags "$(GO_BUILD_TAGS)"
33+ endif
2334
2435IMPORT_PATH := github.com/cloudflare/cloudflared
2536PACKAGE_DIR := $(CURDIR ) /packaging
6172endif
6273
6374ifeq ($(TARGET_OS ) , windows)
64- EXECUTABLE_PATH=./cloudflared .exe
75+ EXECUTABLE_PATH=./$(BINARY_NAME) .exe
6576else
66- EXECUTABLE_PATH=./cloudflared
77+ EXECUTABLE_PATH=./$(BINARY_NAME)
6778endif
6879
6980ifeq ($(FLAVOR ) , centos-7)
@@ -80,17 +91,15 @@ clean:
8091 go clean
8192
8293.PHONY : cloudflared
83- cloudflared :
94+ cloudflared :
8495ifeq ($(FIPS ) , true)
8596 $(info Building cloudflared with go-fips)
86- -test -f fips/fips.go && mv fips/fips.go fips/fips.go.linux-amd64
87- mv fips/fips.go.linux-amd64 fips/fips.go
97+ cp -f fips/fips.go.linux-amd64 cmd/cloudflared/fips.go
8898endif
89-
90- GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) go build -v -mod=vendor $(GO_BUILD_TAGS) $(VERSION_FLAGS) $(IMPORT_PATH)/cmd/cloudflared
91-
99+ GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) go build -v -mod=vendor $(GO_BUILD_TAGS) $(LDFLAGS) $(IMPORT_PATH)/cmd/cloudflared
92100ifeq ($(FIPS ) , true)
93- mv fips/fips.go fips/fips.go.linux-amd64
101+ rm -f cmd/cloudflared/fips.go
102+ ./check-fips.sh cloudflared
94103endif
95104
96105.PHONY : container
@@ -100,10 +109,10 @@ container:
100109.PHONY : test
101110test : vet
102111ifndef CI
103- go test -v -mod=vendor -race $(VERSION_FLAGS ) ./...
112+ go test -v -mod=vendor -race $(LDFLAGS ) ./...
104113else
105114 @mkdir -p .cover
106- go test -v -mod=vendor -race $(VERSION_FLAGS ) -coverprofile=".cover/c.out" ./...
115+ go test -v -mod=vendor -race $(LDFLAGS ) -coverprofile=".cover/c.out" ./...
107116 go tool cover -html ".cover/c.out" -o .cover/all.html
108117endif
109118
@@ -112,10 +121,10 @@ test-ssh-server:
112121 docker-compose -f ssh_server_tests/docker-compose.yml up
113122
114123define publish_package
115- chmod 664 cloudflared *.$(1 ) ; \
124+ chmod 664 $( BINARY_NAME ) *.$(1 ) ; \
116125 for HOST in $(CF_PKG_HOSTS ) ; do \
117126 ssh-keyscan -t ecdsa $$HOST >> ~/.ssh/known_hosts; \
118- scp -p -4 cloudflared *.$(1 ) cfsync@$$HOST:/state/cf-pkg/staging/$(2 ) /$(TARGET_PUBLIC_REPO ) /cloudflared /; \
127+ scp -p -4 $( BINARY_NAME ) *.$(1 ) cfsync@$$HOST:/state/cf-pkg/staging/$(2 ) /$(TARGET_PUBLIC_REPO ) /$( BINARY_NAME ) /; \
119128 done
120129endef
121130
@@ -127,6 +136,8 @@ publish-deb: cloudflared-deb
127136publish-rpm : cloudflared-rpm
128137 $(call publish_package,rpm,yum)
129138
139+ # When we build packages, the package name will be FIPS-aware.
140+ # But we keep the binary installed by it to be named "cloudflared" regardless.
130141define build_package
131142 mkdir -p $(PACKAGE_DIR )
132143 cp cloudflared $(PACKAGE_DIR ) /cloudflared
@@ -247,8 +258,8 @@ tunnelrpc-deps:
247258 capnp compile -ogo tunnelrpc/tunnelrpc.capnp
248259
249260.PHONY : quic-deps
250- quic-deps :
251- which capnp
261+ quic-deps :
262+ which capnp
252263 which capnpc-go
253264 capnp compile -ogo quic/schema/quic_metadata_protocol.capnp
254265
258269 # go get github.com/sudarshan-reddy/go-sumtype (don't do this in build directory or this will cause vendor issues)
259270 # Note: If you have github.com/BurntSushi/go-sumtype then you might have to use the repo above instead
260271 # for now because it uses an older version of golang.org/x/tools.
261- which go-sumtype
272+ which go-sumtype
262273 go-sumtype $$(go list -mod=vendor ./... )
263274
264275.PHONY : goimports
265276goimports :
266- for d in $$ (go list -mod=readonly -f ' {{.Dir}}' -a ./... | fgrep -v tunnelrpc) ; do goimports -format-only -local github.com/cloudflare/cloudflared -w $$ d ; done
277+ for d in $$ (go list -mod=readonly -f ' {{.Dir}}' -a ./... | fgrep -v tunnelrpc) ; do goimports -format-only -local github.com/cloudflare/cloudflared -w $$ d ; done
0 commit comments