Skip to content

Commit 00eb74a

Browse files
author
Kazuyoshi Kato
committed
Make building static binaries simpler
This commit adds STATIC makefile variable to adjust LDFLAGS and build tags accordingly. Fixes #5824. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 455b457 commit 00eb74a

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

BUILDING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ Please refer to [RUNC.md](/docs/RUNC.md) for the currently supported version of
122122
You can build static binaries by providing a few variables to `make`:
123123

124124
```sh
125-
make EXTRA_FLAGS="-buildmode pie" \
126-
EXTRA_LDFLAGS='-linkmode external -extldflags "-fno-PIC -static"' \
127-
BUILDTAGS="netgo osusergo static_build"
125+
make STATIC=1
128126
```
129127

130128
> *Note*:
131129
> - static build is discouraged
132130
> - static containerd binary does not support loading shared object plugins (`*.so`)
131+
> - static build binaries are not position-independent
133132
134133
# Via Docker container
135134

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ endif
9090
GO_BUILDTAGS ?=
9191
GO_BUILDTAGS += urfave_cli_no_docs
9292
GO_BUILDTAGS += ${DEBUG_TAGS}
93+
ifneq ($(STATIC),)
94+
GO_BUILDTAGS += osusergo netgo static_build
95+
endif
9396
GO_TAGS=$(if $(GO_BUILDTAGS),-tags "$(strip $(GO_BUILDTAGS))",)
94-
GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) $(EXTRA_LDFLAGS)'
97+
98+
GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) $(EXTRA_LDFLAGS)
99+
ifneq ($(STATIC),)
100+
GO_LDFLAGS += -extldflags "-static"
101+
endif
102+
GO_LDFLAGS+='
103+
95104
SHIM_GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) -extldflags "-static" $(EXTRA_LDFLAGS)'
96105

97106
# Project packages.

Makefile.linux

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ COMMANDS += containerd-shim containerd-shim-runc-v1 containerd-shim-runc-v2
2121
# check GOOS for cross compile builds
2222
ifeq ($(GOOS),linux)
2323
ifneq ($(GOARCH),$(filter $(GOARCH),mips mipsle mips64 mips64le ppc64))
24-
GO_GCFLAGS += -buildmode=pie
24+
ifeq ($(STATIC),)
25+
GO_GCFLAGS += -buildmode=pie
26+
endif
2527
endif
2628
endif
2729

0 commit comments

Comments
 (0)