Skip to content

Commit e5e324f

Browse files
authored
main: embed git-hash in tinygo-dev executable
1 parent 4fa1fc6 commit e5e324f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a:
179179
# Build the Go compiler.
180180
tinygo:
181181
@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
182-
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags byollvm .
182+
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags byollvm -ldflags="-X main.gitSha1=`git rev-parse --short HEAD`" .
183183

184184
test: wasi-libc
185185
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -buildmode exe -tags byollvm ./cgo ./compileopts ./interp ./transform .

main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ import (
2828
"go.bug.st/serial"
2929
)
3030

31+
var (
32+
// This variable is set at build time using -ldflags parameters.
33+
// See: https://stackoverflow.com/a/11355611
34+
gitSha1 string
35+
)
36+
3137
// commandError is an error type to wrap os/exec.Command errors. This provides
3238
// some more information regarding what went wrong while running a command.
3339
type commandError struct {
@@ -995,7 +1001,11 @@ func main() {
9951001
if s, err := goenv.GorootVersionString(goenv.Get("GOROOT")); err == nil {
9961002
goversion = s
9971003
}
998-
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", goenv.Version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
1004+
version := goenv.Version
1005+
if strings.HasSuffix(goenv.Version, "-dev") && gitSha1 != "" {
1006+
version += "-" + gitSha1
1007+
}
1008+
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
9991009
case "env":
10001010
if flag.NArg() == 0 {
10011011
// Show all environment variables.

0 commit comments

Comments
 (0)