mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 15:03:41 +00:00
goenv: read git hash embedded in the binary
The git hash that's part of `tinygo version` is now read from the binary itself instead of embedding it with a `-ldflags` flag. This means it is also present when building TinyGo using `go build` or `go install`.
This commit is contained in:
+16
-8
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -11,22 +12,29 @@ import (
|
||||
// Update this value before release of new version of software.
|
||||
const version = "0.35.0-dev"
|
||||
|
||||
var (
|
||||
// This variable is set at build time using -ldflags parameters.
|
||||
// See: https://stackoverflow.com/a/11355611
|
||||
GitSha1 string
|
||||
)
|
||||
|
||||
// Return TinyGo version, either in the form 0.30.0 or as a development version
|
||||
// (like 0.30.0-dev-abcd012).
|
||||
func Version() string {
|
||||
v := version
|
||||
if strings.HasSuffix(version, "-dev") && GitSha1 != "" {
|
||||
v += "-" + GitSha1
|
||||
if strings.HasSuffix(version, "-dev") {
|
||||
if hash := readGitHash(); hash != "" {
|
||||
v += "-" + hash
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func readGitHash() string {
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
for _, setting := range info.Settings {
|
||||
if setting.Key == "vcs.revision" {
|
||||
return setting.Value[:8]
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetGorootVersion returns the major and minor version for a given GOROOT path.
|
||||
// If the goroot cannot be determined, (0, 0) is returned.
|
||||
func GetGorootVersion() (major, minor int, err error) {
|
||||
|
||||
Reference in New Issue
Block a user