Skip to content

Commit 434f0e6

Browse files
committed
Compile shim without cgo for size and mem usage
This compiles the shim without cgo and statically which ends up to have lower memory usage and binary size. buildmode=pie cannot be used without cgo. ref: golang/go#17789 (comment) Signed-off-by: Michael Crosby <[email protected]>
1 parent bc063f2 commit 434f0e6

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ COMMANDS=ctr containerd containerd-stress containerd-release
4141
BINARIES=$(addprefix bin/,$(COMMANDS))
4242

4343
GO_TAGS=$(if $(BUILDTAGS),-tags "$(BUILDTAGS)",)
44-
GO_LDFLAGS=-ldflags "-s -w -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PKG) $(EXTRA_LDFLAGS)"
44+
GO_LDFLAGS=-ldflags '-s -w -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PKG) $(EXTRA_LDFLAGS)'
45+
SHIM_GO_LDFLAGS=-ldflags '-s -w -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PKG) -extldflags "-static"'
4546

4647
TESTFLAGS_RACE=
4748
GO_GCFLAGS=
@@ -142,6 +143,10 @@ bin/%: cmd/% FORCE
142143
@echo "$(WHALE) $@${BINARY_SUFFIX}"
143144
@go build -o $@${BINARY_SUFFIX} ${GO_LDFLAGS} ${GO_TAGS} ${GO_GCFLAGS} ./$<
144145

146+
bin/containerd-shim: cmd/containerd-shim FORCE # set !cgo and omit pie for a static shim build: https://github.com/golang/go/issues/17789#issuecomment-258542220
147+
@echo "$(WHALE) bin/containerd-shim"
148+
@CGO_ENABLED=0 go build -o bin/containerd-shim ${SHIM_GO_LDFLAGS} ${GO_TAGS} ./cmd/containerd-shim
149+
145150
binaries: $(BINARIES) ## build binaries
146151
@echo "$(WHALE) $@"
147152

0 commit comments

Comments
 (0)