Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions that use go.args may now use param files automatically #1652

Merged
merged 1 commit into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions go/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
53 changes: 34 additions & 19 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/generate_test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 5 additions & 1 deletion go/tools/builders/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 4 additions & 0 deletions go/tools/builders/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down