From: Michael Hudson-Doyle Date: Tue, 28 Jan 2014 05:47:09 +0000 (+1100) Subject: cmd/go: When linking with gccgo pass .a files in the order they are discovered X-Git-Tag: go1.3beta1~858 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9f8f0a1bfa49b6b617c623f51b9d10ba9a5e4641;p=gostls13.git 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 --- diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go index bfadec68ea..da90f0c029 100644 --- a/src/cmd/go/build.go +++ b/src/cmd/go/build.go @@ -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")