-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
cmd/go: build: add -static flag #26492
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. |
Still, I agree it's better to have
Right (and current
Great.
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 |
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. |
It should
|
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: 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 |
To get a statically linked PIE executable I needed the following invocation (I'm mentioning this here, as I found this documented nowhere on the net, scraped it together from this thread, and this stackoverflow answer) CGO_ENABLED=1 go build -buildmode=pie -tags 'osusergo,netgo,static,' -ldflags '-linkmode=external -extldflags "-static-pie"' . The proposed |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
Since this has been dragging on for a couple of years already, would it not be a better solution to include the instructions for static builds in the release notes for each version, since they keep on changing over time ? That would obviate the need for an actual code change. |
I think we could update the list at the top of this thread continuously for that purpose. Also, this doesn't make @MaoJianwei we understand Go does support static builds - this thread is about making them easier to achieve. |
And hopefully less confusing, too: Often, static linking is used to make it easier to move binaries between different systems (of the same architecture), but that doesn't really work with glibc: there, dynamically linked binaries are compatible with glibc updates, but any glibc change (including certain minor version updates) may break statically linked binaries that use NSS or other forms of |
@mvdan that's a start. The bonus if doing it in release notes is that each version could have relevant info, while this thread would likely be evergreen, only for the latest/next (which one ?) version. Or maybe a table per version here ? |
This comment was marked as off-topic.
This comment was marked as off-topic.
Thanks, @fgm @mvdan @fweimer-rh . |
@MaoJianwei |
Let me rephrase: The proposed |
The PFC components depend on glibc. I'm building the web service as a static binary so we don't need to install musl libc. More info on golang static builds: golang/go#26492
This is a proposal to add
-static
flag togo 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 forgo build
and friends.In addition to the above benefit of hiding the complexity,
-static
can also bring us more niceties, such as:static
build tag third-party software can rely upon. Currently usingstatic_build
tag seems like a de-facto standard, used a lot, but it has to be explicitly defined like in [1] above.--static
flag topkg-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
--static
topkg-config
)The text was updated successfully, but these errors were encountered: