Commit fb54abe9 authored by Hiroshi Ioka's avatar Hiroshi Ioka Committed by Ian Lance Taylor

all: correct location of go tool

In general, there are no guarantee that `go` command exist on $PATH.
This CL tries to get `go` command from $GOROOT/bin instead.

There are three kinds of code we should handle:
    For normal code, the CL implements goCmd() or goCmdName().
    For unit tests, the CL uses testenv.GoTool() or testenv.GoToolPath().
    For integration tests, the CL sets PATH=$GOROOT/bin:$PATH in cmd/dist.

Note that make.bash sets PATH=$GOROOT/bin:$PATH in the build process.
So this change is only useful when we use toolchain manually.

Updates #21875

Change-Id: I963b9f22ea732dd735363ececde4cf94a5db5ca2
Reviewed-on: https://go-review.googlesource.com/64650
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 88ced021
...@@ -27,6 +27,18 @@ import ( ...@@ -27,6 +27,18 @@ import (
"strings" "strings"
) )
func goCmd() string {
var exeSuffix string
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
if _, err := os.Stat(path); err == nil {
return path
}
return "go"
}
// Flags // Flags
var ( var (
checkFile = flag.String("c", "", "optional comma-separated filename(s) to check API against") checkFile = flag.String("c", "", "optional comma-separated filename(s) to check API against")
...@@ -127,7 +139,7 @@ func main() { ...@@ -127,7 +139,7 @@ func main() {
if flag.NArg() > 0 { if flag.NArg() > 0 {
pkgNames = flag.Args() pkgNames = flag.Args()
} else { } else {
stds, err := exec.Command("go", "list", "std").Output() stds, err := exec.Command(goCmd(), "list", "std").Output()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"go/build" "go/build"
"internal/testenv"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
...@@ -163,7 +164,7 @@ func TestSkipInternal(t *testing.T) { ...@@ -163,7 +164,7 @@ func TestSkipInternal(t *testing.T) {
} }
func BenchmarkAll(b *testing.B) { func BenchmarkAll(b *testing.B) {
stds, err := exec.Command("go", "list", "std").Output() stds, err := exec.Command(testenv.GoToolPath(b), "list", "std").Output()
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
......
...@@ -14,8 +14,21 @@ import ( ...@@ -14,8 +14,21 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
) )
func goCmd() string {
var exeSuffix string
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
if _, err := os.Stat(path); err == nil {
return path
}
return "go"
}
var goroot string var goroot string
func main() { func main() {
...@@ -25,7 +38,7 @@ func main() { ...@@ -25,7 +38,7 @@ func main() {
log.Fatal("No $GOROOT set.") log.Fatal("No $GOROOT set.")
} }
out, err := exec.Command("go", "tool", "api", out, err := exec.Command(goCmd(), "tool", "api",
"-c", file("go1", "go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9"), "-c", file("go1", "go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9"),
"-next", file("next"), "-next", file("next"),
"-except", file("except")).CombinedOutput() "-except", file("except")).CombinedOutput()
......
...@@ -85,6 +85,14 @@ type distTest struct { ...@@ -85,6 +85,14 @@ type distTest struct {
} }
func (t *tester) run() { func (t *tester) run() {
var exeSuffix string
if goos == "windows" {
exeSuffix = ".exe"
}
if _, err := os.Stat(filepath.Join(gobin, "go"+exeSuffix)); err == nil {
os.Setenv("PATH", fmt.Sprintf("%s%c%s", gobin, os.PathListSeparator, os.Getenv("PATH")))
}
slurp, err := exec.Command("go", "env", "CGO_ENABLED").Output() slurp, err := exec.Command("go", "env", "CGO_ENABLED").Output()
if err != nil { if err != nil {
log.Fatalf("Error running go env CGO_ENABLED: %v", err) log.Fatalf("Error running go env CGO_ENABLED: %v", err)
......
...@@ -91,20 +91,25 @@ func TestMain(m *testing.M) { ...@@ -91,20 +91,25 @@ func TestMain(m *testing.M) {
if race.Enabled { if race.Enabled {
args = append(args, "-race") args = append(args, "-race")
} }
out, err := exec.Command("go", args...).CombinedOutput() gotool, err := testenv.GoTool()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
out, err := exec.Command(gotool, args...).CombinedOutput()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out) fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out)
os.Exit(2) os.Exit(2)
} }
out, err = exec.Command("go", "env", "GOROOT").CombinedOutput() out, err = exec.Command(gotool, "env", "GOROOT").CombinedOutput()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not find testing GOROOT: %v\n%s", err, out) fmt.Fprintf(os.Stderr, "could not find testing GOROOT: %v\n%s", err, out)
os.Exit(2) os.Exit(2)
} }
testGOROOT = strings.TrimSpace(string(out)) testGOROOT = strings.TrimSpace(string(out))
out, err = exec.Command("go", "env", "CC").CombinedOutput() out, err = exec.Command(gotool, "env", "CC").CombinedOutput()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not find testing CC: %v\n%s", err, out) fmt.Fprintf(os.Stderr, "could not find testing CC: %v\n%s", err, out)
os.Exit(2) os.Exit(2)
......
...@@ -22,7 +22,7 @@ func TestNooptCgoBuild(t *testing.T) { ...@@ -22,7 +22,7 @@ func TestNooptCgoBuild(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
cmd := exec.Command("go", "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out")) cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out"))
cmd.Dir = filepath.Join(runtime.GOROOT(), "src", "runtime", "testdata", "testprogcgo") cmd.Dir = filepath.Join(runtime.GOROOT(), "src", "runtime", "testdata", "testprogcgo")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
......
...@@ -15,10 +15,24 @@ import ( ...@@ -15,10 +15,24 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"runtime"
"github.com/google/pprof/profile" "github.com/google/pprof/profile"
) )
func goCmd() string {
var exeSuffix string
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
if _, err := os.Stat(path); err == nil {
return path
}
return "go"
}
func init() { func init() {
http.HandleFunc("/io", serveSVGProfile(pprofIO)) http.HandleFunc("/io", serveSVGProfile(pprofIO))
http.HandleFunc("/block", serveSVGProfile(pprofBlock)) http.HandleFunc("/block", serveSVGProfile(pprofBlock))
...@@ -147,7 +161,7 @@ func serveSVGProfile(prof func(w io.Writer) error) http.HandlerFunc { ...@@ -147,7 +161,7 @@ func serveSVGProfile(prof func(w io.Writer) error) http.HandlerFunc {
return return
} }
svgFilename := blockf.Name() + ".svg" svgFilename := blockf.Name() + ".svg"
if output, err := exec.Command("go", "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil { if output, err := exec.Command(goCmd(), "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil {
http.Error(w, fmt.Sprintf("failed to execute go tool pprof: %v\n%s", err, output), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to execute go tool pprof: %v\n%s", err, output), http.StatusInternalServerError)
return return
} }
......
...@@ -12,11 +12,25 @@ import ( ...@@ -12,11 +12,25 @@ import (
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
_ "unsafe" _ "unsafe"
) )
func goCmd() string {
var exeSuffix string
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
if _, err := os.Stat(path); err == nil {
return path
}
return "go"
}
// Event describes one event in the trace. // Event describes one event in the trace.
type Event struct { type Event struct {
Off int // offset in input file (for debugging and error reporting) Off int // offset in input file (for debugging and error reporting)
...@@ -757,7 +771,7 @@ func symbolize(events []*Event, bin string) error { ...@@ -757,7 +771,7 @@ func symbolize(events []*Event, bin string) error {
} }
// Start addr2line. // Start addr2line.
cmd := exec.Command("go", "tool", "addr2line", bin) cmd := exec.Command(goCmd(), "tool", "addr2line", bin)
in, err := cmd.StdinPipe() in, err := cmd.StdinPipe()
if err != nil { if err != nil {
return fmt.Errorf("failed to pipe addr2line stdin: %v", err) return fmt.Errorf("failed to pipe addr2line stdin: %v", err)
......
...@@ -811,7 +811,7 @@ func main() { ...@@ -811,7 +811,7 @@ func main() {
} }
exe := filepath.Join(tmpdir, "main.exe") exe := filepath.Join(tmpdir, "main.exe")
cmd := osexec.Command("go", "build", "-o", exe, src) cmd := osexec.Command(testenv.GoToolPath(t), "build", "-o", exe, src)
cmd.Dir = tmpdir cmd.Dir = tmpdir
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
......
...@@ -1043,7 +1043,7 @@ func BenchmarkRunningGoProgram(b *testing.B) { ...@@ -1043,7 +1043,7 @@ func BenchmarkRunningGoProgram(b *testing.B) {
} }
exe := filepath.Join(tmpdir, "main.exe") exe := filepath.Join(tmpdir, "main.exe")
cmd := exec.Command("go", "build", "-o", exe, src) cmd := exec.Command(testenv.GoToolPath(b), "build", "-o", exe, src)
cmd.Dir = tmpdir cmd.Dir = tmpdir
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment