Move gitSha1 build time variable from main to goenv package

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.
Этот коммит содержится в:
ZauberNerd 2022-03-14 21:22:12 +00:00 коммит произвёл Ayke
родитель 836ab95192
коммит d066b5c232
3 изменённых файлов: 11 добавлений и 11 удалений

Просмотреть файл

@ -208,7 +208,7 @@ lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a:
# Build the Go compiler. # Build the Go compiler.
tinygo: 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 @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 -ldflags="-X main.gitSha1=`git rev-parse --short HEAD`" . CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags byollvm -ldflags="-X github.com/tinygo-org/tinygo/goenv.GitSha1=`git rev-parse --short HEAD`" .
test: wasi-libc test: wasi-libc
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=20m -buildmode exe -tags byollvm ./builder ./cgo ./compileopts ./compiler ./interp ./transform . CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=20m -buildmode exe -tags byollvm ./builder ./cgo ./compileopts ./compiler ./interp ./transform .

Просмотреть файл

@ -14,6 +14,12 @@ import (
// Update this value before release of new version of software. // Update this value before release of new version of software.
const Version = "0.23.0-dev" const Version = "0.23.0-dev"
var (
// This variable is set at build time using -ldflags parameters.
// See: https://stackoverflow.com/a/11355611
GitSha1 string
)
// GetGorootVersion returns the major and minor version for a given GOROOT path. // GetGorootVersion returns the major and minor version for a given GOROOT path.
// If the goroot cannot be determined, (0, 0) is returned. // If the goroot cannot be determined, (0, 0) is returned.
func GetGorootVersion(goroot string) (major, minor int, err error) { func GetGorootVersion(goroot string) (major, minor int, err error) {

14
main.go
Просмотреть файл

@ -36,12 +36,6 @@ import (
"go.bug.st/serial/enumerator" "go.bug.st/serial/enumerator"
) )
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 // commandError is an error type to wrap os/exec.Command errors. This provides
// some more information regarding what went wrong while running a command. // some more information regarding what went wrong while running a command.
type commandError struct { type commandError struct {
@ -1023,8 +1017,8 @@ func getBMPPorts() (gdbPort, uartPort string, err error) {
func usage(command string) { func usage(command string) {
version := goenv.Version version := goenv.Version
if strings.HasSuffix(version, "-dev") && gitSha1 != "" { if strings.HasSuffix(version, "-dev") && goenv.GitSha1 != "" {
version += "-" + gitSha1 version += "-" + goenv.GitSha1
} }
switch command { switch command {
@ -1620,8 +1614,8 @@ func main() {
goversion = s goversion = s
} }
version := goenv.Version version := goenv.Version
if strings.HasSuffix(goenv.Version, "-dev") && gitSha1 != "" { if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" {
version += "-" + gitSha1 version += "-" + goenv.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) 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": case "env":