diff options
| author | Jay Conrod <jayconrod@google.com> | 2021-09-30 14:37:30 -0700 |
|---|---|---|
| committer | Jay Conrod <jayconrod@google.com> | 2021-10-14 18:43:59 +0000 |
| commit | 765c9116be44641854f580c19e3589d7b86a3d28 (patch) | |
| tree | a7966442083ff5e4681a0470cc9bd38cc62fdcd5 /src/runtime/debug/mod.go | |
| parent | 011fd002457da0823da5f06b099fcf6e21444b00 (diff) | |
| download | go-765c9116be44641854f580c19e3589d7b86a3d28.tar.xz | |
cmd/go: move module build info formatting into runtime/debug
Previously, modload.PackageBuildInfo returned a string containing
information about modules used to build an executable. This string is
embedded in the binary and can be read with debug.ReadBuildInfo or
'go version -m'.
With this change, debug.BuildInfo now has a MarshalText method that
returns a string in the same format as modload.PackageBuildInfo.
Package.load now calls Package.setBuildInfo, which constructs a
debug.BuildInfo, formats it with MarshalText, then sets
Package.Internal.BuildInfo. This is equivalent to what
modload.PackageBuildInfo did.
modload.PackageBuildInfo is deleted, since it's no longer used.
For #37475
Change-Id: I5875a98cb64737637fec2a450ab2ffa7f1805707
Reviewed-on: https://go-review.googlesource.com/c/go/+/353886
Trust: Jay Conrod <jayconrod@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/runtime/debug/mod.go')
| -rw-r--r-- | src/runtime/debug/mod.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/runtime/debug/mod.go b/src/runtime/debug/mod.go index 05cad61155..11f995ba75 100644 --- a/src/runtime/debug/mod.go +++ b/src/runtime/debug/mod.go @@ -5,6 +5,8 @@ package debug import ( + "bytes" + "fmt" "strings" ) @@ -34,6 +36,41 @@ type Module struct { Replace *Module // replaced by this module } +func (bi *BuildInfo) MarshalText() ([]byte, error) { + buf := &bytes.Buffer{} + 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) + mv := m.Version + if mv == "" { + mv = "(devel)" + } + buf.WriteByte('\t') + buf.WriteString(mv) + if m.Replace == nil { + buf.WriteByte('\t') + buf.WriteString(m.Sum) + } else { + buf.WriteByte('\n') + formatMod("=>", *m.Replace) + } + buf.WriteByte('\n') + } + if bi.Main.Path != "" { + formatMod("mod", bi.Main) + } + for _, dep := range bi.Deps { + formatMod("dep", *dep) + } + + return buf.Bytes(), nil +} + func readBuildInfo(data string) (*BuildInfo, bool) { if len(data) < 32 { return nil, false |
