1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

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 <rsc@golang.org>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Russ Cox 2018-04-26 11:20:03 -04:00
parent aaa76ee82d
commit 4432cd1c27

View File

@ -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