Go: cmd/go: build: add -static flag

Created on 20 Jul 2018  ·  23Comments  ·  Source: golang/go

This is a proposal to add -static flag to go build.

Producing a static build with go already requires a non-trivial amount of flags passed to go build, which on Linux currently amounts to something like:

-ldflags '-extldflags "-fno-PIC -static"' -buildmode pie -tags 'osusergo netgo static_build' [1]

...and this magic string keeps growing.

It would be awesome to encapsulate this sacred knowledge internally, exposing it via a new -static flag for go build and friends.

In addition to the above benefit of hiding the complexity, -static can also bring us more niceties, such as:

  • automatic static build tag third-party software can rely upon. Currently using static_build tag seems like a de-facto standard, used a lot, but it has to be explicitly defined like in [1] above.
  • providing --static flag to pkg-config invocations (those initiated by // #cgo pkg-config: lib lines in the source code). It will solve another issue (linking against a proper set of libraries for both static and dynamic link cases) for which a somewhat verbose workaround is currently required (for example, see ploop_link_static.go and ploop_link_dynamic.go).

See also

Proposal Proposal-Accepted help wanted

Most helpful comment

We build golang binaries across a variety of platforms, preferring static builds wherever possible. Here are the flags we're currently using:

windows: -tags netgo -ldflags '-H=windowsgui -extldflags "-static"'
linux/bsd: -tags netgo -ldflags '-extldflags "-static"'
macos: -ldflags '-s -extldflags "-sectcreate __TEXT __info_plist Info.plist"'
android: -ldflags -s

On macos and android, we need to be able to pull in system frameworks and libraries so we opted not to build static binaries.

All 23 comments

This seems OK but the tag should just be "static" not "static_build" (it's a build tag already). And we should make os/user and net do the right thing - whatever that is - instead of having to hard-code their tags too.

Note that the assumption is that -static does not disable cgo; it just makes the cgo uses statically linked. FWIW, I don't know how well statically linked cgo works, but apparently well enough.

It sounds like maybe the os/user and net non-cgo restrictions only apply on Linux (more precisely, GNU/Linux, since this is a glibc restriction).

If anyone wants to work on this for Go 1.12, help is appreciated.

I get the idea of combining netgo and usergo tags that looks a bit magical, but what's the reason of using the pie builmode since it forces the toolchain to use an external linker (gcc, clang) and many build systems, like alpine docker images, come without one.

This seems OK but the tag should just be "static" not "static_build" (it's a build tag already).

static_build was proposed as it is used in many packages (seems like a de facto standard... I'm too lazy to provide examples but please let me know if you need any).

Still, I agree it's better to have static as it is simple and straightforward (and hopefully won't collide with anything). Proposal changed.

And we should make os/user and net do the right thing - whatever that is

Right (and current netgo and osusergo flags might stay for backward compatibility, as they might also be used to choose one implementation over another, with no regard to static/dynamic linking).

Note that the assumption is that -static does not disable cgo; it just makes the cgo uses statically linked.

Great.

many build systems, like alpine docker images, come without one

I'm afraid I was overly brief describing the idea, let me emphasize.

The idea (specifically, the first part of it) is to hide the burden of choosing the build flags required in order to obtain a working static binary (on a best effort/knowledge basis). The above example is particularly valid for Linux/glibc. In case of Linux/musl (i.e. Alpine), for example, there is _probably_ no need to use osusergo or netgo tags.

Any chance you elaborate on what a static build does differently than regular build?

I would like to work on this proposal but I need further clarification.

Any chance you elaborate on what a static build does differently than regular build?

It should

  1. Set the needed build options (this is platform-dependent, Linux/glibc example is above).
  2. Set the needed build tags (for Linux/glibc this currently amounts to osusergo netgo).
  3. Set the static build tag.
  4. Add the --static flag to pkg-config invocations (those triggered by // #cgo pkg-config: <lib> ... lines in the source code)

Can I work on this as my first contribution to go?

@vikramcse The main requirements here are access to a range of different systems in order to test the work, and the knowledge required to write a good test for whether the program is statically linked. The actual patch is probably not too difficult, I hope. If you still want to give this a try, please go ahead.

Thanks @ianlancetaylor, I should start from simpler issue

We build golang binaries across a variety of platforms, preferring static builds wherever possible. Here are the flags we're currently using:

windows: -tags netgo -ldflags '-H=windowsgui -extldflags "-static"'
linux/bsd: -tags netgo -ldflags '-extldflags "-static"'
macos: -ldflags '-s -extldflags "-sectcreate __TEXT __info_plist Info.plist"'
android: -ldflags -s

On macos and android, we need to be able to pull in system frameworks and libraries so we opted not to build static binaries.

@tmm1 thanks for the info! Just noticed you should also use osusergo tag, at least for Linux.

Unfortunately we didn't get to this during the Go1.13 cycle and currently we are in a code freeze so I shall punt this to Go1.14 and apply an Early-In-Cycle tag too.

I think this won't work on macOS, where fully static builds are not allowed/supported by Apple. Binaries should always go through libSystem, which is also why we changed the way Go calls the kernel in Go 1.12. So, pure Go binaries are already as static as they can be, as far as I can tell.

I propose that on macOS go build -static simply tries to statically link cgo libraries, so that the final binary doesn't depend on third-party .so but just on system libraries. To do this, unfortunately, it looks like it's not sufficient to add the output of pkg-config --static --libs to LDFLAGS because that output still refers to each library as -L/path/to -lfoo (as this is the correct syntax when --static is passed to the linker, which we are not going to do in macOS). So, the output of pkg-config should be rewritten as /path/to/libfoo.a (using a similar library path search algorithm that the linker does).

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o test -ldflags '-extldflags "-f no-PIC -static"' -buildmode pie -tags 'osusergo netgo static_build' test.go

...this is ridiculous

@ddevault that's the point of the issue. I'm sure it can be done for 1.15 if someone steps up to do the work. I imagine the trickiest bit will be portability and writing good tests.

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o test -ldflags '-extldflags "-f no-PIC -static"' -buildmode pie -tags 'osusergo netgo static_build' test.go

@ddevault How are you managing to combine CGO_ENABLED=0 and -buildmode pie? I get the error loadinternal: cannot find runtime/cgo, and from what I'm reading elsewhere it seems that buildmode pie is incompatible with disabling cgo? I'm just looking for the smallest static binary I can get, without cgo. If I remove -buildmode pie, it works... but I'm not sure if I should also be removing the -fno-PIC flag as well?

loadinternal: cannot find runtime/cgo, as I understand it, is a red herring. I get this when compiling all Go projects under any configuration.

Trying to run the binary generated after getting that warning gives me in instant death from a SIGTRAP. Guess I need to choose between -buildmode pie and CGO_ENABLED=0.

[...] In case of Linux/musl (i.e. Alpine), for example, there is _probably_ no need to use osusergo or netgo tags.

For completeness, in #38789 I discovered that osusergo is still necessary on Alpine Linux in some cases.

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o test -ldflags '-extldflags "-f no-PIC -static"' -buildmode pie -tags 'osusergo netgo static_build' test.go

...this is ridiculous

This was sufficient for me to get a statically-linked binary for which readelf -d reports no linkage:

GOOS=linux go build -tags 'osusergo netgo'

Runs fine on CentOS when using CentOS, Alpine Linux, macOS, or Windows as the build OS.

I'm just mentioning this here in case it helps anyone else with manually configured static builds: when we upgraded to go 1.15, I had to also add -linkmode=external to what we were passing to -ldflags, as mentioned in the 1.15 release notes in the linker section: https://golang.org/doc/go1.15#linker

In our case, we are building static binaries on Debian, to be run on Alpine. Without providing -linkmode=external, attempting to run those applications on Alpine resulted in the obscure error message standard_init_linux.go:211: exec user process caused "no such file or directory". Running file against those binaries noted that they were dynamically linked.

@mark-rushakoff ahh nice! You just solved a sleepless night. :-)

@zrhoffman: This was sufficient for me to get a statically-linked binary for which readelf -d reports no linkage:

GOOS=linux go build -tags 'osusergo netgo'

Do you happen to know whether this only works for Go projects using specific packages, or if it can be relied upon to generate static builds in all cases? I understand the latter is probably too much to hope for considering the length of this thread. :)

EDIT: To answer my own question, no, those build tags alone are not sufficient to produce a working static binary in all cases. CGO appears to make things much more complicated.

Was this page helpful?
0 / 5 - 0 ratings