From 4432cd1c27c82c87fe2507a05a1a940f794b617f Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 26 Apr 2018 11:20:03 -0400 Subject: [PATCH] cmd/compilebench: fix GOROOT inconsistencies If GOROOT is unset (as is common now), then the output of "go env GOROOT" may differ from the GOROOT inferred by alternate compilers being executed and also from the value of runtime.GOROOT() inside compilebench, used by go/build. Harmonize all of these by setting GOROOT explicitly (will fix subcommands) and by invoking the go command to learn about packages instead of assuming go/build has any idea what it's doing. Change-Id: If97aa76cc2afec11a8404975f39329db7eb452e0 Reviewed-on: https://go-review.googlesource.com/109516 Run-TryBot: Russ Cox Reviewed-by: David Chase --- cmd/compilebench/main.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd/compilebench/main.go b/cmd/compilebench/main.go index d2cd70b2..4d3b74ef 100644 --- a/cmd/compilebench/main.go +++ b/cmd/compilebench/main.go @@ -67,9 +67,9 @@ package main import ( "bytes" + "encoding/json" "flag" "fmt" - "go/build" "io/ioutil" "log" "os" @@ -144,6 +144,7 @@ func main() { log.Fatalf("%s env GOROOT: %v", *flagGoCmd, err) } goroot = strings.TrimSpace(string(s)) + os.Setenv("GOROOT", goroot) // for any subcommands compiler = *flagCompiler if compiler == "" { @@ -250,11 +251,28 @@ func runBuild(name, dir string, count int) { return } - pkg, err := build.Import(dir, ".", 0) + // Make sure dependencies needed by go tool compile are installed to GOROOT/pkg. + out, err := exec.Command(*flagGoCmd, "build", "-i", dir).CombinedOutput() if err != nil { - log.Print(err) + log.Printf("go build -i %s: %v\n%s", dir, err, out) return } + + // Find dir and source file list. + var pkg struct { + Dir string + GoFiles []string + } + out, err = exec.Command(*flagGoCmd, "list", "-json", dir).Output() + if err != nil { + log.Printf("go list -json %s: %v\n", dir, err) + return + } + if err := json.Unmarshal(out, &pkg); err != nil { + log.Printf("go list -json %s: unmarshal: %v", dir, err) + return + } + args := []string{"-o", "_compilebench_.o"} if is6g { *flagMemprofilerate = -1