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/go: 'go build -race' with a cross-compiled GOOS should not suggest setting CGO_ENABLED #37021

Closed
MasterDimmy opened this issue Feb 4, 2020 · 9 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@MasterDimmy
Copy link

MasterDimmy commented Feb 4, 2020

$ go version
go version go1.13.7 windows/amd64

Does this issue reproduce with the latest release?

YES

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

go env Output
$ go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\8\AppData\Local\go-build
set GOENV=C:\Users\8\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\gopath
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Go\src\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=d:\TEMP\go-build345576314=/tmp/go-build -gno-record-gcc-switches

What did you do?

i compile this file for test:

package main

import (
"fmt"
)

func main() {
c := make(chan bool)
m := make(map[string]string)
go func() {
m["1"] = "a" // First conflicting access.
c <- true
}()
m["2"] = "b" // Second conflicting access.
<-c
for k, v := range m {
fmt.Println(k, v)
}
}

with

set CGO_ENABLED=1
set GOOS=linux
set GO_ARCH=amd64
go build -race

What did you expect to see?

success compile

What did you see instead?

# runtime/cgo
gcc_linux_amd64.c: In function '_cgo_sys_thread_start':
gcc_linux_amd64.c:66:2: error: implicit declaration of function 'sigfillset' [-Werror=implicit-function-declaration]
   66 |  sigfillset(&ign);
      |  ^~~~~~~~~~
cc1.exe: all warnings being treated as errors

Note, that

set CGO_ENABLED=1
set GOOS=linux
go build 

is succecced, but i need to compile with "-race" detector. If CGO_ENABLED=0

set CGO_ENABLED=0
set GOOS=linux
go build -race

then i got

go build: -race requires cgo; enable cgo by setting CGO_ENABLED=1
@bcmills
Copy link
Contributor

bcmills commented Feb 4, 2020

Cross-compiling programs with C dependencies is generally not supported, and the runtime support for -race mode relies on C dependencies.

If you want to build a race-enabled binary, you need to either compile for GOOS=windows or build the binary on a Linux machine.

@bcmills
Copy link
Contributor

bcmills commented Feb 4, 2020

Probably the best we can do here is emit a clearer diagnostic for cross-compiled -race builds. (We should not prompt the user to “enable cgo” if we don't have reason to believe that such a build would succeed.)

CC @jayconrod @matloob

@bcmills bcmills changed the title cannot compile go build -race with GOOS=linux on windows cmd/go: 'go build -race' with a cross-compiled GOOS should not suggest setting CGO_ENABLED Feb 4, 2020
@bcmills bcmills added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Feb 4, 2020
@bcmills bcmills added this to the Backlog milestone Feb 4, 2020
@MasterDimmy
Copy link
Author

just try to cross compile this:

package main

import (
"fmt"
)

func main() {
c := make(chan bool)
m := make(map[string]string)
go func() {
m["1"] = "a" // First conflicting access.
c <- true
}()
m["2"] = "b" // Second conflicting access.
<-c
for k, v := range m {
fmt.Println(k, v)
}
}

from windows\amd64 to linux\amd64 with gox

@MasterDimmy
Copy link
Author

I think "'go build -race' with a cross-compiled GOOS should not suggest setting CGO_ENABLED" is not what this issue is for, that is another issue\suggestion.

Current issue is unavailability to crosscompile "-race" with gox.

@bcmills
Copy link
Contributor

bcmills commented Feb 4, 2020

@MasterDimmy, the fact that go test -race has C dependencies is not something we can feasibly fix. (The race detector uses a fairly extensive C library, and the folks on the Go team are not in a position to rewrite it from scratch in pure Go.)

If you want to volunteer to rewrite the race-detector library in pure Go, you're welcome to do so, but as far as “things we can feasibly plan to fix” are concerned, a better error message for this case is about the limit.

@keyan
Copy link
Contributor

keyan commented Apr 26, 2020

@bcmills I took a stab at just modifying the error message in #38670. I'm just trying getting my feet wet and orient myself in the codebase.

@gopherbot
Copy link

Change https://golang.org/cl/230202 mentions this issue: work: add error for cross-compiled -race builds

@ianlancetaylor
Copy link
Contributor

Wait, I'm not following the suggestion here. Cross-compiling cgo code works fine if you have a cross-compiler.

We may want a general fix to do better checking of whether you are using a cross-compiler when doing a cross-build that uses cgo. But that seems separate from the narrow issue of "go build -race", which should work if a cross-compiler is installed.

@keyan
Copy link
Contributor

keyan commented Apr 28, 2020

I misunderstood Bryan's comment:

Cross-compiling programs with C dependencies is generally not supported

If I understand correctly now, the short term move is just to not mention setting CGO_ENABLED because that isn't sufficient on its' own and it is too verbose to explain that you need to have a cross-compiler installed?

xujianhai666 pushed a commit to xujianhai666/go-1 that referenced this issue May 21, 2020
Race builds require C dependencies, but cross-compiled cgo builds are
not always possible, so don't suggest enabling CGO in those cases.

Fixes golang#37021

Change-Id: I1fd675efc9cef958a926bd63eac8e6858bc59d0a
GitHub-Last-Rev: cbf43c1
GitHub-Pull-Request: golang#38670
Reviewed-on: https://go-review.googlesource.com/c/go/+/230202
Reviewed-by: Bryan C. Mills <bcmills@google.com>
@golang golang locked and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants