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>
diff --git a/go/packages/external.go b/go/packages/external.go
index 39e5ed9..80aac50 100644
--- a/go/packages/external.go
+++ b/go/packages/external.go
@@ -46,8 +46,8 @@
 			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 26d6277..d3ead60 100644
--- a/go/packages/golist.go
+++ b/go/packages/golist.go
@@ -279,7 +279,7 @@
 		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..835e2ab 100644
--- a/go/packages/golist_fallback.go
+++ b/go/packages/golist_fallback.go
@@ -251,7 +251,7 @@
 
 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 fa86c04..5b22de5 100644
--- a/go/packages/gopackages/main.go
+++ b/go/packages/gopackages/main.go
@@ -36,8 +36,14 @@
 	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 @@
 
 	// 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 @@
 
 	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 34cd316..d35d010 100644
--- a/go/packages/packages.go
+++ b/go/packages/packages.go
@@ -82,9 +82,9 @@
 	//
 	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 b2e15ba..ccd089b 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -519,9 +519,9 @@
 		{`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)