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 <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato 2022-06-04 03:26:06 +00:00
parent 455b45708c
commit 00eb74a4ca
3 changed files with 15 additions and 5 deletions

View File

@ -122,14 +122,13 @@ Please refer to [RUNC.md](/docs/RUNC.md) for the currently supported version of
You can build static binaries by providing a few variables to `make`: You can build static binaries by providing a few variables to `make`:
```sh ```sh
make EXTRA_FLAGS="-buildmode pie" \ make STATIC=1
EXTRA_LDFLAGS='-linkmode external -extldflags "-fno-PIC -static"' \
BUILDTAGS="netgo osusergo static_build"
``` ```
> *Note*: > *Note*:
> - static build is discouraged > - static build is discouraged
> - static containerd binary does not support loading shared object plugins (`*.so`) > - static containerd binary does not support loading shared object plugins (`*.so`)
> - static build binaries are not position-independent
# Via Docker container # Via Docker container

View File

@ -90,8 +90,17 @@ endif
GO_BUILDTAGS ?= GO_BUILDTAGS ?=
GO_BUILDTAGS += urfave_cli_no_docs GO_BUILDTAGS += urfave_cli_no_docs
GO_BUILDTAGS += ${DEBUG_TAGS} GO_BUILDTAGS += ${DEBUG_TAGS}
ifneq ($(STATIC),)
GO_BUILDTAGS += osusergo netgo static_build
endif
GO_TAGS=$(if $(GO_BUILDTAGS),-tags "$(strip $(GO_BUILDTAGS))",) GO_TAGS=$(if $(GO_BUILDTAGS),-tags "$(strip $(GO_BUILDTAGS))",)
GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) $(EXTRA_LDFLAGS)'
GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) $(EXTRA_LDFLAGS)
ifneq ($(STATIC),)
GO_LDFLAGS += -extldflags "-static"
endif
GO_LDFLAGS+='
SHIM_GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) -extldflags "-static" $(EXTRA_LDFLAGS)' SHIM_GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PACKAGE) -extldflags "-static" $(EXTRA_LDFLAGS)'
# Project packages. # Project packages.

View File

@ -21,9 +21,11 @@ COMMANDS += containerd-shim containerd-shim-runc-v1 containerd-shim-runc-v2
# check GOOS for cross compile builds # check GOOS for cross compile builds
ifeq ($(GOOS),linux) ifeq ($(GOOS),linux)
ifneq ($(GOARCH),$(filter $(GOARCH),mips mipsle mips64 mips64le ppc64)) ifneq ($(GOARCH),$(filter $(GOARCH),mips mipsle mips64 mips64le ppc64))
ifeq ($(STATIC),)
GO_GCFLAGS += -buildmode=pie GO_GCFLAGS += -buildmode=pie
endif endif
endif endif
endif
# amd64 supports go test -race # amd64 supports go test -race
ifeq ($(GOARCH),amd64) ifeq ($(GOARCH),amd64)