Use CustomCommandLine directly instead of via SpawnAction.Builder.
This change forces use of CustomCommandLine.Builder, which has a richer interface for constructing memory-efficient command lines. It will also permit surveying the code base for inefficient patterns using an IDE.
This change was done by hand and split using Rosie to assist with rollbacks in case of bugs. Reviewers, please pay particular attention to:
* Each all to addInputArgument/addOutputArgument should come with a corresponding matching pair to SpawnAction.Builder#addInput and CustomCommandLine.Builder#addExecPath (eg.).
* The commandLine must be set on the SpawnAction using SpawnAction.Builder#setCommandLine.
Note that most calls to addPrefixed("arg=", val) should be more idiomatically expressed as add("arg", val), but this involves changing tests and making sure that the command line tools can accept the format.
PiperOrigin-RevId: 166203749
diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java
index 524ced3..824fef7 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java
@@ -20,13 +20,12 @@
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.Runfiles;
+import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.util.FileType;
import com.google.devtools.build.lib.vfs.PathFragment;
-import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -110,23 +109,23 @@
FilesToRunProvider py2to3converter =
ruleContext.getExecutablePrerequisite("$python2to3", RuleConfiguredTarget.Mode.HOST);
Artifact output = get2to3OutputArtifact(ruleContext, input);
- List<String> argv = new ArrayList<>();
- argv.add("--no-diffs");
- argv.add("--nobackups");
- argv.add("--write");
- argv.add("--output-dir");
- argv.add(output.getExecPath().getParentDirectory().toString());
- argv.add("--write-unchanged-files");
- argv.add(input.getExecPathString());
+ CustomCommandLine.Builder commandLine =
+ CustomCommandLine.builder()
+ .add("--no-diffs")
+ .add("--nobackups")
+ .add("--write")
+ .addPath("--output-dir", output.getExecPath().getParentDirectory())
+ .add("--write-unchanged-files")
+ .addExecPath(input);
ruleContext.registerAction(
new SpawnAction.Builder()
.addInput(input)
.addOutput(output)
.setExecutable(py2to3converter)
- .addArguments(argv)
.setProgressMessage("Converting to Python 3: %s", input.prettyPrint())
.setMnemonic("2to3")
+ .setCommandLine(commandLine.build())
.build(ruleContext));
return output;
}