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.
2
6
package debug
3
7
8
+ import (
9
+ "fmt"
10
+ "runtime"
11
+ "strconv"
12
+ "strings"
13
+ )
14
+
4
15
// SetMaxStack sets the maximum amount of memory that can be used by a single
5
16
// goroutine stack.
6
17
//
@@ -27,16 +38,17 @@ func Stack() []byte {
27
38
//
28
39
// Not implemented.
29
40
func ReadBuildInfo () (info * BuildInfo , ok bool ) {
30
- return nil , false
41
+ return & BuildInfo { GoVersion : runtime . Compiler + runtime . Version ()}, true
31
42
}
32
43
33
44
// BuildInfo represents the build information read from
34
45
// the running binary.
35
46
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
40
52
}
41
53
42
54
type BuildSetting struct {
@@ -58,3 +70,60 @@ type Module struct {
58
70
func SetGCPercent (n int ) int {
59
71
return n
60
72
}
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