Merge pull request #7022 from kzys/static
Make building static binaries simpler
This commit is contained in:
		@@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										11
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								Makefile
									
									
									
									
									
								
							@@ -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.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user