Skip to content

Commit

Permalink
src/runtime: add runtime.Version()
Browse files Browse the repository at this point in the history
This adds the `Version()` function of the `runtime` package which embeds
the go version that was used to build tinygo.

For programs that are compiled with tinygo the version can be overriden
via the:
`tinygo build -ldflags="-X 'runtime.buildVersion=abc'"` flag.
Otherwise it will continue to use the go version with which tinygo was
compiled.
  • Loading branch information
ZauberNerd authored and aykevl committed Mar 19, 2022
1 parent d066b5c commit 2fdcabd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
var packageJobs []*compileJob
packageBitcodePaths := make(map[string]string)
packageActionIDs := make(map[string]string)

if config.Options.GlobalValues["runtime"]["buildVersion"] == "" {
version := goenv.Version
if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" {
version += "-" + goenv.GitSha1
}
if config.Options.GlobalValues == nil {
config.Options.GlobalValues = make(map[string]map[string]string)
}
if config.Options.GlobalValues["runtime"] == nil {
config.Options.GlobalValues["runtime"] = make(map[string]string)
}
config.Options.GlobalValues["runtime"]["buildVersion"] = version
}

for _, pkg := range lprogram.Sorted() {
pkg := pkg // necessary to avoid a race condition

Expand Down
12 changes: 12 additions & 0 deletions src/runtime/extern.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ package runtime
func Callers(skip int, pc []uintptr) int {
return 0
}

// buildVersion is the Tinygo tree's version string at build time.
//
// This is set by the linker.
var buildVersion string

// Version returns the Tinygo tree's version string.
// It is the same as goenv.Version, or in case of a development build,
// it will be the concatenation of goenv.Version and the git commit hash.
func Version() string {
return buildVersion
}

0 comments on commit 2fdcabd

Please sign in to comment.