Skip to content
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/cgo: pass explicit -O0 to compiler #26487

Closed
zevdg opened this issue Jul 19, 2018 · 9 comments
Closed

cmd/cgo: pass explicit -O0 to compiler #26487

zevdg opened this issue Jul 19, 2018 · 9 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@zevdg
Copy link
Contributor

zevdg commented Jul 19, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOOS="linux"

What did you do?

On Solus Linux

git clone https://github.com/golang/go
cd go/src/
git checkout go1.10.3
./all.bash

What did you expect to see?

All tests passing

What did you see instead?

# _/home/wolf/src/go/misc/cgo/test
cgo-dwarf-inference:2:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
cgo-dwarf-inference:4:2: error: initializer element is not constant
cgo-dwarf-inference:4:2: note: (near initialization for '__cgodebug_ints[0]')
FAIL	_/home/wolf/src/go/misc/cgo/test [build failed]
2018/07/19 16:57:27 Failed: exit status 2

The reason can be found in this patch

Quoting from that

When the go compiler uses cgo, removes all
C compilation optimization flags,
See commit: b5d18b5.

This is because optimization flags
breaks c code build process, see:
#14669

cgo creates invalid C code that works
with -O0 using gcc. The code it generates is valid C++
just not valid C.

In Clear Linux gcc compiler uses at least
-O1 optimization flag (if not optimization is specified), this cause an
issue when cgo is used. The go compiler removes any optimization flag
(even -O0 that should work
correctly) letting gcc use -O1 as default.

Since Solus and Clear Linux patch Go in their distribution, normal go users don't run into this problem, but anyone on those distros who tries to compile go from source runs into this problem.

I don't see any disadvantage to upstreaming this patch. Although go shouldn't be expected to play nicely with any old arbitrarily patched gcc, being explicit about the -O0 requirement instead of implicitly assuming gcc's default seems better all around. Making it easier for Solus and Clear Linux users to build (and contribute to) go makes this a win/win IMO.

Tagging the original author of this patch @jcvenegas in case he wants to submit this and himself and get due credit.

@ianlancetaylor ianlancetaylor changed the title Upstream Solus/Clear Linux patch: explicit gcc -O0 cmd/cgo: pass explicit -O0 to compiler Jul 19, 2018
@ianlancetaylor
Copy link
Contributor

Because of copyright reasons I don't particularly want to look at a patch that has not gone through the review process. If the original author wants to send in the patch, that would be great.

Otherwise, I take the problem to be that on some Linux distros the compiler defaults to -O1, but the code generated by cgo only works correctly with -O0. Therefore, we should explicitly pass -O0. For #14669 we explicitly removed -O options, but for these distros we need to explicitly add -O0. That seems fine.

@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 19, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jul 19, 2018
@zevdg
Copy link
Contributor Author

zevdg commented Jul 19, 2018

Yes, you've summarized the problem perfectly. Hopefully @jcvenegas will chime in.

@iamoryanmoshe
Copy link
Contributor

Do we know which distros this happens on? Is there a rule to check by?

@ianlancetaylor
Copy link
Contributor

@oryanmoshe We don't need to check, we can just always pass -O0.

@iamoryanmoshe
Copy link
Contributor

Do we want to wait for the original author to submit a CL or can I do it? @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

@oryanmoshe We haven't seen any response from @jcvenegas, so, sure, go for it (for 1.12). Thanks.

@iamoryanmoshe
Copy link
Contributor

I'm on it

@gopherbot
Copy link

Change https://golang.org/cl/127755 mentions this issue: cmd/cgo: pass explicit -O0 to the compiler

@jcvenegas
Copy link

@oryanmoshe @ianlancetaylor , sorry I miss this issue with a bunch of github notifications. Thanks for add the patch will make easy to work with some distros.

@golang golang locked and limited conversation to collaborators Aug 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants