diff --git a/Makefile b/Makefile index 425a0cfc..6dd372b9 100644 --- a/Makefile +++ b/Makefile @@ -179,7 +179,7 @@ lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a: # Build the Go compiler. tinygo: @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 - CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags byollvm . + 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`" . test: wasi-libc CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -buildmode exe -tags byollvm ./cgo ./compileopts ./interp ./transform . diff --git a/main.go b/main.go index 36756354..a245bddb 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,12 @@ import ( "go.bug.st/serial" ) +var ( + // This variable is set at build time using -ldflags parameters. + // See: https://stackoverflow.com/a/11355611 + gitSha1 string +) + // commandError is an error type to wrap os/exec.Command errors. This provides // some more information regarding what went wrong while running a command. type commandError struct { @@ -995,7 +1001,11 @@ func main() { if s, err := goenv.GorootVersionString(goenv.Get("GOROOT")); err == nil { goversion = s } - 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) + version := goenv.Version + if strings.HasSuffix(goenv.Version, "-dev") && gitSha1 != "" { + version += "-" + gitSha1 + } + fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version) case "env": if flag.NArg() == 0 { // Show all environment variables.