-
Notifications
You must be signed in to change notification settings - Fork 917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src/runtime: implement runtime.Version() function #2676
Conversation
src/runtime/extern.go
Outdated
// It is either the commit hash and date at the time of the build or, | ||
// when possible, a release tag like "go1.3". | ||
func Version() string { | ||
return "TODO: not implemented" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually something that's not too difficult to support. The compiler already supports the -X
flag and we could add the version relatively easily. See these lines for a starting point:
Lines 846 to 850 in 3883550
// Insert values from -ldflags="-X ..." into the IR. | |
err = setGlobalValues(mod, config.Options.GlobalValues) | |
if err != nil { | |
return err | |
} |
We could for example add runtime.buildVersion
to GlobalValues
earlier in the build. The version itself can be obtained using goenv.GorootVersionString
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the PR to your suggestion.
Now the tinygo runtime.Version() is always the version of golang that was used to build tinygo, but not the version of tinygo itself, which I think would be more useful?
Also, for some reason I wasn't able to set the version via go build -tags=llvm11 -ldflags="-X 'runtime.buildVersion=abc'" -o build/tinygo
- shouldn't that work too, so that tinygo could set its own version during build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the tinygo runtime.Version() is always the version of golang that was used to build tinygo, but not the version of tinygo itself, which I think would be more useful?
I agree that we should return the version of TinyGo used to build the binary here, which is what the main Go seems to do with respect to the version of Go used to build binary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved and exported main.gitSha1
to goenv.GitSha1
to be able to access it from both the main
and builder
package.
Now the runtime.Version()
function returns a version string similar to what tinygo version
returns (0.23.0
- or, if it ends in -dev
it will be the concatenation of the version and git sha 0.23.0-dev-f5170c0b
).
a040e74
to
c094a2d
Compare
0337f0d
to
f5170c0
Compare
Edit: I found The other ones seem to come from wasmtime:
And they look like:
Apparently there's a switch to allow unknown exports: bytecodealliance/wasmtime#2879, but I want to first understand why and how this error came to be before enabling any further options. |
Adding the Lines 276 to 305 in 3883550
does fix the issue, but I don't understand why it is necessary. And when I just build the test file
Which I assume has somehow to do with the variable being set by the linker? |
Ok, so in the linked issue the proposed fix is to remove Line 14 in 3883550
|
Compiling a minimal program: package main
import "runtime"
func main() {
println(runtime.Version())
} to WASI And looking at the resulting WAT files in both cases it seems the only change is whether the |
Moving and exporting this variable from the main to the goenv package allows us to use it from both the main and the builder package. This is done in preparation to include the value in `tinygo build` linker flags, so that we can embed the version and git sha into binaries built with tinygo.
f5170c0
to
64d9efa
Compare
Edit: Nevermind, seems like I overwrote |
64d9efa
to
f03949f
Compare
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.
Exporting symbols seems to embed them in the WASM exports section which causes wasmtime to fail: bytecodealliance/wasmtime#2587 As a workaround, it is possible to specify the `--allow-unknown-exports` flag on wasmtime. But as discussed in the above linked issue, this seems to only be a workaround. For the Rust compiler the fix was to remove the `--export-dynamic` linker flag when targeting `wasm32-wasi`: rust-lang/rust#81255 Which is waht this commit does for Tinygo too.
f03949f
to
aab7419
Compare
Thank you! This looks good to me. I hope that |
Note that programs using |
This adds the
Version()
function of theruntime
package which embedsthe 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.