cmd/guru: if built with gccgo, switch to gc mode if GOROOT is valid

The gccgo compiler does not provide a gc-style GOROOT with standard
library sources. The effect is that guru may not fully work when using
gccgo. However, it can fully work if the GOROOT environment variable
points to valid gc-style GOROOT. In that case, make it work by telling
the go/build package to use gc mode.

Change-Id: Iadff8be61be8cc9a7ff2ca0a067b116b62895451
Reviewed-on: https://go-review.googlesource.com/117997
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/cmd/guru/main.go b/cmd/guru/main.go
index 761828a..8e4af00 100644
--- a/cmd/guru/main.go
+++ b/cmd/guru/main.go
@@ -19,6 +19,8 @@
 	"io"
 	"log"
 	"os"
+	"path/filepath"
+	"runtime"
 	"runtime/pprof"
 	"strings"
 	"sync"
@@ -38,6 +40,14 @@
 
 func init() {
 	flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
+
+	// gccgo does not provide a GOROOT with standard library sources.
+	// If we have one in the environment, force gc mode.
+	if build.Default.Compiler == "gccgo" {
+		if _, err := os.Stat(filepath.Join(runtime.GOROOT(), "src", "runtime", "runtime.go")); err == nil {
+			build.Default.Compiler = "gc"
+		}
+	}
 }
 
 const useHelp = "Run 'guru -help' for more information.\n"