Commit 9f8f0a1b authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle Committed by Dave Cheney

cmd/go: When linking with gccgo pass .a files in the order they are discovered

Under some circumstances linking a test binary with gccgo can fail, because
the installed version of the library ends up before the version built for the
test on the linker command line.

This admittedly slightly hackish fix fixes this by putting the library archives
on the linker command line in the order that a pre-order depth first traversal
of the dependencies gives them, which has the side effect of always putting the
version of the library built for the test first.

Fixes #6768

LGTM=rsc
R=golang-codereviews, minux.ma, gobot, rsc, dave
CC=golang-codereviews
https://golang.org/cl/28050043
parent 610dc92e
......@@ -1817,8 +1817,9 @@ func (gccgoToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles
func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []*action, mainpkg string, ofiles []string) error {
// gccgo needs explicit linking with all package dependencies,
// and all LDFLAGS from cgo dependencies.
afiles := make(map[*Package]string)
sfiles := make(map[*Package][]string)
afilesSeen := make(map[*Package]bool)
afiles := []string{}
sfiles := []string{}
ldflags := b.gccArchArgs()
cgoldflags := []string{}
usesCgo := false
......@@ -1826,8 +1827,9 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
for _, a := range allactions {
if a.p != nil {
if !a.p.Standard {
if afiles[a.p] == "" || a.objpkg != a.target {
afiles[a.p] = a.target
if !afilesSeen[a.p] || a.objpkg != a.target {
afilesSeen[a.p] = true
afiles = append(afiles, a.target)
}
}
cgoldflags = append(cgoldflags, a.p.CgoLDFLAGS...)
......@@ -1841,7 +1843,7 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
}
for _, f := range stringList(a.p.SwigFiles, a.p.SwigCXXFiles) {
soname := a.p.swigSoname(f)
sfiles[a.p] = append(sfiles[a.p], filepath.Join(sd, soname))
sfiles = append(sfiles, filepath.Join(sd, soname))
}
usesCgo = true
}
......@@ -1850,12 +1852,8 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
}
}
}
for _, afile := range afiles {
ldflags = append(ldflags, afile)
}
for _, sfiles := range sfiles {
ldflags = append(ldflags, sfiles...)
}
ldflags = append(ldflags, afiles...)
ldflags = append(ldflags, sfiles...)
ldflags = append(ldflags, cgoldflags...)
if usesCgo && goos == "linux" {
ldflags = append(ldflags, "-Wl,-E")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment