Skip to content

Commit 82fc265

Browse files
ZauberNerddeadprogram
authored andcommitted
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.
1 parent 51d538b commit 82fc265

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a:
208208
# Build the Go compiler.
209209
tinygo:
210210
@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
211-
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`" .
211+
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`" .
212212

213213
test: wasi-libc
214214
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 .

goenv/version.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414
// Update this value before release of new version of software.
1515
const Version = "0.23.0-dev"
1616

17+
var (
18+
// This variable is set at build time using -ldflags parameters.
19+
// See: https://stackoverflow.com/a/11355611
20+
GitSha1 string
21+
)
22+
1723
// GetGorootVersion returns the major and minor version for a given GOROOT path.
1824
// If the goroot cannot be determined, (0, 0) is returned.
1925
func GetGorootVersion(goroot string) (major, minor int, err error) {

main.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ import (
3636
"go.bug.st/serial/enumerator"
3737
)
3838

39-
var (
40-
// This variable is set at build time using -ldflags parameters.
41-
// See: https://stackoverflow.com/a/11355611
42-
gitSha1 string
43-
)
44-
4539
// commandError is an error type to wrap os/exec.Command errors. This provides
4640
// some more information regarding what went wrong while running a command.
4741
type commandError struct {
@@ -1023,8 +1017,8 @@ func getBMPPorts() (gdbPort, uartPort string, err error) {
10231017

10241018
func usage(command string) {
10251019
version := goenv.Version
1026-
if strings.HasSuffix(version, "-dev") && gitSha1 != "" {
1027-
version += "-" + gitSha1
1020+
if strings.HasSuffix(version, "-dev") && goenv.GitSha1 != "" {
1021+
version += "-" + goenv.GitSha1
10281022
}
10291023

10301024
switch command {
@@ -1620,8 +1614,8 @@ func main() {
16201614
goversion = s
16211615
}
16221616
version := goenv.Version
1623-
if strings.HasSuffix(goenv.Version, "-dev") && gitSha1 != "" {
1624-
version += "-" + gitSha1
1617+
if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" {
1618+
version += "-" + goenv.GitSha1
16251619
}
16261620
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
16271621
case "env":

0 commit comments

Comments
 (0)