-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: override semantics break e.g. GOFLAGS=-ldflags #38522
Comments
I think @myitcv also raised a similar point about
For example, here's one sample idea to extend the current syntax. Allow Edit: I realise that |
CC @matloob |
Yes, changing the default behavior is entirely reasonable except when it isn't. For example, what if I set
to turn off DWARF by default, but then I want to override that, with |
In general the possible can of complexity we could open here is quite large, and I am wary of anything that sounds "straightforward". For example, supposing we had the += syntax, using it in GOFLAGS would not be enough, because GOFLAGS applies before the command line. The Makefile command line would still need to be changed too. At that point why not make the Makefile accept from a given environment variable the things you want to inject? |
@rsc in your example if ldflags were indeed "additive", you could enabled a DWARF binary by just adding in -ldflags=-w=0, I think. |
It is pretty common with other compilers/linkers to have the concept of "rightmost setting wins", e.g. if you say "-w -w=0 -w=1 -w=0" you get -w=0. |
I agree with the rightmost setting wins rule, which is what the linker implements anyhow. That is how |
This commit removes the DWARF debugging information[1] Decided not to try to also turn off generation of the Go symbol table using the `-s` flag, as: 1. the GOFLAGS functionality currently only allows 1 ldflag to be set[2] (NB this may be fixed in Go 1.15[3]) 2. The improvements to the binary size in Go 1.15 [4] mean that the `-s` flag will no longer really do anything[5] Will also try shrinking the binary size further using upx[6] in a separate pull request. [1] https://stackoverflow.com/a/22276273 [2] golang/go#38522 [3] golang/go#26849 (comment) [4] See #64 [5] https://twitter.com/bradfitz/status/1256989624590712834 [6] https://upx.github.io/
This commit removes the DWARF debugging information[1], resulting in the Docker image size shrinking from 4.76MB to 2.88MB. Decided not to try to also turn off generation of the Go symbol table using the `-s` flag, as: 1. the GOFLAGS functionality currently only allows 1 ldflag to be set[2] (NB this may be fixed in Go 1.15[3]) 2. The improvements to the binary size in Go 1.15 [4] mean that the `-s` flag will no longer really do anything[5] Will also try shrinking the binary size further using upx[6] in a separate pull request. [1] https://stackoverflow.com/a/22276273 [2] golang/go#38522 [3] golang/go#26849 (comment) [4] See #64 [5] https://twitter.com/bradfitz/status/1256989624590712834 [6] https://upx.github.io/
This commit removes the DWARF debugging information[1], resulting in the Docker image size shrinking from 4.76MB to 2.88MB. Decided not to try to also turn off generation of the Go symbol table using the `-s` flag, as: 1. the GOFLAGS functionality currently only allows 1 ldflag to be set[2] (NB this may be fixed in Go 1.15[3]) 2. The improvements to the binary size in Go 1.15 [4] mean that the `-s` flag will no longer really do anything[5] Will also try shrinking the binary size further using upx[6] in a separate pull request. [1] https://stackoverflow.com/a/22276273 [2] golang/go#38522 [3] golang/go#26849 (comment) [4] See #64 [5] https://twitter.com/bradfitz/status/1256989624590712834 [6] https://upx.github.io/
This discussion never fully resolved, but it sounds like the rough consensus is that the current behavior is working as intended. |
I can get behind declaring the semantics as “rightmost setting wins”. However, the larger issue is “working as intended” only if we declare my use-case invalid :). Thinking about this again, maybe it would be helpful for this discussion to frame things in terms of default options and override options? Currently, |
@stapelberg, I wouldn't want to add an environment variable that overrides the command line. That seems very wrong to me. Above I wrote:
That still seems like the right approach to me. It's not the go command's responsibility to work around inadequate makefiles. |
That’s fair. I agree that a command-line flag is more direct than an environment variable, and it makes sense that the environment variable supplies defaults which the command-line overrides. More as a note to myself: the take-away is that I see that https://golang.org/cmd/go/ currently already documents the override semantics:
Still left to do: the “rightmost setting wins” semantics should also be documented here. |
I was asked to post this as a separate issue in #26849 (comment), so here goes:
Comment #26849 (comment) already touches upon the override semantics issue (
-ldflags=-w -ldflags=-s
results in only-s
), but since that example would likely be fixed by quoting changes, I’d like to stress another nuance not addressed by quoting: the current override behavior makes setting-ldflags
inGOFLAGS
impossible when thego
tool command lines contain an explicit-ldflags
flag.For my use-case, I want to set the ELF interpreter in my build system, so I figured I’d set the
GOFLAGS
environment variable like so:This works in general, but breaks as soon as the software in question specifies its own ldflags on the command line, like e.g. containerd does in its Makefile.
It’s easy to verify this:
Notably, from C build systems, I’m used to LDFLAGS generally being extended, never overridden.
For my use-case, I’m currently setting
-Wl,--dynamic-linker
inCGO_LDFLAGS
instead. Not entirely sure how reliable that is longer-term, but it seems to work with the packages I currently care about.The text was updated successfully, but these errors were encountered: