diff options
author | 2015-09-28 13:21:45 +0000 | |
---|---|---|
committer | 2015-09-28 14:35:36 +0000 | |
commit | a385d3eb7b78d4d155b7218019a08e7457f2514c (patch) | |
tree | ac817b31a3c15d8bf2f62d8130aa721eb0e36555 /src | |
parent | 4e7ed50345f3a3037549f7c41f852b5d9db4a08d (diff) |
Refactor BuildView; clearly identify the ide_build_info and the testing API.
Also inject the EventHandler all the way through to the SkyframeExecutor.
--
MOS_MIGRATED_REVID=104094731
Diffstat (limited to 'src')
4 files changed, 98 insertions, 134 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java b/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java index 08b5d6601a..b9e7afe91f 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java @@ -47,15 +47,12 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; -import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.events.StoredEventHandler; import com.google.devtools.build.lib.packages.AspectParameters; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.BuildType; -import com.google.devtools.build.lib.packages.NoSuchPackageException; -import com.google.devtools.build.lib.packages.NoSuchTargetException; import com.google.devtools.build.lib.packages.NoSuchThingException; import com.google.devtools.build.lib.packages.PackageSpecification; import com.google.devtools.build.lib.packages.RawAttributeMapper; @@ -268,49 +265,20 @@ public class BuildView { return skyframeExecutor.getLastWorkspaceStatusActionForTesting(); } - /** - * Returns a corresponding ConfiguredTarget, if one exists; otherwise throws an {@link - * NoSuchConfiguredTargetException}. - */ - @ThreadSafe - private ConfiguredTarget getConfiguredTarget(Target target, BuildConfiguration config) - throws NoSuchConfiguredTargetException { - ConfiguredTarget result = - getExistingConfiguredTarget(target.getLabel(), config); - if (result == null) { - throw new NoSuchConfiguredTargetException(target.getLabel(), config); - } - return result; - } - - /** - * Obtains a {@link ConfiguredTarget} given a {@code label}, by delegating - * to the package cache and - * {@link #getConfiguredTarget(Target, BuildConfiguration)}. - */ - public ConfiguredTarget getConfiguredTarget(Label label, BuildConfiguration config) - throws NoSuchPackageException, NoSuchTargetException, NoSuchConfiguredTargetException { - return getConfiguredTarget(packageManager.getLoadedTarget(label), config); - } - - public Iterable<ConfiguredTarget> getDirectPrerequisites(ConfiguredTarget ct, - BuildConfigurationCollection configurations) - throws InterruptedException { - return getDirectPrerequisites(ct, null, configurations); - } - - public Iterable<ConfiguredTarget> getDirectPrerequisites( - ConfiguredTarget ct, @Nullable final LoadingCache<Label, Target> targetCache, - BuildConfigurationCollection configurations) - throws InterruptedException { - return skyframeExecutor.getConfiguredTargets(ct.getConfiguration(), - getDirectPrerequisiteDependencies(ct, targetCache, configurations), false); + @VisibleForTesting + public Iterable<ConfiguredTarget> getDirectPrerequisitesForTesting( + EventHandler eventHandler, ConfiguredTarget ct, BuildConfigurationCollection configurations) + throws InterruptedException { + return skyframeExecutor.getConfiguredTargets( + eventHandler, ct.getConfiguration(), + getDirectPrerequisiteDependenciesForTesting(eventHandler, ct, null, configurations), false); } - public Iterable<Dependency> getDirectPrerequisiteDependencies( - ConfiguredTarget ct, @Nullable final LoadingCache<Label, Target> targetCache, - BuildConfigurationCollection configurations) - throws InterruptedException { + @VisibleForTesting + public Iterable<Dependency> getDirectPrerequisiteDependenciesForTesting( + EventHandler eventHandler, ConfiguredTarget ct, + @Nullable final LoadingCache<Label, Target> targetCache, + BuildConfigurationCollection configurations) throws InterruptedException { if (!(ct.getTarget() instanceof Rule)) { return ImmutableList.of(); } @@ -345,14 +313,15 @@ public class BuildView { TargetAndConfiguration ctgNode = new TargetAndConfiguration(ct.getTarget(), ct.getConfiguration()); return dependencyResolver.dependentNodes(ctgNode, configurations.getHostConfiguration(), - getConfigurableAttributeKeys(ctgNode)); + getConfigurableAttributeKeysForTesting(eventHandler, ctgNode)); } /** * Returns ConfigMatchingProvider instances corresponding to the configurable attribute keys * present in this rule's attributes. */ - private Set<ConfigMatchingProvider> getConfigurableAttributeKeys(TargetAndConfiguration ctg) { + private Set<ConfigMatchingProvider> getConfigurableAttributeKeysForTesting( + EventHandler eventHandler, TargetAndConfiguration ctg) { if (!(ctg.getTarget() instanceof Rule)) { return ImmutableSet.of(); } @@ -364,14 +333,9 @@ public class BuildView { if (BuildType.Selector.isReservedLabel(label)) { continue; } - try { - ConfiguredTarget ct = getConfiguredTarget(label, ctg.getConfiguration()); - keys.add(Preconditions.checkNotNull(ct.getProvider(ConfigMatchingProvider.class))); - } catch ( - NoSuchPackageException | NoSuchTargetException | NoSuchConfiguredTargetException e) { - // All lookups should succeed because we should not be looking up any targets in error. - throw new IllegalStateException(e); - } + ConfiguredTarget ct = getConfiguredTargetForTesting( + eventHandler, label, ctg.getConfiguration()); + keys.add(Preconditions.checkNotNull(ct.getProvider(ConfigMatchingProvider.class))); } } return keys.build(); @@ -792,31 +756,40 @@ public class BuildView { return ImmutableList.copyOf(nodes); } - /** - * Returns an existing ConfiguredTarget for the specified target and - * configuration, or null if none exists. No validity check is done. - */ - @ThreadSafe - public ConfiguredTarget getExistingConfiguredTarget(Target target, BuildConfiguration config) { - return getExistingConfiguredTarget(target.getLabel(), config); - } - - /** - * Returns an existing ConfiguredTarget for the specified node, or null if none exists. No - * validity check is done. - */ - @ThreadSafe - private ConfiguredTarget getExistingConfiguredTarget( - Label label, BuildConfiguration configuration) { + // For ide_build_info + public ConfiguredTarget getConfiguredTargetForIdeInfo( + EventHandler eventHandler, Label label, BuildConfiguration configuration) { return Iterables.getFirst( skyframeExecutor.getConfiguredTargets( - configuration, ImmutableList.of(new Dependency(label, configuration)), true), + eventHandler, + configuration, + ImmutableList.of(new Dependency(label, configuration)), + true), null); } - private ListMultimap<Attribute, ConfiguredTarget> getPrerequisiteMapForTesting( - ConfiguredTarget target, BuildConfigurationCollection configurations) + public ConfiguredTarget getConfiguredTargetForIdeInfo( + EventHandler eventHandler, Target target, BuildConfiguration config) { + return getConfiguredTargetForIdeInfo(eventHandler, target.getLabel(), config); + } + + public Iterable<ConfiguredTarget> getDirectPrerequisitesForIdeInfo( + EventHandler eventHandler, ConfiguredTarget ct, BuildConfigurationCollection configurations) throws InterruptedException { + return getDirectPrerequisitesForTesting(eventHandler, ct, configurations); + } + + public Iterable<Dependency> getDirectPrerequisiteDependenciesForIdeInfo( + EventHandler eventHandler, ConfiguredTarget ct, + @Nullable final LoadingCache<Label, Target> targetCache, + BuildConfigurationCollection configurations) throws InterruptedException { + return getDirectPrerequisiteDependenciesForTesting( + eventHandler, ct, targetCache, configurations); + } + + private ListMultimap<Attribute, ConfiguredTarget> getPrerequisiteMapForTesting( + EventHandler eventHandler, ConfiguredTarget target, + BuildConfigurationCollection configurations) throws InterruptedException { DependencyResolver resolver = new DependencyResolver() { @Override protected void invalidVisibilityReferenceHook(TargetAndConfiguration node, Label label) { @@ -837,13 +810,14 @@ public class BuildView { ListMultimap<Attribute, Dependency> depNodeNames; try { depNodeNames = resolver.dependentNodeMap(ctNode, configurations.getHostConfiguration(), - /*aspect=*/null, AspectParameters.EMPTY, getConfigurableAttributeKeys(ctNode)); + /*aspect=*/null, AspectParameters.EMPTY, + getConfigurableAttributeKeysForTesting(eventHandler, ctNode)); } catch (EvalException e) { throw new IllegalStateException(e); } ImmutableMap<Dependency, ConfiguredTarget> cts = skyframeExecutor.getConfiguredTargetMap( - ctNode.getConfiguration(), ImmutableSet.copyOf(depNodeNames.values()), false); + eventHandler, ctNode.getConfiguration(), ImmutableSet.copyOf(depNodeNames.values()), false); ImmutableListMultimap.Builder<Attribute, ConfiguredTarget> builder = ImmutableListMultimap.builder(); @@ -854,12 +828,8 @@ public class BuildView { } /** - * Sets the possible artifact roots in the artifact factory. This allows the - * factory to resolve paths with unknown roots to artifacts. - * <p> - * <em>Note: This must be called before any call to - * {@link #getConfiguredTarget(Label, BuildConfiguration)} - * </em> + * Sets the possible artifact roots in the artifact factory. This allows the factory to resolve + * paths with unknown roots to artifacts. */ @VisibleForTesting // for BuildViewTestCase public void setArtifactRoots(ImmutableMap<PackageIdentifier, Path> packageRoots, @@ -892,21 +862,13 @@ public class BuildView { } /** - * Returns a configured target for the specified target and configuration. - * This should only be called from test cases, and is needed, because - * plain {@link #getConfiguredTarget(Target, BuildConfiguration)} does not - * construct the configured target graph, and would thus fail if called from - * outside an update. + * Returns a configured target for the specified target and configuration. Returns {@code null} + * if something goes wrong. */ @VisibleForTesting - public ConfiguredTarget getConfiguredTargetForTesting(Label label, BuildConfiguration config) - throws NoSuchPackageException, NoSuchTargetException { - return getConfiguredTargetForTesting(packageManager.getLoadedTarget(label), config); - } - - @VisibleForTesting - public ConfiguredTarget getConfiguredTargetForTesting(Target target, BuildConfiguration config) { - return skyframeExecutor.getConfiguredTargetForTesting(target.getLabel(), config); + public ConfiguredTarget getConfiguredTargetForTesting( + EventHandler eventHandler, Label label, BuildConfiguration config) { + return skyframeExecutor.getConfiguredTargetForTesting(eventHandler, label, config); } /** @@ -916,20 +878,13 @@ public class BuildView { public RuleContext getRuleContextForTesting( ConfiguredTarget target, StoredEventHandler eventHandler, BuildConfigurationCollection configurations, BinTools binTools) throws InterruptedException { - BuildConfiguration config = target.getConfiguration(); - CachingAnalysisEnvironment analysisEnvironment = + BuildConfiguration targetConfig = target.getConfiguration(); + CachingAnalysisEnvironment env = new CachingAnalysisEnvironment(getArtifactFactory(), - new ConfiguredTargetKey(target.getLabel(), config), - /*isSystemEnv=*/false, config.extendedSanityChecks(), eventHandler, - /*skyframeEnv=*/null, config.isActionsEnabled(), binTools); - return new RuleContext.Builder(analysisEnvironment, - (Rule) target.getTarget(), config, configurations.getHostConfiguration(), - ruleClassProvider.getPrerequisiteValidator()) - .setVisibility(NestedSetBuilder.<PackageSpecification>create( - Order.STABLE_ORDER, PackageSpecification.EVERYTHING)) - .setPrerequisites(getPrerequisiteMapForTesting(target, configurations)) - .setConfigConditions(ImmutableSet.<ConfigMatchingProvider>of()) - .build(); + new ConfiguredTargetKey(target.getLabel(), targetConfig), + /*isSystemEnv=*/false, targetConfig.extendedSanityChecks(), eventHandler, + /*skyframeEnv=*/null, targetConfig.isActionsEnabled(), binTools); + return getRuleContextForTesting(eventHandler, target, env, configurations); } /** @@ -937,15 +892,16 @@ public class BuildView { * given configured target. */ @VisibleForTesting - public RuleContext getRuleContextForTesting(ConfiguredTarget target, AnalysisEnvironment env, - BuildConfigurationCollection configurations) throws InterruptedException { + public RuleContext getRuleContextForTesting(EventHandler eventHandler, ConfiguredTarget target, + AnalysisEnvironment env, BuildConfigurationCollection configurations) + throws InterruptedException { BuildConfiguration targetConfig = target.getConfiguration(); return new RuleContext.Builder( env, (Rule) target.getTarget(), targetConfig, configurations.getHostConfiguration(), ruleClassProvider.getPrerequisiteValidator()) .setVisibility(NestedSetBuilder.<PackageSpecification>create( Order.STABLE_ORDER, PackageSpecification.EVERYTHING)) - .setPrerequisites(getPrerequisiteMapForTesting(target, configurations)) + .setPrerequisites(getPrerequisiteMapForTesting(eventHandler, target, configurations)) .setConfigConditions(ImmutableSet.<ConfigMatchingProvider>of()) .build(); } @@ -957,13 +913,13 @@ public class BuildView { */ @VisibleForTesting public ConfiguredTarget getPrerequisiteConfiguredTargetForTesting( - ConfiguredTarget dependentTarget, ConfiguredTarget desiredTarget, + EventHandler eventHandler, ConfiguredTarget dependentTarget, Label desiredTarget, BuildConfigurationCollection configurations) throws InterruptedException { Collection<ConfiguredTarget> configuredTargets = - getPrerequisiteMapForTesting(dependentTarget, configurations).values(); + getPrerequisiteMapForTesting(eventHandler, dependentTarget, configurations).values(); for (ConfiguredTarget ct : configuredTargets) { - if (ct.getLabel().equals(desiredTarget.getLabel())) { + if (ct.getLabel().equals(desiredTarget)) { return ct; } } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java index cbf864d840..d21c14ff08 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java @@ -1091,14 +1091,17 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { * returned list. */ @ThreadSafety.ThreadSafe - public ImmutableList<ConfiguredTarget> getConfiguredTargets(BuildConfiguration originalConfig, - Iterable<Dependency> keys, boolean useOriginalConfig) { - return getConfiguredTargetMap(originalConfig, keys, useOriginalConfig).values().asList(); + public ImmutableList<ConfiguredTarget> getConfiguredTargets( + EventHandler eventHandler, BuildConfiguration originalConfig, Iterable<Dependency> keys, + boolean useOriginalConfig) { + return getConfiguredTargetMap( + eventHandler, originalConfig, keys, useOriginalConfig).values().asList(); } @ThreadSafety.ThreadSafe public ImmutableMap<Dependency, ConfiguredTarget> getConfiguredTargetMap( - BuildConfiguration originalConfig, Iterable<Dependency> keys, boolean useOriginalConfig) { + EventHandler eventHandler, BuildConfiguration originalConfig, Iterable<Dependency> keys, + boolean useOriginalConfig) { checkActive(); Map<Dependency, BuildConfiguration> configs; @@ -1116,7 +1119,7 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { configs = new HashMap<>(); configs.put(Iterables.getOnlyElement(keys), originalConfig); } else { - configs = getConfigurations(originalConfig.getOptions(), keys); + configs = getConfigurations(eventHandler, originalConfig.getOptions(), keys); } } else { configs = new HashMap<>(); @@ -1134,7 +1137,7 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { } } - EvaluationResult<SkyValue> result = evaluateSkyKeys(skyKeys); + EvaluationResult<SkyValue> result = evaluateSkyKeys(eventHandler, skyKeys); ImmutableMap.Builder<Dependency, ConfiguredTarget> cts = ImmutableMap.builder(); DependentNodeLoop: @@ -1169,8 +1172,8 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { * Retrieves the configurations needed for the given deps, trimming down their fragments * to those only needed by their transitive closures. */ - private Map<Dependency, BuildConfiguration> getConfigurations(BuildOptions fromOptions, - Iterable<Dependency> keys) { + private Map<Dependency, BuildConfiguration> getConfigurations(EventHandler eventHandler, + BuildOptions fromOptions, Iterable<Dependency> keys) { Map<Dependency, BuildConfiguration> builder = new HashMap<>(); Set<Dependency> depsToEvaluate = new HashSet<>(); @@ -1190,7 +1193,8 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { transitiveFragmentSkyKeys.add(TransitiveTargetValue.key(key.getLabel())); } } - EvaluationResult<SkyValue> fragmentsResult = evaluateSkyKeys(transitiveFragmentSkyKeys); + EvaluationResult<SkyValue> fragmentsResult = evaluateSkyKeys( + eventHandler, transitiveFragmentSkyKeys); for (Dependency key : keys) { if (!depsToEvaluate.contains(key)) { // No fragments to compute here. @@ -1212,7 +1216,7 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { configSkyKeys.add(BuildConfigurationValue.key(fragmentsMap.get(key.getLabel()), getDynamicConfigOptions(key, fromOptions))); } - EvaluationResult<SkyValue> configsResult = evaluateSkyKeys(configSkyKeys); + EvaluationResult<SkyValue> configsResult = evaluateSkyKeys(eventHandler, configSkyKeys); for (Dependency key : keys) { if (!depsToEvaluate.contains(key) || labelsWithErrors.contains(key.getLabel())) { continue; @@ -1242,7 +1246,8 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { /** * Evaluates the given sky keys, blocks, and returns their evaluation results. */ - private EvaluationResult<SkyValue> evaluateSkyKeys(final Iterable<SkyKey> skyKeys) { + private EvaluationResult<SkyValue> evaluateSkyKeys( + final EventHandler eventHandler, final Iterable<SkyKey> skyKeys) { EvaluationResult<SkyValue> result; try { result = callUninterruptibly(new Callable<EvaluationResult<SkyValue>>() { @@ -1251,7 +1256,7 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { synchronized (valueLookupLock) { try { skyframeBuildView.enableAnalysis(true); - return buildDriver.evaluate(skyKeys, false, DEFAULT_THREAD_COUNT, errorEventListener); + return buildDriver.evaluate(skyKeys, false, DEFAULT_THREAD_COUNT, eventHandler); } finally { skyframeBuildView.enableAnalysis(false); } @@ -1287,13 +1292,16 @@ public abstract class SkyframeExecutor implements WalkableGraphFactory { @VisibleForTesting @Nullable public ConfiguredTarget getConfiguredTargetForTesting( - Label label, BuildConfiguration configuration) { + EventHandler eventHandler, Label label, BuildConfiguration configuration) { if (memoizingEvaluator.getExistingValueForTesting( PrecomputedValue.WORKSPACE_STATUS_KEY.getKeyForTesting()) == null) { injectWorkspaceStatusData(); } return Iterables.getFirst( - getConfiguredTargets(configuration, ImmutableList.of(new Dependency(label, configuration)), + getConfiguredTargets( + eventHandler, + configuration, + ImmutableList.of(new Dependency(label, configuration)), true), null); } diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java b/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java index db183ce9ea..0ef5e14462 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java @@ -315,7 +315,7 @@ public abstract class AnalysisTestCase extends FoundationTestCase { } catch (LabelSyntaxException e) { throw new AssertionError(e); } - return skyframeExecutor.getConfiguredTargetForTesting(parsedLabel, configuration); + return skyframeExecutor.getConfiguredTargetForTesting(reporter, parsedLabel, configuration); } /** 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 f7021eb8d6..420f0adf7a 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 @@ -381,7 +381,7 @@ public abstract class BuildViewTestCase extends FoundationTestCase { */ protected Iterable<ConfiguredTarget> getDirectPrerequisites(ConfiguredTarget target) throws InterruptedException { - return view.getDirectPrerequisites(target, masterConfig); + return view.getDirectPrerequisitesForTesting(reporter, target, masterConfig); } /** @@ -431,7 +431,8 @@ public abstract class BuildViewTestCase extends FoundationTestCase { * given configured target. */ protected RuleContext getRuleContext(ConfiguredTarget target) throws InterruptedException { - return view.getRuleContextForTesting(target, new StubAnalysisEnvironment(), masterConfig); + return view.getRuleContextForTesting( + reporter, target, new StubAnalysisEnvironment(), masterConfig); } /** @@ -590,8 +591,7 @@ public abstract class BuildViewTestCase extends FoundationTestCase { protected ConfiguredTarget getConfiguredTarget(String label, BuildConfiguration config) throws NoSuchPackageException, NoSuchTargetException, LabelSyntaxException, InterruptedException { - ensureTargetsVisited(label); - return view.getConfiguredTargetForTesting(getTarget(label), config); + return getConfiguredTarget(Label.parseAbsolute(label), config); } /** @@ -601,7 +601,7 @@ public abstract class BuildViewTestCase extends FoundationTestCase { protected ConfiguredTarget getConfiguredTarget(Label label, BuildConfiguration config) throws NoSuchPackageException, NoSuchTargetException, InterruptedException { ensureTargetsVisited(label); - return view.getConfiguredTargetForTesting(getTarget(label), config); + return view.getConfiguredTargetForTesting(reporter, label, config); } /** @@ -690,7 +690,7 @@ public abstract class BuildViewTestCase extends FoundationTestCase { throws IOException, Exception { Target rule = scratchRule(packageName, ruleName, lines); if (ensureTargetsVisited(rule.getLabel())) { - return view.getConfiguredTargetForTesting(rule, config); + return view.getConfiguredTargetForTesting(reporter, rule.getLabel(), config); } else { return null; } |