From a6971f4c117642c93184cda48ee409aea4f53ada Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Mon, 8 May 2017 15:47:55 -0700 Subject: [PATCH] cmd/compilebench: use go command in goroot, not environment The "go" command is a random "go" in the environment, not necessarily the one under test. Use the go command in the goroot we're testing. This CL removes the need to add $GOROOT/bin to your path before running compilebench. Change-Id: Ieb7f441f8287105e13446006e73b760d80e51e03 Reviewed-on: https://go-review.googlesource.com/42932 Run-TryBot: Keith Randall Reviewed-by: Josh Bleecher Snyder TryBot-Result: Gobot Gobot --- cmd/compilebench/main.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/compilebench/main.go b/cmd/compilebench/main.go index e04bd61f..d2cd70b2 100644 --- a/cmd/compilebench/main.go +++ b/cmd/compilebench/main.go @@ -76,20 +76,20 @@ import ( "os/exec" "path/filepath" "regexp" - "runtime" "strconv" "strings" "time" ) var ( - goroot = runtime.GOROOT() + goroot string compiler string runRE *regexp.Regexp is6g bool ) var ( + flagGoCmd = flag.String("go", "go", "path to \"go\" command") flagAlloc = flag.Bool("alloc", false, "report allocations") flagObj = flag.Bool("obj", false, "report object file stats") flagCompiler = flag.String("compile", "", "use `exe` as the cmd/compile binary") @@ -139,14 +139,20 @@ func main() { usage() } + s, err := exec.Command(*flagGoCmd, "env", "GOROOT").CombinedOutput() + if err != nil { + log.Fatalf("%s env GOROOT: %v", *flagGoCmd, err) + } + goroot = strings.TrimSpace(string(s)) + compiler = *flagCompiler if compiler == "" { - out, err := exec.Command("go", "tool", "-n", "compile").CombinedOutput() + out, err := exec.Command(*flagGoCmd, "tool", "-n", "compile").CombinedOutput() if err != nil { - out, err = exec.Command("go", "tool", "-n", "6g").CombinedOutput() + out, err = exec.Command(*flagGoCmd, "tool", "-n", "6g").CombinedOutput() is6g = true if err != nil { - out, err = exec.Command("go", "tool", "-n", "compile").CombinedOutput() + out, err = exec.Command(*flagGoCmd, "tool", "-n", "compile").CombinedOutput() log.Fatalf("go tool -n compiler: %v\n%s", err, out) } } @@ -193,14 +199,14 @@ func runStdCmd() { args = append(args, "-gcflags", *flagCompilerFlags) } args = append(args, "std", "cmd") - cmd := exec.Command("go", args...) - cmd.Dir = filepath.Join(runtime.GOROOT(), "src") + cmd := exec.Command(*flagGoCmd, args...) + cmd.Dir = filepath.Join(goroot, "src") runCmd("BenchmarkStdCmd", cmd) } // path is either a path to a file ("$GOROOT/test/helloworld.go") or a package path ("cmd/go"). func runSize(name, path string) { - cmd := exec.Command("go", "build", "-o", "_compilebenchout_", path) + cmd := exec.Command(*flagGoCmd, "build", "-o", "_compilebenchout_", path) cmd.Stdout = os.Stderr cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -240,7 +246,7 @@ func runBuild(name, dir string, count int) { runSize("BenchmarkCmdGoSize", "cmd/go") return case "BenchmarkHelloSize": - runSize("BenchmarkHelloSize", filepath.Join(runtime.GOROOT(), "test/helloworld.go")) + runSize("BenchmarkHelloSize", filepath.Join(goroot, "test/helloworld.go")) return }