Add CommandLinesAndParamFiles class.
This class is currently unused. In order to keep CL sizes down we introduce this class prior to using it in the spawn runners.
This class can manage an action's list of command lines and param files. For instance, SpawnAction will contain one of these instances (instead of keeping a single command line object and adding param file write actions to the action graph).
At spawn execution time, the spawn runners will use this class to resolve the list of command lines and param files into a master argument list + some number of param files that need to be written. The local spawn runners can simply write the param files using a helper. In the distributed cases the param files are ready-made VirtualActionInputs that can be added to the Spawn's other inputs.
RELNOTES: None
PiperOrigin-RevId: 192439501
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ParameterFile.java b/src/main/java/com/google/devtools/build/lib/actions/ParameterFile.java
index 2671a7e..61449f2 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ParameterFile.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ParameterFile.java
@@ -15,7 +15,12 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.devtools.build.lib.util.FileType;
+import com.google.devtools.build.lib.util.ShellEscaper;
import com.google.devtools.build.lib.vfs.PathFragment;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
/**
* Support for parameter file generation (as used by gcc and other tools, e.g.
@@ -76,4 +81,41 @@
return original.replaceName(original.getBaseName() + "-" + flavor + ".params");
}
+ /** Writes an argument list to a parameter file. */
+ public static void writeParameterFile(
+ OutputStream out, Iterable<String> arguments, ParameterFileType type, Charset charset)
+ throws IOException {
+ switch (type) {
+ case SHELL_QUOTED:
+ writeContentQuoted(out, arguments, charset);
+ break;
+ case UNQUOTED:
+ writeContentUnquoted(out, arguments, charset);
+ break;
+ }
+ }
+
+ /** Writes the arguments from the list into the parameter file. */
+ private static void writeContentUnquoted(
+ OutputStream outputStream, Iterable<String> arguments, Charset charset) throws IOException {
+ OutputStreamWriter out = new OutputStreamWriter(outputStream, charset);
+ for (String line : arguments) {
+ out.write(line);
+ out.write('\n');
+ }
+ out.flush();
+ }
+
+ /**
+ * Writes the arguments from the list into the parameter file with shell quoting (if required).
+ */
+ private static void writeContentQuoted(
+ OutputStream outputStream, Iterable<String> arguments, Charset charset) throws IOException {
+ OutputStreamWriter out = new OutputStreamWriter(outputStream, charset);
+ for (String line : ShellEscaper.escapeAll(arguments)) {
+ out.write(line);
+ out.write('\n');
+ }
+ out.flush();
+ }
}