cmd/go: gccgo: consistent results, fix vendoring

Pass the -fdebug-prefix-map and -gno-record-gcc-switches compiler
options to gccgo to generate consistent results.

Fix the vendoring code to look for /vendor/, not just /vendor, to
avoid being confused by something like vendor/vendor.org.

Tested by the cmd/go tests in a followup CL.

Change-Id: I814b38ef4d82fd0fa1bec4c1ec743ea1e717711f
Reviewed-on: https://go-review.googlesource.com/45695
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/libgo/go/cmd/go/build.go b/libgo/go/cmd/go/build.go
index 4b91505..7e98379 100644
--- a/libgo/go/cmd/go/build.go
+++ b/libgo/go/cmd/go/build.go
@@ -2692,6 +2692,8 @@
 	ofile = obj + out
 	gcargs := []string{"-g"}
 	gcargs = append(gcargs, b.gccArchArgs()...)
+	gcargs = append(gcargs, "-fdebug-prefix-map="+b.work+"=/tmp/go-build")
+	gcargs = append(gcargs, "-gno-record-gcc-switches")
 	if pkgpath := gccgoPkgpath(p); pkgpath != "" {
 		gcargs = append(gcargs, "-fgo-pkgpath="+pkgpath)
 	}
@@ -2707,14 +2709,14 @@
 
 	for _, path := range p.Imports {
 		// If this is a new vendor path, add it to the list of importArgs
-		if i := strings.LastIndex(path, "/vendor"); i >= 0 {
+		if i := strings.LastIndex(path, "/vendor/"); i >= 0 {
 			for _, dir := range savedirs {
 				// Check if the vendor path is already included in dir
-				if strings.HasSuffix(dir, path[:i+len("/vendor")]) {
+				if strings.HasSuffix(dir, path[:i+len("/vendor/")]) {
 					continue
 				}
 				// Make sure this vendor path is not already in the list for importArgs
-				vendorPath := dir + "/" + path[:i+len("/vendor")]
+				vendorPath := dir + "/" + path[:i+len("/vendor/")]
 				for _, imp := range importArgs {
 					if imp == "-I" {
 						continue
@@ -2788,7 +2790,12 @@
 	for _, f := range ofiles {
 		absOfiles = append(absOfiles, mkAbs(objDir, f))
 	}
-	return b.run(p.Dir, p.ImportPath, nil, "ar", "rc", mkAbs(objDir, afile), absOfiles)
+	absAfile := mkAbs(objDir, afile)
+	// Try with D modifier first, then without if that fails.
+	if b.run(p.Dir, p.ImportPath, nil, "ar", "rcD", absAfile, absOfiles) != nil {
+		return b.run(p.Dir, p.ImportPath, nil, "ar", "rc", absAfile, absOfiles)
+	}
+	return nil
 }
 
 func (tools gccgoToolchain) link(b *builder, root *action, out string, allactions []*action, mainpkg string, ofiles []string, buildmode, desc string) error {
@@ -3080,6 +3087,12 @@
 		defs = append(defs, "-fsplit-stack")
 	}
 	defs = tools.maybePIC(defs)
+	if b.gccSupportsFlag("-fdebug-prefix-map=a=b") {
+		defs = append(defs, "-fdebug-prefix-map="+b.work+"=/tmp/go-build")
+	}
+	if b.gccSupportsFlag("-gno-record-gcc-switches") {
+		defs = append(defs, "-gno-record-gcc-switches")
+	}
 	return b.run(p.Dir, p.ImportPath, nil, envList("CC", defaultCC), "-Wall", "-g",
 		"-I", objdir, "-I", inc, "-o", ofile, defs, "-c", cfile)
 }