2019-09-05 01:23:54 +05:30
|
|
|
// Copyright 2018 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 packagesdriver fetches type sizes for go/packages and go/analysis.
|
|
|
|
package packagesdriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"go/types"
|
|
|
|
"strings"
|
2020-04-04 00:59:12 +05:30
|
|
|
|
|
|
|
"golang.org/x/tools/internal/gocommand"
|
2019-09-05 01:23:54 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var debug = false
|
|
|
|
|
2020-12-12 05:46:53 +05:30
|
|
|
func GetSizesGolist(ctx context.Context, inv gocommand.Invocation, gocmdRunner *gocommand.Runner) (types.Sizes, error) {
|
|
|
|
inv.Verb = "list"
|
|
|
|
inv.Args = []string{"-f", "{{context.GOARCH}} {{context.Compiler}}", "--", "unsafe"}
|
2020-08-15 22:43:07 +05:30
|
|
|
stdout, stderr, friendlyErr, rawErr := gocmdRunner.RunRaw(ctx, inv)
|
2019-09-05 01:23:54 +05:30
|
|
|
var goarch, compiler string
|
2020-04-04 00:59:12 +05:30
|
|
|
if rawErr != nil {
|
|
|
|
if strings.Contains(rawErr.Error(), "cannot find main module") {
|
2019-09-05 01:23:54 +05:30
|
|
|
// User's running outside of a module. All bets are off. Get GOARCH and guess compiler is gc.
|
|
|
|
// TODO(matloob): Is this a problem in practice?
|
2020-12-12 05:46:53 +05:30
|
|
|
inv.Verb = "env"
|
|
|
|
inv.Args = []string{"GOARCH"}
|
2020-08-15 22:43:07 +05:30
|
|
|
envout, enverr := gocmdRunner.Run(ctx, inv)
|
2019-09-05 01:23:54 +05:30
|
|
|
if enverr != nil {
|
2020-04-04 00:59:12 +05:30
|
|
|
return nil, enverr
|
2019-09-05 01:23:54 +05:30
|
|
|
}
|
|
|
|
goarch = strings.TrimSpace(envout.String())
|
|
|
|
compiler = "gc"
|
|
|
|
} else {
|
2020-04-04 00:59:12 +05:30
|
|
|
return nil, friendlyErr
|
2019-09-05 01:23:54 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fields := strings.Fields(stdout.String())
|
|
|
|
if len(fields) < 2 {
|
2020-04-04 00:59:12 +05:30
|
|
|
return nil, fmt.Errorf("could not parse GOARCH and Go compiler in format \"<GOARCH> <compiler>\":\nstdout: <<%s>>\nstderr: <<%s>>",
|
|
|
|
stdout.String(), stderr.String())
|
2019-09-05 01:23:54 +05:30
|
|
|
}
|
|
|
|
goarch = fields[0]
|
|
|
|
compiler = fields[1]
|
|
|
|
}
|
|
|
|
return types.SizesFor(compiler, goarch), nil
|
|
|
|
}
|