mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-10 05:53:39 +00:00
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
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
.DS_Store
|
||||||
|
.vscode
|
||||||
go.work
|
go.work
|
||||||
go.work.sum
|
go.work.sum
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,17 @@
|
|||||||
// Package debug is a dummy package that is not yet implemented.
|
// Portions copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package debug is a very partially implemented package to allow compilation.
|
||||||
package debug
|
package debug
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// SetMaxStack sets the maximum amount of memory that can be used by a single
|
// SetMaxStack sets the maximum amount of memory that can be used by a single
|
||||||
// goroutine stack.
|
// goroutine stack.
|
||||||
//
|
//
|
||||||
@@ -27,16 +38,17 @@ func Stack() []byte {
|
|||||||
//
|
//
|
||||||
// Not implemented.
|
// Not implemented.
|
||||||
func ReadBuildInfo() (info *BuildInfo, ok bool) {
|
func ReadBuildInfo() (info *BuildInfo, ok bool) {
|
||||||
return nil, false
|
return &BuildInfo{GoVersion: runtime.Compiler + runtime.Version()}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildInfo represents the build information read from
|
// BuildInfo represents the build information read from
|
||||||
// the running binary.
|
// the running binary.
|
||||||
type BuildInfo struct {
|
type BuildInfo struct {
|
||||||
Path string // The main package path
|
GoVersion string // version of the Go toolchain that built the binary, e.g. "go1.19.2"
|
||||||
Main Module // The module containing the main package
|
Path string // The main package path
|
||||||
Deps []*Module // Module dependencies
|
Main Module // The module containing the main package
|
||||||
Settings []BuildSetting
|
Deps []*Module // Module dependencies
|
||||||
|
Settings []BuildSetting
|
||||||
}
|
}
|
||||||
|
|
||||||
type BuildSetting struct {
|
type BuildSetting struct {
|
||||||
@@ -58,3 +70,60 @@ type Module struct {
|
|||||||
func SetGCPercent(n int) int {
|
func SetGCPercent(n int) int {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start of stolen from big go. TODO: import/reuse without copy pasta.
|
||||||
|
|
||||||
|
// quoteKey reports whether key is required to be quoted.
|
||||||
|
func quoteKey(key string) bool {
|
||||||
|
return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`")
|
||||||
|
}
|
||||||
|
|
||||||
|
// quoteValue reports whether value is required to be quoted.
|
||||||
|
func quoteValue(value string) bool {
|
||||||
|
return strings.ContainsAny(value, " \t\r\n\"`")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bi *BuildInfo) String() string {
|
||||||
|
buf := new(strings.Builder)
|
||||||
|
if bi.GoVersion != "" {
|
||||||
|
fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion)
|
||||||
|
}
|
||||||
|
if bi.Path != "" {
|
||||||
|
fmt.Fprintf(buf, "path\t%s\n", bi.Path)
|
||||||
|
}
|
||||||
|
var formatMod func(string, Module)
|
||||||
|
formatMod = func(word string, m Module) {
|
||||||
|
buf.WriteString(word)
|
||||||
|
buf.WriteByte('\t')
|
||||||
|
buf.WriteString(m.Path)
|
||||||
|
buf.WriteByte('\t')
|
||||||
|
buf.WriteString(m.Version)
|
||||||
|
if m.Replace == nil {
|
||||||
|
buf.WriteByte('\t')
|
||||||
|
buf.WriteString(m.Sum)
|
||||||
|
} else {
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
formatMod("=>", *m.Replace)
|
||||||
|
}
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
}
|
||||||
|
if bi.Main != (Module{}) {
|
||||||
|
formatMod("mod", bi.Main)
|
||||||
|
}
|
||||||
|
for _, dep := range bi.Deps {
|
||||||
|
formatMod("dep", *dep)
|
||||||
|
}
|
||||||
|
for _, s := range bi.Settings {
|
||||||
|
key := s.Key
|
||||||
|
if quoteKey(key) {
|
||||||
|
key = strconv.Quote(key)
|
||||||
|
}
|
||||||
|
value := s.Value
|
||||||
|
if quoteValue(value) {
|
||||||
|
value = strconv.Quote(value)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(buf, "build\t%s=%s\n", key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user