From 518f53210ffe4e5ea0e5b5fcac1f7916133da11b Mon Sep 17 00:00:00 2001 From: kaipi Date: Wed, 31 Jan 2018 10:22:00 -0800 Subject: [PATCH] Order the input-output map keys before processing its elements. This fixes non-determinism in how the protos get bundled into the BundledProtos_X archives. PiperOrigin-RevId: 184009755 --- .../build/lib/rules/objc/ProtobufSupport.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java index 4c7bb874007a26..1d046dfafbcb47 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java @@ -41,6 +41,7 @@ import com.google.devtools.build.lib.vfs.PathFragment; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -164,7 +165,7 @@ public ProtobufSupport( public ProtobufSupport registerGenerationActions() { int actionId = 0; - for (ImmutableSet inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet inputProtos : orderedInputOutputKeySet()) { Iterable outputProtos = inputsToOutputsMap.get(inputProtos); registerGenerationAction(outputProtos, inputProtos, getUniqueBundledProtosSuffix(actionId)); actionId++; @@ -203,7 +204,7 @@ public ProtobufSupport registerCompilationActions() int actionId = 0; Iterable userHeaderSearchPaths = ImmutableList.of(getWorkspaceRelativeOutputDir()); - for (ImmutableSet inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet inputProtos : orderedInputOutputKeySet()) { ImmutableSet outputProtos = inputsToOutputsMap.get(inputProtos); IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId); @@ -243,7 +244,7 @@ public ProtobufSupport addFilesToBuild(NestedSetBuilder filesToBuild) } int actionId = 0; - for (ImmutableSet inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet inputProtos : orderedInputOutputKeySet()) { ImmutableSet outputProtos = inputsToOutputsMap.get(inputProtos); IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId); @@ -275,7 +276,7 @@ public Optional getObjcProvider() { } int actionId = 0; - for (ImmutableSet inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet inputProtos : orderedInputOutputKeySet()) { ImmutableSet outputProtos = inputsToOutputsMap.get(inputProtos); IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId); @@ -372,6 +373,20 @@ private static ImmutableSetMultimap, Artifact> getInputsT return inputsToOutputsMapBuilder.build(); } + /** + * Returns an ordered list of ImmutableSetss representing the keys to the inputs-outputs + * map. Using an ordered list ensures that for the same inputs, the keys are processed in the same + * order, and avoids non-determinism in the intermediate outputs. + */ + private List> orderedInputOutputKeySet() { + return new Ordering>() { + @Override + public int compare(ImmutableSet o1, ImmutableSet o2) { + return o1.hashCode() - o2.hashCode(); + } + }.sortedCopy(inputsToOutputsMap.keySet()); + } + private String getBundledProtosSuffix() { return "_" + BUNDLED_PROTOS_IDENTIFIER; }