Commit 19f2bd8c authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle Committed by Ian Lance Taylor

cmd/go: be more careful when linking a test exe with gccgo

Previously, we ended up passing two compiled objects for the package
being tested when linking the test executable.  Somewhat by luck, this
worked most of the time but occasionally it did not.  This changes the
linking code to not pass two objects for the same ImportPath and to
always pass the object for the test version of the package and removes
some unecessary nil checks.

Change-Id: I7bbd3fc708f14672ee2cc6aed3397421fceb8a38
Reviewed-on: https://go-review.googlesource.com/1840Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 281ae928
...@@ -1899,7 +1899,6 @@ func (gccgoToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles ...@@ -1899,7 +1899,6 @@ 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 { 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, // gccgo needs explicit linking with all package dependencies,
// and all LDFLAGS from cgo dependencies. // and all LDFLAGS from cgo dependencies.
apackagesSeen := make(map[*Package]bool)
afiles := []string{} afiles := []string{}
ldflags := b.gccArchArgs() ldflags := b.gccArchArgs()
cgoldflags := []string{} cgoldflags := []string{}
...@@ -1907,27 +1906,30 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions [] ...@@ -1907,27 +1906,30 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
cxx := len(p.CXXFiles) > 0 cxx := len(p.CXXFiles) > 0
objc := len(p.MFiles) > 0 objc := len(p.MFiles) > 0
// Prefer the output of an install action to the output of a build action, // For a given package import path:
// because the install action will delete the output of the build action. // 1) prefer a test package (created by (*builder).test) to a non-test package
// Iterate over the list backward (reverse dependency order) so that we // 2) prefer the output of an install action to the output of a build action
// always see the install before the build. // 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-- { for i := len(allactions) - 1; i >= 0; i-- {
a := allactions[i] a := allactions[i]
if !a.p.Standard { if a.p.fake && !importPathsSeen[a.p.ImportPath] {
if a.p != nil && !apackagesSeen[a.p] { importPathsSeen[a.p.ImportPath] = true
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) afiles = append(afiles, a.target)
} }
} }
for i := len(allactions) - 1; i >= 0; i-- {
a := allactions[i]
if !a.p.Standard && !importPathsSeen[a.p.ImportPath] {
importPathsSeen[a.p.ImportPath] = true
afiles = append(afiles, a.target)
} }
} }
for _, a := range allactions { for _, a := range allactions {
if a.p != nil {
cgoldflags = append(cgoldflags, a.p.CgoLDFLAGS...) cgoldflags = append(cgoldflags, a.p.CgoLDFLAGS...)
if len(a.p.CgoFiles) > 0 { if len(a.p.CgoFiles) > 0 {
usesCgo = true usesCgo = true
...@@ -1942,7 +1944,6 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions [] ...@@ -1942,7 +1944,6 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
objc = true objc = true
} }
} }
}
ldflags = append(ldflags, afiles...) ldflags = append(ldflags, afiles...)
ldflags = append(ldflags, cgoldflags...) ldflags = append(ldflags, cgoldflags...)
ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...) ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...)
......
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