From 5ad251b2bdd25232486cf8af1dfc7c032d7e6fae Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 17 Oct 2019 12:24:30 +0200 Subject: [PATCH] main: add go version to tinygo version This appears to be necessary for Visual Studio Code. --- main.go | 6 +++++- target.go | 47 +++++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index b68a24d2..b35f63b1 100644 --- a/main.go +++ b/main.go @@ -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 := "" + 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. diff --git a/target.go b/target.go index 92b4b564..655b5f24 100644 --- a/target.go +++ b/target.go @@ -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.