builder: move Go version code to goenv package
This is necessary to avoid a circular dependency in the loader (which soon will need to read the Go version) and because it seems like a better place anyway.
Этот коммит содержится в:
родитель
4918395f88
коммит
2a98433c8e
5 изменённых файлов: 67 добавлений и 61 удалений
|
@ -21,7 +21,7 @@ func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
|
||||||
if goroot == "" {
|
if goroot == "" {
|
||||||
return nil, errors.New("cannot locate $GOROOT, please set it manually")
|
return nil, errors.New("cannot locate $GOROOT, please set it manually")
|
||||||
}
|
}
|
||||||
major, minor, err := getGorootVersion(goroot)
|
major, minor, err := goenv.GetGorootVersion(goroot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not read version from GOROOT (%v): %v", goroot, err)
|
return nil, fmt.Errorf("could not read version from GOROOT (%v): %v", goroot, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,71 +1,13 @@
|
||||||
package builder
|
package builder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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) {
|
|
||||||
s, err := GorootVersionString(goroot)
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if s == "" || s[:2] != "go" {
|
|
||||||
return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := strings.Split(s[2:], ".")
|
|
||||||
if len(parts) < 2 {
|
|
||||||
return 0, 0, errors.New("could not parse Go version: version has less than two parts")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore the errors, we don't really handle errors here anyway.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, fmt.Errorf("failed to parse version: %s", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GorootVersionString 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 GorootVersionString(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
|
// getClangHeaderPath returns the path to the built-in Clang headers. It tries
|
||||||
// multiple locations, which should make it find the directory when installed in
|
// multiple locations, which should make it find the directory when installed in
|
||||||
// various ways.
|
// various ways.
|
||||||
|
|
64
goenv/version.go
Обычный файл
64
goenv/version.go
Обычный файл
|
@ -0,0 +1,64 @@
|
||||||
|
package goenv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
s, err := GorootVersionString(goroot)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if s == "" || s[:2] != "go" {
|
||||||
|
return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(s[2:], ".")
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return 0, 0, errors.New("could not parse Go version: version has less than two parts")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore the errors, we don't really handle errors here anyway.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, fmt.Errorf("failed to parse version: %s", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GorootVersionString 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 GorootVersionString(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
|
||||||
|
}
|
||||||
|
}
|
2
main.go
2
main.go
|
@ -897,7 +897,7 @@ func main() {
|
||||||
usage()
|
usage()
|
||||||
case "version":
|
case "version":
|
||||||
goversion := "<unknown>"
|
goversion := "<unknown>"
|
||||||
if s, err := builder.GorootVersionString(goenv.Get("GOROOT")); err == nil {
|
if s, err := goenv.GorootVersionString(goenv.Get("GOROOT")); err == nil {
|
||||||
goversion = s
|
goversion = s
|
||||||
}
|
}
|
||||||
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
|
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
|
||||||
|
|
|
@ -73,7 +73,7 @@ func TestCompiler(t *testing.T) {
|
||||||
t.Run("ARM64Linux", func(t *testing.T) {
|
t.Run("ARM64Linux", func(t *testing.T) {
|
||||||
runPlatTests("aarch64--linux-gnu", matches, t)
|
runPlatTests("aarch64--linux-gnu", matches, t)
|
||||||
})
|
})
|
||||||
goVersion, err := builder.GorootVersionString(goenv.Get("GOROOT"))
|
goVersion, err := goenv.GorootVersionString(goenv.Get("GOROOT"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("could not get Go version:", err)
|
t.Error("could not get Go version:", err)
|
||||||
return
|
return
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче