golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.18k stars 17.46k forks source link

cmd/go: with -compiler=gccgo should build C files even if CGO_ENABLED=0 #41758

Open ianlancetaylor opened 3 years ago

ianlancetaylor commented 3 years ago

When using gccgo, it is sometimes necessary to include C files in a package to provide functionality that when using gc would be implemented in assembler. On tip cmd/go does not build these C files if the environment variable CGO_ENABLED=0. This causes some valid packages to fail to build with gccgo.

The correct fix may be that if the package has an import "C", then C files should not be built if CGO_ENABLED=0. But if the package does not have an import "C", then C files should be built.

An example package that uses this is golang.org/x/sys/unix. Using the gccgo version of the go tool (I need to use the gccgo version here because https://golang.org/cl/219817 is not yet ported to the main go tool):

> go test
PASS
ok      golang.org/x/sys/unix   0.863s
> CGO_ENABLED=0 gccgoenv go test
# golang.org/x/sys/unix.test
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:48: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscallNoError'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:21: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscallNoError'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:28: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscall'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:35: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscall'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:42: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscall'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:53: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscall'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:21: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscallNoError'
/home/iant/gopath/src/golang.org/x/sys/unix/gccgo.go:21: error: undefined reference to 'golang.x2eorg..z2fx..z2fsys..z2funix.realSyscallNoError'
collect2: error: ld returned 1 exit status
FAIL    golang.org/x/sys/unix [build failed]
ianlancetaylor commented 3 years ago

This was found while investigating tricksterproxy/trickster#498.