diff options
author | 2016-12-05 22:01:25 +0000 | |
---|---|---|
committer | 2016-12-06 12:06:56 +0000 | |
commit | 901e14a0a60a6d5fb83d1b5cb88142e2440308b9 (patch) | |
tree | 268cd6361670bd707b72f609e2e39d0a6baf50bd | |
parent | 0db9b11502d109ada45e0bd2f3a1e87572c92185 (diff) |
Override BuildConfiguration.equals() for dynamic configs.
This makes tests that unnecessarily relied on reference equality pass
with --experimental_dynamic_configs=notrim. In particular, with
--nodistinct_host_configuration dynamic configurations still use
distinct instances even though the options are the same.
This is a roll forward of original change: https://github.com/bazelbuild/bazel/commit/2a2be3907981d9654575493c7012d95d1241f373
That was rolled back because of lots of expensive hashCode() calls.
This version precomputes the hash code.
--
PiperOrigin-RevId: 141095789
MOS_MIGRATED_REVID=141095789
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java | 31 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestCase.java | 2 |
2 files changed, 32 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java index 1a7814a0e1..c4689c90f2 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java @@ -85,6 +85,7 @@ import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Queue; import java.util.Set; import java.util.TreeMap; @@ -1080,6 +1081,8 @@ public final class BuildConfiguration { private final ImmutableMap<String, String> testEnvironment; private final ImmutableMap<String, String> commandLineBuildVariables; + private final int hashCode; // We can precompute the hash code as all its inputs are immutable. + /** * Helper container for {@link #transitiveOptionsMap} below. */ @@ -1140,6 +1143,33 @@ public final class BuildConfiguration { } /** + * Returns {@code true} if this configuration is semantically equal to the other, including + * checking that both have the same sets of fragments and options. + */ + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof BuildConfiguration)) { + return false; + } + BuildConfiguration otherConfig = (BuildConfiguration) other; + return actionsEnabled == otherConfig.actionsEnabled + && fragments.values().equals(otherConfig.fragments.values()) + && buildOptions.getOptions().equals(otherConfig.buildOptions.getOptions()); + } + + private int computeHashCode() { + return Objects.hash(actionsEnabled, fragments, buildOptions.getOptions()); + } + + @Override + public int hashCode() { + return hashCode; + } + + /** * Returns map of all the fragments for this configuration. */ public ImmutableMap<Class<? extends Fragment>, Fragment> getAllFragments() { @@ -1295,6 +1325,7 @@ public final class BuildConfiguration { globalMakeEnv = globalMakeEnvBuilder.build(); checksum = Fingerprint.md5Digest(buildOptions.computeCacheKey()); + hashCode = computeHashCode(); } /** diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestCase.java b/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestCase.java index 9cf952c200..d9dd9ae4ae 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestCase.java @@ -426,7 +426,7 @@ public abstract class BuildViewTestCase extends FoundationTestCase { */ protected final void createBuildView() throws Exception { Preconditions.checkNotNull(masterConfig); - Preconditions.checkState(getHostConfiguration() == getTargetConfiguration() + Preconditions.checkState(getHostConfiguration().equals(getTargetConfiguration()) || getHostConfiguration().isHostConfiguration(), "Host configuration %s is not a host configuration' " + "and does not match target configuration %s", |