Skip to content

Commit 6c38fab

Browse files
committed
src/runtime: add runtime.Version()
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.
1 parent de6493f commit 6c38fab

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

builder/build.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
204204
var packageJobs []*compileJob
205205
packageBitcodePaths := make(map[string]string)
206206
packageActionIDs := make(map[string]string)
207+
208+
if config.Options.GlobalValues["runtime"]["buildVersion"] == "" {
209+
version := goenv.Version
210+
if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" {
211+
version += "-" + goenv.GitSha1
212+
}
213+
if config.Options.GlobalValues == nil {
214+
config.Options.GlobalValues = make(map[string]map[string]string)
215+
}
216+
if config.Options.GlobalValues["runtime"] == nil {
217+
config.Options.GlobalValues["runtime"] = make(map[string]string)
218+
}
219+
config.Options.GlobalValues["runtime"]["buildVersion"] = version
220+
}
221+
207222
for _, pkg := range lprogram.Sorted() {
208223
pkg := pkg // necessary to avoid a race condition
209224

src/runtime/extern.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@ package runtime
33
func Callers(skip int, pc []uintptr) int {
44
return 0
55
}
6+
7+
// buildVersion is the Tinygo tree's version string at build time.
8+
//
9+
// This is set by the linker.
10+
var buildVersion string
11+
12+
// Version returns the Tinygo tree's version string.
13+
// It is the same as goenv.Version, or in case of a development build,
14+
// it will be the concatenation of goenv.Version and the git commit hash.
15+
func Version() string {
16+
return buildVersion
17+
}

0 commit comments

Comments
 (0)