Skip to content

Commit

Permalink
Use param files with go-protoc
Browse files Browse the repository at this point in the history
If Bazel uses a param file for the args to go-protoc, go-protoc will
in turn use a param file for protoc.
  • Loading branch information
fmeum committed Jun 8, 2022
1 parent 10e90ba commit 863083d
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go/tools/builders/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// Go rules as an action.
func asm(args []string) error {
// Parse arguments.
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
log.SetFlags(0)
log.SetPrefix("builder: ")

args, err := expandParamsFiles(os.Args[1:])
args, _, err := expandParamsFiles(os.Args[1:])
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

func compile(args []string) error {
// Parse arguments.
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

func compilePkg(args []string) error {
// Parse arguments.
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// cover transforms a source file with "go tool cover". It is invoked by the
// Go rules as an action.
func cover(args []string) error {
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ func runAndLogCommand(cmd *exec.Cmd, verbose bool) error {
// expandParamsFiles 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.
func expandParamsFiles(args []string) ([]string, error) {
func expandParamsFiles(args []string) ([]string, bool, 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
return args, false, nil
}
var expandedArgs []string
last := 0
Expand All @@ -195,12 +195,12 @@ func expandParamsFiles(args []string) ([]string, error) {
fileName := args[pi][len("-param="):]
fileArgs, err := readParamsFile(fileName)
if err != nil {
return nil, err
return nil, true, err
}
expandedArgs = append(expandedArgs, fileArgs...)
}
expandedArgs = append(expandedArgs, args[last:]...)
return expandedArgs, nil
return expandedArgs, true, nil
}

// readParamsFiles parses a Bazel params file in "shell" format. The file
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/generate_test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func main() {

func genTestMain(args []string) error {
// Prepare our flags
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func run(args []string) error {
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

func link(args []string) error {
// Parse arguments.
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/nogo_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
// run returns an error if there is a problem loading the package or if any
// analysis fails.
func run(args []string) error {
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return fmt.Errorf("error reading paramfiles: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
// handle them, and ar may not be available (cpp.ar_executable is libtool
// on darwin).
func pack(args []string) error {
args, err := expandParamsFiles(args)
args, _, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down
21 changes: 19 additions & 2 deletions go/tools/builders/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type genFileInfo struct {

func run(args []string) error {
// process the args
args, err := expandParamsFiles(args)
args, useParamFile, err := expandParamsFiles(args)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,7 +89,24 @@ func run(args []string) error {
"--descriptor_set_in", strings.Join(descriptors, string(os.PathListSeparator)),
}
protoc_args = append(protoc_args, flags.Args()...)
cmd := exec.Command(*protoc, protoc_args...)

var cmd *exec.Cmd
if useParamFile {
paramFile, err := ioutil.TempFile(tmpDir, "protoc-*.params")
if err != nil {
return fmt.Errorf("error creating param file for protoc: %v", err)
}
for _, arg := range protoc_args {
_, err := fmt.Fprintln(paramFile, arg)
if err != nil {
return fmt.Errorf("error writing param file for protoc: %v", err)
}
}
cmd = exec.Command(*protoc, "@"+paramFile.Name())
} else {
cmd = exec.Command(*protoc, protoc_args...)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions proto/compiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def go_proto_compile(go, compiler, protos, imports, importpath):
args.add_all(go_srcs, before_each = "-expected")
args.add_all(imports, before_each = "-import")
args.add_all(proto_paths.keys())
args.use_param_file("-param=%s")
go.actions.run(
inputs = depset(
direct = [
Expand Down

0 comments on commit 863083d

Please sign in to comment.