]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: Use exported CgoLDFlags when compiler=gccgo
authorErik Westrup <erik.westrup@gmail.com>
Wed, 26 Mar 2014 22:23:31 +0000 (15:23 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 26 Mar 2014 22:23:31 +0000 (15:23 -0700)
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

src/cmd/go/build.go
src/cmd/go/test.bash

index 6166410c6e0c7f73cf0de29ff0c27970cf8fc235..a21c873c85f5f4aab638a91f6611ce992fbff190 100644 (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")
        }
index fe00df9e24d9f8948a5ddfedadb323fd19392b58..1091695b3744e8c2ed51f770c7e2e3eb1f2768da 100755 (executable)
@@ -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