main: add go version to tinygo version

This appears to be necessary for Visual Studio Code.
Этот коммит содержится в:
Ayke van Laethem 2019-10-17 12:24:30 +02:00 коммит произвёл Ron Evans
родитель 5324fb7a40
коммит 5ad251b2bd
2 изменённых файлов: 32 добавлений и 21 удалений

Просмотреть файл

@ -896,7 +896,11 @@ func main() {
case "help":
usage()
case "version":
fmt.Printf("tinygo version %s %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
goversion := "<unknown>"
if s, err := getGorootVersionString(goenv.Get("GOROOT")); err == nil {
goversion = s
}
fmt.Printf("tinygo version %s %s/%s (using go version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion)
case "env":
if flag.NArg() == 0 {
// Show all environment variables.

Просмотреть файл

@ -308,25 +308,8 @@ func (spec *TargetSpec) OpenOCDConfiguration() (args []string, err error) {
// getGorootVersion returns the major and minor version for a given GOROOT path.
// If the goroot cannot be determined, (0, 0) is returned.
func getGorootVersion(goroot string) (major, minor int, err error) {
var s string
var n int
var trailing string
if data, err := ioutil.ReadFile(filepath.Join(
goroot, "src", "runtime", "internal", "sys", "zversion.go")); err == nil {
r := regexp.MustCompile("const TheVersion = `(.*)`")
matches := r.FindSubmatch(data)
if len(matches) != 2 {
return 0, 0, errors.New("Invalid go version output:\n" + string(data))
}
s = string(matches[1])
} else if data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")); err == nil {
s = string(data)
} else {
s, err := getGorootVersionString(goroot)
if err != nil {
return 0, 0, err
}
@ -340,7 +323,8 @@ func getGorootVersion(goroot string) (major, minor int, err error) {
}
// Ignore the errors, we don't really handle errors here anyway.
n, err = fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing)
var trailing string
n, err := fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing)
if n == 2 && err == io.EOF {
// Means there were no trailing characters (i.e., not an alpha/beta)
err = nil
@ -351,6 +335,29 @@ func getGorootVersion(goroot string) (major, minor int, err error) {
return
}
// getGorootVersionString returns the version string as reported by the Go
// toolchain for the given GOROOT path. It is usually of the form `go1.x.y` but
// can have some variations (for beta releases, for example).
func getGorootVersionString(goroot string) (string, error) {
if data, err := ioutil.ReadFile(filepath.Join(
goroot, "src", "runtime", "internal", "sys", "zversion.go")); err == nil {
r := regexp.MustCompile("const TheVersion = `(.*)`")
matches := r.FindSubmatch(data)
if len(matches) != 2 {
return "", errors.New("Invalid go version output:\n" + string(data))
}
return string(matches[1]), nil
} else if data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")); err == nil {
return string(data), nil
} else {
return "", err
}
}
// getClangHeaderPath returns the path to the built-in Clang headers. It tries
// multiple locations, which should make it find the directory when installed in
// various ways.