Skip to content

Commit 89340f8

Browse files
authored
Allows compilation of code using debug.BuildInfo and show correct tinygo version (#4343)
debug: Allows compilation of code using debug.BuildInfo and show correct tinygo version
1 parent 824bf24 commit 89340f8

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
.vscode
13
go.work
24
go.work.sum
35

src/runtime/debug/debug.go

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
// Package debug is a dummy package that is not yet implemented.
1+
// Portions copyright 2009 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package debug is a very partially implemented package to allow compilation.
26
package debug
37

8+
import (
9+
"fmt"
10+
"runtime"
11+
"strconv"
12+
"strings"
13+
)
14+
415
// SetMaxStack sets the maximum amount of memory that can be used by a single
516
// goroutine stack.
617
//
@@ -27,16 +38,17 @@ func Stack() []byte {
2738
//
2839
// Not implemented.
2940
func ReadBuildInfo() (info *BuildInfo, ok bool) {
30-
return nil, false
41+
return &BuildInfo{GoVersion: runtime.Compiler + runtime.Version()}, true
3142
}
3243

3344
// BuildInfo represents the build information read from
3445
// the running binary.
3546
type BuildInfo struct {
36-
Path string // The main package path
37-
Main Module // The module containing the main package
38-
Deps []*Module // Module dependencies
39-
Settings []BuildSetting
47+
GoVersion string // version of the Go toolchain that built the binary, e.g. "go1.19.2"
48+
Path string // The main package path
49+
Main Module // The module containing the main package
50+
Deps []*Module // Module dependencies
51+
Settings []BuildSetting
4052
}
4153

4254
type BuildSetting struct {
@@ -58,3 +70,60 @@ type Module struct {
5870
func SetGCPercent(n int) int {
5971
return n
6072
}
73+
74+
// Start of stolen from big go. TODO: import/reuse without copy pasta.
75+
76+
// quoteKey reports whether key is required to be quoted.
77+
func quoteKey(key string) bool {
78+
return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`")
79+
}
80+
81+
// quoteValue reports whether value is required to be quoted.
82+
func quoteValue(value string) bool {
83+
return strings.ContainsAny(value, " \t\r\n\"`")
84+
}
85+
86+
func (bi *BuildInfo) String() string {
87+
buf := new(strings.Builder)
88+
if bi.GoVersion != "" {
89+
fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion)
90+
}
91+
if bi.Path != "" {
92+
fmt.Fprintf(buf, "path\t%s\n", bi.Path)
93+
}
94+
var formatMod func(string, Module)
95+
formatMod = func(word string, m Module) {
96+
buf.WriteString(word)
97+
buf.WriteByte('\t')
98+
buf.WriteString(m.Path)
99+
buf.WriteByte('\t')
100+
buf.WriteString(m.Version)
101+
if m.Replace == nil {
102+
buf.WriteByte('\t')
103+
buf.WriteString(m.Sum)
104+
} else {
105+
buf.WriteByte('\n')
106+
formatMod("=>", *m.Replace)
107+
}
108+
buf.WriteByte('\n')
109+
}
110+
if bi.Main != (Module{}) {
111+
formatMod("mod", bi.Main)
112+
}
113+
for _, dep := range bi.Deps {
114+
formatMod("dep", *dep)
115+
}
116+
for _, s := range bi.Settings {
117+
key := s.Key
118+
if quoteKey(key) {
119+
key = strconv.Quote(key)
120+
}
121+
value := s.Value
122+
if quoteValue(value) {
123+
value = strconv.Quote(value)
124+
}
125+
fmt.Fprintf(buf, "build\t%s=%s\n", key, value)
126+
}
127+
128+
return buf.String()
129+
}

0 commit comments

Comments
 (0)