blob: 2989f21094336d6261ea337b9d83e6c1e0ed5bb4 [file] [log] [blame]
Serge Bazanskiab4ef132021-09-24 13:58:13 +02001// gotoolchain provides information about the Go toolchain used on the host by
2// rules_go.
3package gotoolchain
4
5import (
6 "fmt"
Serge Bazanskiae574132021-12-17 17:28:52 +01007 "os"
8 "strings"
Serge Bazanskiab4ef132021-09-24 13:58:13 +02009
10 "github.com/bazelbuild/rules_go/go/tools/bazel"
11)
12
13func mustRunfile(s string) string {
Serge Bazanskiae574132021-12-17 17:28:52 +010014 // When running as a tool (in a genrule, or a test, etc.), bazel.Runfile
15 // does not work. However, ${0}.runfiles/$runfile should be present. If so,
16 // return early and return that. Otherwise, carry on with bazel.Runfile.
17 //
18 // TODO(q3k): dig deeper into this and unify with //metropolis/cli/pkg/datafile.
19
20 // Ignore the error, worst case we get an empty string that will make a
21 // garbage path that won't point to a file.
22 ex, _ := os.Executable()
23 rf := ex + ".runfiles"
24 if _, err := os.Stat(rf); err == nil {
25 parts := strings.Split(s, "/")
26 parts[0] = rf
27 rf = strings.Join(parts, "/")
28 if _, err := os.Stat(rf); err == nil {
29 return rf
30 }
31 }
32
Serge Bazanskiab4ef132021-09-24 13:58:13 +020033 res, err := bazel.Runfile(s)
34 if err != nil {
35 panic(fmt.Sprintf("runfile %q not found: %v", s, err))
36 }
37 return res
38}
39
40var (
41 // Go is a path to the `go` executable.
42 Go = mustRunfile(`GOTOOL`)
43 // Root is the GOROOT path.
44 Root = mustRunfile(`GOROOT`)
45)