New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/go: build: add -static flag #26492

Open
kolyshkin opened this Issue Jul 19, 2018 · 11 comments

Comments

Projects
None yet
10 participants
@kolyshkin
Contributor

kolyshkin commented Jul 19, 2018

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

  • #12058 (original proposal for adding conditional --static to pkg-config)
  • #24787 (initial version of this very proposal)

@ianlancetaylor ianlancetaylor changed the title from [proposal] go build: add -static flag to proposal: cmd/go: build: add -static flag Jul 20, 2018

@gopherbot gopherbot added this to the Proposal milestone Jul 20, 2018

@gopherbot gopherbot added the Proposal label Jul 20, 2018

@rsc

This comment has been minimized.

Contributor

rsc commented Jul 23, 2018

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).

@rsc rsc modified the milestones: Proposal, Go1.12 Jul 23, 2018

@rsc

This comment has been minimized.

Contributor

rsc commented Jul 23, 2018

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

@agnivade agnivade added help wanted and removed NeedsDecision labels Jul 24, 2018

@amenzhinsky

This comment has been minimized.

Contributor

amenzhinsky commented Jul 24, 2018

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.

@kolyshkin

This comment has been minimized.

Contributor

kolyshkin commented Jul 24, 2018

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.

@iamoryanmoshe

This comment has been minimized.

Contributor

iamoryanmoshe commented Jul 24, 2018

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.

@kolyshkin

This comment has been minimized.

Contributor

kolyshkin commented Jul 25, 2018

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)
@vikramcse

This comment has been minimized.

vikramcse commented Aug 3, 2018

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

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Aug 3, 2018

@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.

@vikramcse

This comment has been minimized.

vikramcse commented Aug 4, 2018

Thanks @ianlancetaylor, I should start from simpler issue

@tmm1

This comment has been minimized.

Contributor

tmm1 commented Nov 2, 2018

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.

@kolyshkin

This comment has been minimized.

Contributor

kolyshkin commented Nov 6, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment