diff options
author | Jay Conrod <jayconrod@gmail.com> | 2018-08-13 12:43:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-13 12:43:17 -0400 |
commit | f299a80f5387ba30524a6e6171e01282e0063670 (patch) | |
tree | 983f5da7cafa5c3c5f9707f91c57ece1fff28f53 | |
parent | 40030d65edcc25f5cfd4e8de50dcc7cdd9741045 (diff) | |
download | bazelbuild-rules_go-f299a80f5387ba30524a6e6171e01282e0063670.tar.gz |
Actions that use go.args may now use param files automatically (#1652)
Multiple param files are now supported as well.
-rw-r--r-- | go/private/actions/link.bzl | 9 | ||||
-rw-r--r-- | go/private/context.bzl | 2 | ||||
-rw-r--r-- | go/tools/builders/asm.go | 4 | ||||
-rw-r--r-- | go/tools/builders/cgo.go | 4 | ||||
-rw-r--r-- | go/tools/builders/compile.go | 4 | ||||
-rw-r--r-- | go/tools/builders/cover.go | 4 | ||||
-rw-r--r-- | go/tools/builders/env.go | 53 | ||||
-rw-r--r-- | go/tools/builders/generate_test_main.go | 4 | ||||
-rw-r--r-- | go/tools/builders/info.go | 4 | ||||
-rw-r--r-- | go/tools/builders/link.go | 4 | ||||
-rw-r--r-- | go/tools/builders/pack.go | 6 | ||||
-rw-r--r-- | go/tools/builders/protoc.go | 4 | ||||
-rw-r--r-- | go/tools/builders/stdlib.go | 4 |
13 files changed, 82 insertions, 24 deletions
diff --git a/go/private/actions/link.bzl b/go/private/actions/link.bzl index 51b74d89..748ddbf9 100644 --- a/go/private/actions/link.bzl +++ b/go/private/actions/link.bzl @@ -88,8 +88,11 @@ def emit_link( if go.mode.link == LINKMODE_PLUGIN: tool_args.add_all(["-pluginpath", archive.data.importpath]) - builder_args.add_all([struct(archive = archive, test_archives = test_archives)], before_each = "-dep", - map_each = _map_archive) + builder_args.add_all( + [struct(archive = archive, test_archives = test_archives)], + before_each = "-dep", + map_each = _map_archive, + ) builder_args.add_all(test_archives, before_each = "-dep", map_each = _format_archive) # Build a list of rpaths for dynamic libraries we need to find. @@ -138,8 +141,6 @@ def emit_link( tool_args.add("-w") tool_args.add_joined("-extldflags", extldflags, join_with = " ") - builder_args.use_param_file("@%s") - builder_args.set_param_file_format("multiline") go.actions.run( inputs = sets.union( archive.libs, diff --git a/go/private/context.bzl b/go/private/context.bzl index bbf12522..e821da90 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -79,6 +79,8 @@ def _declare_directory(go, path = "", ext = "", name = ""): def _new_args(go): args = go.actions.args() + args.use_param_file("-param=%s") + args.set_param_file_format("multiline") args.add_all(["-sdk", go.sdk.root_file.dirname]) args.add_joined("-tags", go.tags, join_with = ",") return args diff --git a/go/tools/builders/asm.go b/go/tools/builders/asm.go index 1370fc3a..a84b8abe 100644 --- a/go/tools/builders/asm.go +++ b/go/tools/builders/asm.go @@ -26,6 +26,10 @@ import ( func run(args []string) error { // Parse arguments. + args, err := readParamsFiles(args) + if err != nil { + return err + } builderArgs, toolArgs := splitArgs(args) flags := flag.NewFlagSet("GoAsm", flag.ExitOnError) goenv := envFlags(flags) diff --git a/go/tools/builders/cgo.go b/go/tools/builders/cgo.go index b5aeb75f..b56934f4 100644 --- a/go/tools/builders/cgo.go +++ b/go/tools/builders/cgo.go @@ -35,6 +35,10 @@ import ( ) func run(args []string) error { + args, err := readParamsFiles(args) + if err != nil { + return err + } builderArgs, toolArgs := splitArgs(args) sources := multiFlag{} importMode := false diff --git a/go/tools/builders/compile.go b/go/tools/builders/compile.go index 30c8e2f3..8345bbbb 100644 --- a/go/tools/builders/compile.go +++ b/go/tools/builders/compile.go @@ -30,6 +30,10 @@ import ( func run(args []string) error { // Parse arguments. + args, err := readParamsFiles(args) + if err != nil { + return err + } builderArgs, toolArgs := splitArgs(args) flags := flag.NewFlagSet("GoCompile", flag.ExitOnError) unfiltered := multiFlag{} diff --git a/go/tools/builders/cover.go b/go/tools/builders/cover.go index ea7c00a2..8cac244b 100644 --- a/go/tools/builders/cover.go +++ b/go/tools/builders/cover.go @@ -31,6 +31,10 @@ import ( ) func run(args []string) error { + args, err := readParamsFiles(args) + if err != nil { + return err + } flags := flag.NewFlagSet("cover", flag.ExitOnError) var coverSrc, coverVar, origSrc, srcName string flags.StringVar(&coverSrc, "o", "", "coverage output file") diff --git a/go/tools/builders/env.go b/go/tools/builders/env.go index cc7d1885..8819af2e 100644 --- a/go/tools/builders/env.go +++ b/go/tools/builders/env.go @@ -129,33 +129,48 @@ func runAndLogCommand(cmd *exec.Cmd, verbose bool) error { return nil } +// readParamsFile looks for arguments in args of the form +// "-param=filename". When it finds these arguments it reads the file "filename" +// and replaces the argument with its content (each argument must be on a +// separate line; blank lines are ignored). +func readParamsFiles(args []string) ([]string, error) { + var paramsIndices []int + for i, arg := range args { + if strings.HasPrefix(arg, "-param=") { + paramsIndices = append(paramsIndices, i) + } + } + if len(paramsIndices) == 0 { + return args, nil + } + var expandedArgs []string + last := 0 + for _, pi := range paramsIndices { + expandedArgs = append(expandedArgs, args[last:pi]...) + last = pi + 1 + + fileName := args[pi][len("-param="):] + content, err := ioutil.ReadFile(fileName) + if err != nil { + return nil, err + } + fileArgs := strings.Split(string(content), "\n") + expandedArgs = append(expandedArgs, fileArgs...) + } + expandedArgs = append(expandedArgs, args[last:]...) + return expandedArgs, nil +} + // splitArgs splits a list of command line arguments into two parts: arguments // that should be interpreted by the builder (before "--"), and arguments // that should be passed through to the underlying tool (after "--"). -// A group consisting of a single argument that is prefixed with an '@', is -// treated as a pointer to a params file, which is read and its contents used -// as the arguments. func splitArgs(args []string) (builderArgs []string, toolArgs []string) { for i, arg := range args { if arg == "--" { - - return readParamsFile(args[:i]), readParamsFile(args[i+1:]) - } - } - return readParamsFile(args), nil -} - -// readParamsFile replaces the passed in slice with the contents of a params -// file, if the slice is a single string that starts with an '@'. -// Errors reading the file are ignored and the original slice is returned. -func readParamsFile(args []string) []string { - if len(args) == 1 && strings.HasPrefix(args[0], "@") { - content, err := ioutil.ReadFile(args[0][1:]) - if err == nil { - args = strings.Split(string(content), "\n") + return args[:i], args[i+1:] } } - return args + return args, nil } // abs returns the absolute representation of path. Some tools/APIs require diff --git a/go/tools/builders/generate_test_main.go b/go/tools/builders/generate_test_main.go index 26c50d9f..2dbc600a 100644 --- a/go/tools/builders/generate_test_main.go +++ b/go/tools/builders/generate_test_main.go @@ -158,6 +158,10 @@ func main() { func run(args []string) error { // Prepare our flags + args, err := readParamsFiles(args) + if err != nil { + return err + } imports := multiFlag{} sources := multiFlag{} flags := flag.NewFlagSet("GoTestGenTest", flag.ExitOnError) diff --git a/go/tools/builders/info.go b/go/tools/builders/info.go index 73806321..0a6b19c3 100644 --- a/go/tools/builders/info.go +++ b/go/tools/builders/info.go @@ -24,6 +24,10 @@ import ( ) func run(args []string) error { + args, err := readParamsFiles(args) + if err != nil { + return err + } filename := "" flags := flag.NewFlagSet("info", flag.ExitOnError) flags.StringVar(&filename, "out", filename, "The file to write the report to") diff --git a/go/tools/builders/link.go b/go/tools/builders/link.go index 1240e393..7827ca6b 100644 --- a/go/tools/builders/link.go +++ b/go/tools/builders/link.go @@ -30,6 +30,10 @@ import ( func run(args []string) error { // Parse arguments. + args, err := readParamsFiles(args) + if err != nil { + return err + } builderArgs, toolArgs := splitArgs(args) xstamps := multiFlag{} stamps := multiFlag{} diff --git a/go/tools/builders/pack.go b/go/tools/builders/pack.go index faa39543..e84012d9 100644 --- a/go/tools/builders/pack.go +++ b/go/tools/builders/pack.go @@ -31,13 +31,17 @@ import ( "io" "io/ioutil" "log" - "path/filepath" "os" + "path/filepath" "strconv" "strings" ) func run(args []string) error { + args, err := readParamsFiles(args) + if err != nil { + return err + } flags := flag.NewFlagSet("GoPack", flag.ExitOnError) goenv := envFlags(flags) inArchive := flags.String("in", "", "Path to input archive") diff --git a/go/tools/builders/protoc.go b/go/tools/builders/protoc.go index f2e8aa3a..b3031344 100644 --- a/go/tools/builders/protoc.go +++ b/go/tools/builders/protoc.go @@ -40,6 +40,10 @@ type genFileInfo struct { func run(args []string) error { // process the args + args, err := readParamsFiles(args) + if err != nil { + return err + } options := multiFlag{} descriptors := multiFlag{} expected := multiFlag{} diff --git a/go/tools/builders/stdlib.go b/go/tools/builders/stdlib.go index 7e45e66b..2152ae43 100644 --- a/go/tools/builders/stdlib.go +++ b/go/tools/builders/stdlib.go @@ -27,6 +27,10 @@ import ( func run(args []string) error { // process the args + args, err := readParamsFiles(args) + if err != nil { + return err + } flags := flag.NewFlagSet("stdlib", flag.ExitOnError) goenv := envFlags(flags) filterBuildid := flags.String("filter_buildid", "", "Path to filter_buildid tool") |