1
0
mirror of https://github.com/golang/go synced 2024-11-23 04:50:06 -07:00

cmd/go: Use exported CgoLDFlags when compiler=gccgo

If you compile a program that has cgo LDFLAGS directives, those are exported to an environment variable to be used by subsequent compiler tool invocations. The linking phase when using the gccgo toolchain did not consider the envvar CGO_LDFLAGS's linking directives resulting in undefined references when using cgo+gccgo.

Fixes #7573

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/80780043
This commit is contained in:
Erik Westrup 2014-03-26 15:23:31 -07:00 committed by Ian Lance Taylor
parent a6adc19ea8
commit 881d693c86
2 changed files with 26 additions and 0 deletions

View File

@ -1905,6 +1905,7 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
ldflags = append(ldflags, afiles...)
ldflags = append(ldflags, sfiles...)
ldflags = append(ldflags, cgoldflags...)
ldflags = append(ldflags, p.CgoLDFLAGS...)
if usesCgo && goos == "linux" {
ldflags = append(ldflags, "-Wl,-E")
}

View File

@ -669,6 +669,31 @@ if ! ./testgo test -c -test.bench=XXX fmt; then
fi
rm -f fmt.test
TEST 'Issue 7573: cmd/cgo: undefined reference when linking a C-library using gccgo'
d=$(mktemp -d -t testgoXXX)
export GOPATH=$d
mkdir -p $d/src/cgoref
ldflags="-L alibpath -lalib"
echo "
package main
// #cgo LDFLAGS: $ldflags
// void f(void) {}
import \"C\"
func main() { C.f() }
" >$d/src/cgoref/cgoref.go
go_cmds="$(./testgo build -n -compiler gccgo cgoref 2>&1 1>/dev/null)"
ldflags_count="$(echo "$go_cmds" | egrep -c "^gccgo.*$(echo $ldflags | sed -e 's/-/\\-/g')" || true)"
if [ "$ldflags_count" -lt 1 ]; then
echo "No Go-inline "#cgo LDFLAGS:" (\"$ldflags\") passed to gccgo linking stage."
ok=false
fi
rm -rf $d
unset ldflags_count
unset go_cmds
unset ldflags
unset GOPATH
# clean up
if $started; then stop; fi
rm -rf testdata/bin testdata/bin1