Skip to content

Commit 5ad251b

Browse files
aykevldeadprogram
authored andcommitted
main: add go version to tinygo version
This appears to be necessary for Visual Studio Code.
1 parent 5324fb7 commit 5ad251b

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,11 @@ func main() {
896896
case "help":
897897
usage()
898898
case "version":
899-
fmt.Printf("tinygo version %s %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
899+
goversion := "<unknown>"
900+
if s, err := getGorootVersionString(goenv.Get("GOROOT")); err == nil {
901+
goversion = s
902+
}
903+
fmt.Printf("tinygo version %s %s/%s (using go version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion)
900904
case "env":
901905
if flag.NArg() == 0 {
902906
// Show all environment variables.

target.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,8 @@ func (spec *TargetSpec) OpenOCDConfiguration() (args []string, err error) {
308308
// getGorootVersion returns the major and minor version for a given GOROOT path.
309309
// If the goroot cannot be determined, (0, 0) is returned.
310310
func getGorootVersion(goroot string) (major, minor int, err error) {
311-
var s string
312-
var n int
313-
var trailing string
314-
315-
if data, err := ioutil.ReadFile(filepath.Join(
316-
goroot, "src", "runtime", "internal", "sys", "zversion.go")); err == nil {
317-
318-
r := regexp.MustCompile("const TheVersion = `(.*)`")
319-
matches := r.FindSubmatch(data)
320-
if len(matches) != 2 {
321-
return 0, 0, errors.New("Invalid go version output:\n" + string(data))
322-
}
323-
324-
s = string(matches[1])
325-
326-
} else if data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")); err == nil {
327-
s = string(data)
328-
329-
} else {
311+
s, err := getGorootVersionString(goroot)
312+
if err != nil {
330313
return 0, 0, err
331314
}
332315

@@ -340,7 +323,8 @@ func getGorootVersion(goroot string) (major, minor int, err error) {
340323
}
341324

342325
// Ignore the errors, we don't really handle errors here anyway.
343-
n, err = fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing)
326+
var trailing string
327+
n, err := fmt.Sscanf(s, "go%d.%d%s", &major, &minor, &trailing)
344328
if n == 2 && err == io.EOF {
345329
// Means there were no trailing characters (i.e., not an alpha/beta)
346330
err = nil
@@ -351,6 +335,29 @@ func getGorootVersion(goroot string) (major, minor int, err error) {
351335
return
352336
}
353337

338+
// getGorootVersionString returns the version string as reported by the Go
339+
// toolchain for the given GOROOT path. It is usually of the form `go1.x.y` but
340+
// can have some variations (for beta releases, for example).
341+
func getGorootVersionString(goroot string) (string, error) {
342+
if data, err := ioutil.ReadFile(filepath.Join(
343+
goroot, "src", "runtime", "internal", "sys", "zversion.go")); err == nil {
344+
345+
r := regexp.MustCompile("const TheVersion = `(.*)`")
346+
matches := r.FindSubmatch(data)
347+
if len(matches) != 2 {
348+
return "", errors.New("Invalid go version output:\n" + string(data))
349+
}
350+
351+
return string(matches[1]), nil
352+
353+
} else if data, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION")); err == nil {
354+
return string(data), nil
355+
356+
} else {
357+
return "", err
358+
}
359+
}
360+
354361
// getClangHeaderPath returns the path to the built-in Clang headers. It tries
355362
// multiple locations, which should make it find the directory when installed in
356363
// various ways.

0 commit comments

Comments
 (0)