Skip to content

Commit a040e74

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 095312f commit a040e74

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

builder/build.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
195195
var packageJobs []*compileJob
196196
packageBitcodePaths := make(map[string]string)
197197
packageActionIDs := make(map[string]string)
198+
199+
if config.Options.GlobalValues["runtime"]["buildVersion"] == "" {
200+
version, _ := goenv.GorootVersionString(goenv.Get("GOROOT"))
201+
config.Options.GlobalValues["runtime"] = map[string]string{"buildVersion": version}
202+
}
203+
198204
for _, pkg := range lprogram.Sorted() {
199205
pkg := pkg // necessary to avoid a race condition
200206

src/runtime/extern.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@ package runtime
33
func Callers(skip int, pc []uintptr) int {
44
return 0
55
}
6+
7+
// buildVersion is the Go tree's version string at build time.
8+
//
9+
// This is set by the linker.
10+
//
11+
// This is accessed by "go version <binary>".
12+
var buildVersion string
13+
14+
// Version returns the Go tree's version string.
15+
// It is either the commit hash and date at the time of the build or,
16+
// when possible, a release tag like "go1.3".
17+
func Version() string {
18+
return buildVersion
19+
}

0 commit comments

Comments
 (0)