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

make.bash: clarify that CGO_ENABLED=0 sets default into resulting compiler #51305

Open
rittneje opened this issue Feb 21, 2022 · 7 comments
Open
Labels
Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rittneje
Copy link

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

$ go version
1.17.7

Does this issue reproduce with the latest release?

Yes.

What did you do?

Ran CGO_ENABLED=0 ./make.bash.

What did you expect to see?

I expected it to produce a compiler (without using cgo) that abides by the documented behavior for cgo. Namely, host builds have cgo enabled by default.

What did you see instead?

The resulting compiler defaults to cgo disabled even for the host, as confirmed by go env.

@seankhliao
Copy link
Member

This is working as intended, ref #12808

@rittneje
Copy link
Author

rittneje commented Feb 21, 2022

@seankhliao This behavior is not at all implied by the current documentation for make.bash.

# CGO_ENABLED: Controls cgo usage during the build. Set it to 1
# to include all cgo related files, .c and .go file with "cgo"
# build directive, in the build. Set it to 0 to ignore them.

That does not make it clear that it is actually going to burn a default into the resulting compiler. Probably there need to be two different flags:

  1. Should cgo be enabled while compiling?
  2. What default value should be burned into the resulting compiler?

Please re-open this issue.

@ianlancetaylor
Copy link
Contributor

The current behavior is working as intended. We aren't going to change how CGO_ENABLED works today.

Reopening to clarify the documentation.

@ianlancetaylor ianlancetaylor changed the title make.bash: CGO_ENABLED=0 sets default into resulting compiler make.bash: clarify that CGO_ENABLED=0 sets default into resulting compiler Feb 21, 2022
@ianlancetaylor ianlancetaylor added Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Feb 21, 2022
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Feb 21, 2022
@rittneje
Copy link
Author

We aren't going to change how CGO_ENABLED works today.

@ianlancetaylor I'd like to request that you reconsider this. Today if I build a compiler targeting linux from an Alpine image (yes I know this isn't fully supported), then my options are either:

  1. build with CGO_ENABLED=0, which has the unwanted side effect of producing a compiler that doesn't do cgo for the host by default
  2. build without CGO_ENABLED=0 and produce an unusable compiler that is linked with musl

I think it would make more sense (and be more consistent) if there were CGO_ENABLED_FOR_$GOOS_$GOARCH environment variables, which set the defaults for the built compiler. This would accomplish two goals:

  1. In my case, I would run with CGO_ENABLED=0 CGO_ENABLED_FOR_linux_amd64=1 and get the expected result.
  2. People who are currently setting CC_FOR_$GOOS_$GOARCH can improve their experience by not having to explicitly set CGO_ENABLED=1 for a target for which they have pre-selected a C compiler.

@ianlancetaylor
Copy link
Contributor

I'm not opposed to adding CGO_ENABLED_FOR_${GOOS}_$(GOARCH} or perhaps more simply CGO_ENABLED_FOR_TARGET. What I'm opposed to is changing the current meaning of CGO_ENABLED.

@rsc
Copy link
Contributor

rsc commented Feb 22, 2022

We are planning some changes to the way cgo in std is handled, and it may be worth revisiting this behavior at that time. For now we should probably sit on this issue.

@rittneje
Copy link
Author

For anyone looking for a workaround for the original issue, on linux/amd64 you can explicitly set GOHOSTARCH=386 to fool make.bash into thinking it is doing a cross-compile, so it will produce a compiler without cgo that still has cgo enabled by default. Note that you will also need to install gcc-multilib, as the bootstrap compiler will still be compiled with cgo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants