diff options
author | kaipi <kaipi@google.com> | 2018-01-31 10:22:00 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-01-31 10:24:02 -0800 |
commit | 518f53210ffe4e5ea0e5b5fcac1f7916133da11b (patch) | |
tree | 415d84f11fdb57b1fe2b7e37e22a3c8b2c55f86b /src/main/java/com/google/devtools | |
parent | 109dc141b46389c73eaffa1b19825de594a85b7d (diff) |
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
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java | 23 |
1 files 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 4c7bb87400..1d046dfafb 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.FileSystemUtils; 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 @@ final class ProtobufSupport { public ProtobufSupport registerGenerationActions() { int actionId = 0; - for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) { Iterable<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos); registerGenerationAction(outputProtos, inputProtos, getUniqueBundledProtosSuffix(actionId)); actionId++; @@ -203,7 +204,7 @@ final class ProtobufSupport { int actionId = 0; Iterable<PathFragment> userHeaderSearchPaths = ImmutableList.of(getWorkspaceRelativeOutputDir()); - for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) { ImmutableSet<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos); IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId); @@ -243,7 +244,7 @@ final class ProtobufSupport { } int actionId = 0; - for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) { ImmutableSet<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos); IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId); @@ -275,7 +276,7 @@ final class ProtobufSupport { } int actionId = 0; - for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) { + for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) { ImmutableSet<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos); IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId); @@ -372,6 +373,20 @@ final class ProtobufSupport { return inputsToOutputsMapBuilder.build(); } + /** + * Returns an ordered list of ImmutableSets<Artifact>s 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<ImmutableSet<Artifact>> orderedInputOutputKeySet() { + return new Ordering<ImmutableSet<Artifact>>() { + @Override + public int compare(ImmutableSet<Artifact> o1, ImmutableSet<Artifact> o2) { + return o1.hashCode() - o2.hashCode(); + } + }.sortedCopy(inputsToOutputsMap.keySet()); + } + private String getBundledProtosSuffix() { return "_" + BUNDLED_PROTOS_IDENTIFIER; } |