Skip to content

Commit 025d1c2

Browse files
committed
goenv: read git hash embedded in the binary
This PR makes two practical changes: * The git hash is 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`. * A flag is added to indicate whether the tree was modified. This might aid in helping people: it is now easier to see whether they have made local modifications.
1 parent 5862b48 commit 025d1c2

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

GNUmakefile

Lines changed: 1 addition & 1 deletion
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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,47 @@ 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+
hash := ""
29+
modified := false
30+
if info, ok := debug.ReadBuildInfo(); ok {
31+
for _, setting := range info.Settings {
32+
println("setting:", setting.Key, setting.Value)
33+
switch setting.Key {
34+
case "vcs.revision":
35+
hash = setting.Value[:8]
36+
case "vcs.modified":
37+
modified = setting.Value == "true"
38+
}
39+
}
40+
}
41+
value := hash
42+
if value != "" && modified {
43+
value += "-modified"
44+
}
45+
return value
46+
}
47+
3048
// GetGorootVersion returns the major and minor version for a given GOROOT path.
3149
// If the goroot cannot be determined, (0, 0) is returned.
3250
func GetGorootVersion() (major, minor int, err error) {

0 commit comments

Comments
 (0)