Description: Fix linking order when testing. Author: Michael Hudson-Doyle Origin: vendor Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/gccgo-go/+bug/1381671 Forwarded: no Last-Update: 2014-12-02 --- This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ --- a/src/cmd/go/build.go +++ b/src/cmd/go/build.go @@ -1705,7 +1705,6 @@ 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. - apackagesSeen := make(map[*Package]bool) afiles := []string{} sfiles := []string{} ldflags := b.gccArchArgs() @@ -1713,22 +1712,26 @@ usesCgo := false cxx := false - // Prefer the output of an install action to the output of a build action, - // because the install action will delete the output of the build action. - // Iterate over the list backward (reverse dependency order) so that we - // always see the install before the build. + // For a given package import path: + // 1) prefer a fake package (created by (*builder).test) to a real package + // 2) prefer the output of an install action to the output of a build action + // because the install action will delete the output of the build + // action + // Iterating over the list backwards (reverse dependency order) ensures that we + // always see an install before a build. + importPathsSeen := make(map[string]bool) for i := len(allactions) - 1; i >= 0; i-- { a := allactions[i] - if !a.p.Standard { - if a.p != nil && !apackagesSeen[a.p] { - apackagesSeen[a.p] = true - if a.p.fake { - // move _test files to the top of the link order - afiles = append([]string{a.target}, afiles...) - } else { - afiles = append(afiles, a.target) - } - } + if a.p != nil && a.p.fake && !importPathsSeen[a.p.ImportPath] { + importPathsSeen[a.p.ImportPath] = true + afiles = append(afiles, a.target) + } + } + for i := len(allactions) - 1; i >= 0; i-- { + a := allactions[i] + if a.p != nil && !a.p.Standard && !importPathsSeen[a.p.ImportPath] { + importPathsSeen[a.p.ImportPath] = true + afiles = append(afiles, a.target) } }