Skip to content

Commit c728e03

Browse files
committed
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`.
1 parent ceb7891 commit c728e03

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

GNUmakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ endif
291291

292292
tinygo: ## Build the TinyGo compiler
293293
@if [ ! -f "$(LLVM_BUILDDIR)/bin/llvm-config" ]; then echo "Fetch and build LLVM first by running:"; echo " $(MAKE) llvm-source"; echo " $(MAKE) $(LLVM_BUILDDIR)"; exit 1; fi
294-
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GOENVFLAGS) $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags "byollvm osusergo" -ldflags="-X github.com/tinygo-org/tinygo/goenv.GitSha1=`git rev-parse --short HEAD`" .
294+
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GOENVFLAGS) $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags "byollvm osusergo" .
295295
test: wasi-libc check-nodejs-version
296296
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=1h -buildmode exe -tags "byollvm osusergo" $(GOTESTPKGS)
297297

goenv/version.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"runtime/debug"
78
"strings"
89
)
910

1011
// Version of TinyGo.
1112
// Update this value before release of new version of software.
1213
const version = "0.35.0-dev"
1314

14-
var (
15-
// This variable is set at build time using -ldflags parameters.
16-
// See: https://stackoverflow.com/a/11355611
17-
GitSha1 string
18-
)
19-
2015
// Return TinyGo version, either in the form 0.30.0 or as a development version
2116
// (like 0.30.0-dev-abcd012).
2217
func Version() string {
2318
v := version
24-
if strings.HasSuffix(version, "-dev") && GitSha1 != "" {
25-
v += "-" + GitSha1
19+
if strings.HasSuffix(version, "-dev") {
20+
if hash := readGitHash(); hash != "" {
21+
v += "-" + hash
22+
}
2623
}
2724
return v
2825
}
2926

27+
func readGitHash() string {
28+
if info, ok := debug.ReadBuildInfo(); ok {
29+
for _, setting := range info.Settings {
30+
if setting.Key == "vcs.revision" {
31+
return setting.Value[:8]
32+
}
33+
}
34+
}
35+
return ""
36+
}
37+
3038
// GetGorootVersion returns the major and minor version for a given GOROOT path.
3139
// If the goroot cannot be determined, (0, 0) is returned.
3240
func GetGorootVersion() (major, minor int, err error) {

0 commit comments

Comments
 (0)