diff --git a/go/packages/external.go b/go/packages/external.go index 39e5ed99..80aac505 100644 --- a/go/packages/external.go +++ b/go/packages/external.go @@ -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...) diff --git a/go/packages/golist.go b/go/packages/golist.go index 26d62771..d3ead604 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -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 diff --git a/go/packages/golist_fallback.go b/go/packages/golist_fallback.go index 331bb655..835e2aba 100644 --- a/go/packages/golist_fallback.go +++ b/go/packages/golist_fallback.go @@ -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 diff --git a/go/packages/gopackages/main.go b/go/packages/gopackages/main.go index fa86c04a..5b22de5a 100644 --- a/go/packages/gopackages/main.go +++ b/go/packages/gopackages/main.go @@ -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 } diff --git a/go/packages/packages.go b/go/packages/packages.go index 34cd316b..d35d0104 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -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. diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index b2e15bac..ccd089be 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -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)