go/packages: rename config.Flags to BuildFlags

This change adds the -buildflag argument to go/packages/gopackages,
which may be passed repeatedly to set config.BuildFlags. I felt -flag
was too vague.

config.Flags is renamed to config.BuildFlags for consistency, and
packages.findExternalDriver now passes -buildflag instead of -flag to
drivers. This will break existing drivers.

Change-Id: Iaed58026373a46e137a236ee9a652eb3a9433ee3
Reviewed-on: https://go-review.googlesource.com/130136
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Jay Conrod 2018-08-20 17:51:35 -04:00 committed by Alan Donovan
parent c1406c36ef
commit 5fad05c818
6 changed files with 34 additions and 12 deletions

View File

@ -46,8 +46,8 @@ func findExternalDriver(cfg *Config) driver {
fmt.Sprintf("-export=%t", usesExportData(cfg)),
fmt.Sprintf("-deps=%t", cfg.Mode >= LoadImports),
}
for _, f := range cfg.Flags {
fullargs = append(fullargs, fmt.Sprintf("-flags=%v", f))
for _, f := range cfg.BuildFlags {
fullargs = append(fullargs, fmt.Sprintf("-buildflag=%v", f))
}
fullargs = append(fullargs, "--")
fullargs = append(fullargs, words...)

View File

@ -279,7 +279,7 @@ func golistargs(cfg *Config, words []string) []string {
fmt.Sprintf("-export=%t", usesExportData(cfg)),
fmt.Sprintf("-deps=%t", cfg.Mode >= LoadImports),
}
fullargs = append(fullargs, cfg.Flags...)
fullargs = append(fullargs, cfg.BuildFlags...)
fullargs = append(fullargs, "--")
fullargs = append(fullargs, words...)
return fullargs

View File

@ -251,7 +251,7 @@ func getDeps(cfg *Config, words ...string) (originalSet map[string]*jsonPackage,
func golistArgsFallback(cfg *Config, words []string) []string {
fullargs := []string{"list", "-e", "-json"}
fullargs = append(fullargs, cfg.Flags...)
fullargs = append(fullargs, cfg.BuildFlags...)
fullargs = append(fullargs, "--")
fullargs = append(fullargs, words...)
return fullargs

View File

@ -36,8 +36,14 @@ var (
cpuprofile = flag.String("cpuprofile", "", "write CPU profile to this file")
memprofile = flag.String("memprofile", "", "write memory profile to this file")
traceFlag = flag.String("trace", "", "write trace log to this file")
buildFlags stringListValue
)
func init() {
flag.Var(&buildFlags, "buildflag", "pass argument to underlying build system (may be repeated)")
}
func usage() {
fmt.Fprintln(os.Stderr, `Usage: gopackages [-deps] [-cgo] [-mode=...] [-private] package...
@ -106,9 +112,10 @@ func main() {
// Load, parse, and type-check the packages named on the command line.
cfg := &packages.Config{
Mode: packages.LoadSyntax,
Error: func(error) {}, // we'll take responsibility for printing errors
Tests: *testFlag,
Mode: packages.LoadSyntax,
Error: func(error) {}, // we'll take responsibility for printing errors
Tests: *testFlag,
BuildFlags: buildFlags,
}
// -mode flag
@ -249,3 +256,18 @@ func print(lpkg *packages.Package) {
fmt.Println()
}
// stringListValue is a flag.Value that accumulates strings.
// e.g. --flag=one --flag=two would produce []string{"one", "two"}.
type stringListValue []string
func newStringListValue(val []string, p *[]string) *stringListValue {
*p = val
return (*stringListValue)(p)
}
func (ss *stringListValue) Get() interface{} { return []string(*ss) }
func (ss *stringListValue) String() string { return fmt.Sprintf("%q", *ss) }
func (ss *stringListValue) Set(s string) error { *ss = append(*ss, s); return nil }

View File

@ -82,9 +82,9 @@ type Config struct {
//
Env []string
// Flags is a list of command-line flags to be passed through to
// BuildFlags is a list of command-line flags to be passed through to
// the build system's query tool.
Flags []string
BuildFlags []string
// Error is called for each error encountered during parsing and type-checking.
// It must be safe to call Error simultaneously from multiple goroutines.

View File

@ -519,9 +519,9 @@ package b`,
{`a`, []string{`-tags=tag tag2`}, "a.go b.go c.go d.go", "a.go b.go"},
} {
cfg := &packages.Config{
Mode: packages.LoadImports,
Flags: test.tags,
Env: append(os.Environ(), "GOPATH="+tmp, "GO111MODULE=off"),
Mode: packages.LoadImports,
BuildFlags: test.tags,
Env: append(os.Environ(), "GOPATH="+tmp, "GO111MODULE=off"),
}
initial, err := packages.Load(cfg, test.pattern)