Skip to content

Commit

Permalink
Move DependencyKind subclasses into DependencyKind.
Browse files Browse the repository at this point in the history
Cleanup leading to toolchain transitions, bazelbuild#10523.
  • Loading branch information
katre committed May 8, 2020
1 parent 4a99774 commit e4f2b49
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 69 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ java_library(
srcs = ["DependencyKind.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/packages",
"//third_party:auto_value",
"//third_party:guava",
"//third_party:jsr305",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private TransitiveInfoCollection findPrerequisite(
Label label,
BuildConfiguration config) {
for (ConfiguredTargetAndData prerequisite :
prerequisiteMap.get(DependencyResolver.VISIBILITY_DEPENDENCY)) {
prerequisiteMap.get(DependencyKind.VISIBILITY_DEPENDENCY)) {
if (prerequisite.getTarget().getLabel().equals(label)
&& Objects.equals(prerequisite.getConfiguration(), config)) {
return prerequisite.getConfiguredTarget();
Expand Down Expand Up @@ -209,7 +209,7 @@ public final ConfiguredTarget createConfiguredTarget(
analysisEnvironment,
target,
config,
prerequisiteMap.get(DependencyResolver.OUTPUT_FILE_RULE_DEPENDENCY),
prerequisiteMap.get(DependencyKind.OUTPUT_FILE_RULE_DEPENDENCY),
visibility);
if (analysisEnvironment.getSkyframeEnv().valuesMissing()) {
return null;
Expand All @@ -232,7 +232,7 @@ public final ConfiguredTarget createConfiguredTarget(
analysisEnvironment,
target,
config,
prerequisiteMap.get(DependencyResolver.OUTPUT_FILE_RULE_DEPENDENCY),
prerequisiteMap.get(DependencyKind.OUTPUT_FILE_RULE_DEPENDENCY),
visibility);
SourceArtifact artifact =
artifactFactory.getSourceArtifact(
Expand All @@ -248,7 +248,7 @@ public final ConfiguredTarget createConfiguredTarget(
analysisEnvironment,
target,
config,
prerequisiteMap.get(DependencyResolver.VISIBILITY_DEPENDENCY),
prerequisiteMap.get(DependencyKind.VISIBILITY_DEPENDENCY),
visibility);
return new PackageGroupConfiguredTarget(targetContext, packageGroup);
} else if (target instanceof EnvironmentGroup) {
Expand Down Expand Up @@ -576,7 +576,7 @@ public static OrderedSetMultimap<Attribute, ConfiguredTargetAndData> transformPr
OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> map, Target target) {
OrderedSetMultimap<Attribute, ConfiguredTargetAndData> result = OrderedSetMultimap.create();
for (Map.Entry<DependencyKind, ConfiguredTargetAndData> entry : map.entries()) {
if (entry.getKey() == DependencyResolver.TOOLCHAIN_DEPENDENCY) {
if (entry.getKey() == DependencyKind.TOOLCHAIN_DEPENDENCY) {
continue;
}
Attribute attribute = entry.getKey().getAttribute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.packages.AspectClass;
import com.google.devtools.build.lib.packages.Attribute;
import javax.annotation.Nullable;
Expand All @@ -25,6 +27,15 @@
*/
public interface DependencyKind {

/** A dependency for visibility. */
DependencyKind VISIBILITY_DEPENDENCY = new NonAttributeDependencyKind("VISIBILITY");

/** The dependency on the rule that creates a given output file. */
DependencyKind OUTPUT_FILE_RULE_DEPENDENCY = new NonAttributeDependencyKind("OUTPUT_FILE");

/** A dependency on a resolved toolchain. */
DependencyKind TOOLCHAIN_DEPENDENCY = new NonAttributeDependencyKind("TOOLCHAIN");

/**
* The attribute through which a dependency arises.
*
Expand All @@ -41,4 +52,49 @@ public interface DependencyKind {
*/
@Nullable
AspectClass getOwningAspect();

/** A dependency caused by something that's not an attribute. Special cases enumerated below. */
final class NonAttributeDependencyKind implements DependencyKind {
private final String name;

private NonAttributeDependencyKind(String name) {
this.name = name;
}

@Override
public Attribute getAttribute() {
return null;
}

@Nullable
@Override
public AspectClass getOwningAspect() {
throw new IllegalStateException();
}

@Override
public String toString() {
return String.format("%s(%s)", getClass().getSimpleName(), this.name);
}
}

/** A dependency through an attribute, either that of an aspect or the rule itself. */
@AutoValue
abstract class AttributeDependencyKind implements DependencyKind {
@Override
public abstract Attribute getAttribute();

@Override
@Nullable
public abstract AspectClass getOwningAspect();

public static AttributeDependencyKind forRule(Attribute attribute) {
return new AutoValue_DependencyKind_AttributeDependencyKind(attribute, null);
}

public static AttributeDependencyKind forAspect(Attribute attribute, AspectClass owningAspect) {
return new AutoValue_DependencyKind_AttributeDependencyKind(
attribute, Preconditions.checkNotNull(owningAspect));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis;

import static com.google.devtools.build.lib.analysis.DependencyKind.OUTPUT_FILE_RULE_DEPENDENCY;
import static com.google.devtools.build.lib.analysis.DependencyKind.TOOLCHAIN_DEPENDENCY;
import static com.google.devtools.build.lib.analysis.DependencyKind.VISIBILITY_DEPENDENCY;

import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.AspectCollection.AspectCycleOnPathException;
import com.google.devtools.build.lib.analysis.DependencyKind.AttributeDependencyKind;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory;
Expand Down Expand Up @@ -65,63 +70,6 @@
*/
public abstract class DependencyResolver {

/** A dependency caused by something that's not an attribute. Special cases enumerated below. */
private static final class NonAttributeDependencyKind implements DependencyKind {
private final String name;

private NonAttributeDependencyKind(String name) {
this.name = name;
}

@Override
public Attribute getAttribute() {
return null;
}

@Nullable
@Override
public AspectClass getOwningAspect() {
throw new IllegalStateException();
}

@Override
public String toString() {
return String.format("%s(%s)", getClass().getSimpleName(), this.name);
}
}

/** A dependency for visibility. */
public static final DependencyKind VISIBILITY_DEPENDENCY =
new NonAttributeDependencyKind("VISIBILITY");

/** The dependency on the rule that creates a given output file. */
public static final DependencyKind OUTPUT_FILE_RULE_DEPENDENCY =
new NonAttributeDependencyKind("OUTPUT_FILE");

/** A dependency on a resolved toolchain. */
public static final DependencyKind TOOLCHAIN_DEPENDENCY =
new NonAttributeDependencyKind("TOOLCHAIN");

/** A dependency through an attribute, either that of an aspect or the rule itself. */
@AutoValue
public abstract static class AttributeDependencyKind implements DependencyKind {
@Override
public abstract Attribute getAttribute();

@Override
@Nullable
public abstract AspectClass getOwningAspect();

public static AttributeDependencyKind forRule(Attribute attribute) {
return new AutoValue_DependencyResolver_AttributeDependencyKind(attribute, null);
}

public static AttributeDependencyKind forAspect(Attribute attribute, AspectClass owningAspect) {
return new AutoValue_DependencyResolver_AttributeDependencyKind(
attribute, Preconditions.checkNotNull(owningAspect));
}
}

/**
* What we know about a dependency edge after factoring in the properties of the configured target
* that the edge originates from, but not the properties of target it points to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void processOutput(Iterable<ConfiguredTarget> partialResult) throws Inter
dep.getTransition().apply(fromOptions, eventHandler).values();
String hostConfigurationChecksum = hostConfiguration.checksum();
String dependencyName;
if (attributeAndDep.getKey() == DependencyResolver.TOOLCHAIN_DEPENDENCY) {
if (attributeAndDep.getKey() == DependencyKind.TOOLCHAIN_DEPENDENCY) {
dependencyName = "[toolchain dependency]";
} else {
dependencyName = attributeAndDep.getKey().getAttribute().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.DependencyKind;
import com.google.devtools.build.lib.analysis.DependencyResolver;
import com.google.devtools.build.lib.analysis.DuplicateException;
import com.google.devtools.build.lib.analysis.InconsistentAspectOrderException;
import com.google.devtools.build.lib.analysis.ResolvedToolchainContext;
Expand Down Expand Up @@ -454,7 +453,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
targetPkg.getRepositoryMapping(),
unloadedToolchainContext,
targetDescription,
depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
depValueMap.get(DependencyKind.TOOLCHAIN_DEPENDENCY));
}

return createAspect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.Dependency;
import com.google.devtools.build.lib.analysis.DependencyKind;
import com.google.devtools.build.lib.analysis.DependencyResolver;
import com.google.devtools.build.lib.analysis.DuplicateException;
import com.google.devtools.build.lib.analysis.EmptyConfiguredTarget;
import com.google.devtools.build.lib.analysis.InconsistentAspectOrderException;
Expand Down Expand Up @@ -333,7 +332,7 @@ public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunc
target.getPackage().getRepositoryMapping(),
unloadedContext.getValue(),
targetDescription,
depValueMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY)));
depValueMap.get(DependencyKind.TOOLCHAIN_DEPENDENCY)));
}
toolchainContexts = contextsBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void missingEdgeHook(
}

String message;
if (dependencyKind == TOOLCHAIN_DEPENDENCY) {
if (dependencyKind == DependencyKind.TOOLCHAIN_DEPENDENCY) {
message =
String.format(
"Target '%s' depends on toolchain '%s', which cannot be found: %s'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public RuleContext getRuleContextForTesting(
target.getPackage().getRepositoryMapping(),
unloadedToolchainContext.getValue(),
targetDescription,
prerequisiteMap.get(DependencyResolver.TOOLCHAIN_DEPENDENCY));
prerequisiteMap.get(DependencyKind.TOOLCHAIN_DEPENDENCY));
resolvedToolchainContext.addContext(unloadedToolchainContext.getKey(), toolchainContext);
}

Expand Down

0 comments on commit e4f2b49

Please sign in to comment.