From 6e10efac41ea0f85130585d44df2f0565ea8d6fa Mon Sep 17 00:00:00 2001 From: tomlu Date: Thu, 25 Jan 2018 12:31:53 -0800 Subject: Add CommandLineItem interface. This interface makes it clearer in the type system exactly how items that go into a CustomCommandLine are turned into strings. It is a preparatory change to allow command line fingerprints to be more cheaply calculated, but it is valuable in itself from a code quality standpoint. PiperOrigin-RevId: 183274022 --- .../lib/analysis/actions/CustomCommandLine.java | 43 ++++++++-------------- 1 file changed, 16 insertions(+), 27 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/analysis/actions/CustomCommandLine.java') diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/CustomCommandLine.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/CustomCommandLine.java index f86d5603fc..a987c99917 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/actions/CustomCommandLine.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/CustomCommandLine.java @@ -42,7 +42,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; -import java.util.function.Function; import javax.annotation.Nullable; /** A customizable, serializable class for building memory efficient command lines. */ @@ -77,10 +76,8 @@ public final class CustomCommandLine extends CommandLine { abstract void eval(ImmutableList.Builder builder); } - // TODO(bazel-team): CustomMultiArgv is going to be difficult to expose - // in Skylark. Maybe we can get rid of them by refactoring JavaCompileAction. It also - // raises immutability / serialization issues. - /** Custom Java code producing a List of String arguments. */ + /** Deprecated. Do not use. TODO(b/64841073): Remove this */ + @Deprecated public abstract static class CustomMultiArgv extends StandardArgvFragment { @Override @@ -179,17 +176,17 @@ public final class CustomCommandLine extends CommandLine { } /** Each argument is mapped using the supplied map function */ - public MappedVectorArg mapped(Function mapFn) { + public MappedVectorArg mapped(CommandLineItem.MapFn mapFn) { return new MappedVectorArg<>(this, mapFn); } } /** A vector arg that maps some type T to strings. */ - public static class MappedVectorArg extends VectorArg { + static class MappedVectorArg extends VectorArg { private final Iterable values; - private final Function mapFn; + private final CommandLineItem.MapFn mapFn; - private MappedVectorArg(SimpleVectorArg other, Function mapFn) { + private MappedVectorArg(SimpleVectorArg other, CommandLineItem.MapFn mapFn) { super( other.isNestedSet, other.isEmpty, @@ -263,7 +260,7 @@ public final class CustomCommandLine extends CommandLine { @SuppressWarnings("unchecked") private static void push(List arguments, VectorArg vectorArg) { final Iterable values; - final Function mapFn; + final CommandLineItem.MapFn mapFn; if (vectorArg instanceof SimpleVectorArg) { values = ((SimpleVectorArg) vectorArg).values; mapFn = null; @@ -342,14 +339,12 @@ public final class CustomCommandLine extends CommandLine { mutatedValues.add(arguments.get(argi++)); } } - if (hasMapEach) { - Function mapFn = (Function) arguments.get(argi++); - for (int i = 0; i < count; ++i) { - mutatedValues.set(i, mapFn.apply(mutatedValues.get(i))); - } - } + CommandLineItem.MapFn mapFn = + hasMapEach + ? (CommandLineItem.MapFn) arguments.get(argi++) + : CommandLineItem.MapFn.DEFAULT; for (int i = 0; i < count; ++i) { - mutatedValues.set(i, valueToString(mutatedValues.get(i))); + mutatedValues.set(i, mapFn.expandToCommandLine(mutatedValues.get(i))); } if (hasFormatEach) { String formatStr = (String) arguments.get(argi++); @@ -413,7 +408,7 @@ public final class CustomCommandLine extends CommandLine { String formatStr = (String) arguments.get(argi++); Object[] args = new Object[argCount]; for (int i = 0; i < argCount; ++i) { - args[i] = valueToString(arguments.get(argi++)); + args[i] = CommandLineItem.expandToCommandLine(arguments.get(argi++)); } builder.add(String.format(formatStr, args)); return argi; @@ -433,7 +428,7 @@ public final class CustomCommandLine extends CommandLine { public int eval(List arguments, int argi, ImmutableList.Builder builder) { String before = (String) arguments.get(argi++); Object arg = arguments.get(argi++); - builder.add(before + valueToString(arg)); + builder.add(before + CommandLineItem.expandToCommandLine(arg)); return argi; } } @@ -1074,7 +1069,7 @@ public final class CustomCommandLine extends CommandLine { i = ((ArgvFragment) substitutedArg).eval(arguments, i, builder); } } else { - builder.add(valueToString(substitutedArg)); + builder.add(CommandLineItem.expandToCommandLine(substitutedArg)); } } return builder.build(); @@ -1082,7 +1077,7 @@ public final class CustomCommandLine extends CommandLine { private void evalSimpleVectorArg(Iterable arg, ImmutableList.Builder builder) { for (Object value : arg) { - builder.add(valueToString(value)); + builder.add(CommandLineItem.expandToCommandLine(value)); } } @@ -1101,10 +1096,4 @@ public final class CustomCommandLine extends CommandLine { return arg; } } - - private static String valueToString(Object value) { - return value instanceof Artifact - ? ((Artifact) value).getExecPath().getPathString() - : value.toString(); - } } -- cgit v1.2.3