Search in sources :

Example 6 with Aspect

use of com.google.devtools.build.lib.packages.Aspect in project bazel by bazelbuild.

the class AspectCollectionTest method duplicateAspect3.

/**
   * a2 wants a1, a3 wants a1 and a2, a1 wants a2. the path is [a2, a1, a2, a3], so a2 occurs twice.
   * First occurrence of a2 does not see a1, but the second does => error.
   */
@Test
public void duplicateAspect3() throws Exception {
    Aspect a1 = createAspect("a1", "a2");
    Aspect a2 = createAspect("a2", "a1");
    Aspect a3 = createAspect("a3", "a1", "a2");
    try {
        AspectCollection.create(ImmutableList.of(a2, a1, a2, a3), ImmutableSet.of(a3.getDescriptor()));
        Assert.fail();
    } catch (AspectCycleOnPathException e) {
        assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
        assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
    }
}
Also used : AspectCycleOnPathException(com.google.devtools.build.lib.analysis.AspectCollection.AspectCycleOnPathException) Aspect(com.google.devtools.build.lib.packages.Aspect) Test(org.junit.Test)

Example 7 with Aspect

use of com.google.devtools.build.lib.packages.Aspect in project bazel by bazelbuild.

the class AspectCollectionTest method validateOrder2.

/**
   * a3 wants a1, a1 wants a2, a2 wants a1, path is a1, a2, a3, so a2 comes after a1.
   */
@Test
public void validateOrder2() throws Exception {
    Aspect a1 = createAspect("a1", "a2");
    Aspect a2 = createAspect("a2", "a1");
    Aspect a3 = createAspect("a3", "a1");
    AspectCollection collection = AspectCollection.create(ImmutableList.of(a1, a2, a3), ImmutableSet.of(a3.getDescriptor()));
    validateAspectCollection(collection, ImmutableList.of(a1, a3), ImmutableList.of(a3), expectDeps(a3, a1), expectDeps(a1));
}
Also used : Aspect(com.google.devtools.build.lib.packages.Aspect) Test(org.junit.Test)

Example 8 with Aspect

use of com.google.devtools.build.lib.packages.Aspect in project bazel by bazelbuild.

the class AspectCollectionTest method unneededRemoved.

/**
   * a3 wants no one => a1 and a2 must be removed.
   */
@Test
public void unneededRemoved() throws Exception {
    Aspect a1 = createAspect("a1");
    Aspect a2 = createAspect("a2");
    Aspect a3 = createAspect("a3");
    AspectCollection collection = AspectCollection.create(ImmutableList.of(a1, a2, a3), ImmutableSet.of(a3.getDescriptor()));
    validateAspectCollection(collection, ImmutableList.of(a3), ImmutableList.of(a3), expectDeps(a3));
}
Also used : Aspect(com.google.devtools.build.lib.packages.Aspect) Test(org.junit.Test)

Example 9 with Aspect

use of com.google.devtools.build.lib.packages.Aspect in project bazel by bazelbuild.

the class AspectCollection method create.

/**
   *  Creates an {@link AspectCollection} from an ordered list of aspects and
   *  a set of visible aspects.
   *
   *  The order of aspects is reverse to the order in which they originated, with
   *  the earliest originating occurring last in the list.
   */
public static AspectCollection create(Iterable<Aspect> aspectPath, Set<AspectDescriptor> visibleAspects) throws AspectCycleOnPathException {
    LinkedHashMap<AspectDescriptor, Aspect> aspectMap = deduplicateAspects(aspectPath);
    LinkedHashMap<AspectDescriptor, ArrayList<AspectDescriptor>> deps = new LinkedHashMap<>();
    // deps[aspect] contains all aspects that 'aspect' needs, in reverse order.
    for (Entry<AspectDescriptor, Aspect> aspect : ImmutableList.copyOf(aspectMap.entrySet()).reverse()) {
        boolean needed = visibleAspects.contains(aspect.getKey());
        for (AspectDescriptor depAspectDescriptor : deps.keySet()) {
            if (depAspectDescriptor.equals(aspect.getKey())) {
                continue;
            }
            Aspect depAspect = aspectMap.get(depAspectDescriptor);
            if (depAspect.getDefinition().getRequiredProvidersForAspects().isSatisfiedBy(aspect.getValue().getDefinition().getAdvertisedProviders())) {
                deps.get(depAspectDescriptor).add(aspect.getKey());
                needed = true;
            }
        }
        if (needed && !deps.containsKey(aspect.getKey())) {
            deps.put(aspect.getKey(), new ArrayList<AspectDescriptor>());
        }
    }
    // Record only the needed aspects from all aspects, in correct order.
    ImmutableList<AspectDescriptor> neededAspects = ImmutableList.copyOf(deps.keySet()).reverse();
    // Calculate visible aspect paths.
    HashMap<AspectDescriptor, AspectDeps> aspectPaths = new HashMap<>();
    ImmutableSet.Builder<AspectDeps> visibleAspectPaths = ImmutableSet.builder();
    for (AspectDescriptor visibleAspect : visibleAspects) {
        visibleAspectPaths.add(buildAspectDeps(visibleAspect, aspectPaths, deps));
    }
    return new AspectCollection(ImmutableSet.copyOf(neededAspects), visibleAspectPaths.build());
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Aspect(com.google.devtools.build.lib.packages.Aspect) LinkedHashMap(java.util.LinkedHashMap) ImmutableSet(com.google.common.collect.ImmutableSet) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor)

Example 10 with Aspect

use of com.google.devtools.build.lib.packages.Aspect in project bazel by bazelbuild.

the class DependencyResolver method collectOriginatingAspects.

/**
   * Collect all aspects that originate on {@code attribute} of {@code originalRule}
   * and are applicable to a {@code target}
   *
   * They are appended to {@code filteredAspectPath} and registered in {@code visibleAspects} set.
   */
private static void collectOriginatingAspects(Rule originalRule, Attribute attribute, Rule target, ImmutableList.Builder<Aspect> filteredAspectPath, ImmutableSet.Builder<AspectDescriptor> visibleAspects) {
    ImmutableList<Aspect> baseAspects = attribute.getAspects(originalRule);
    RuleClass ruleClass = target.getRuleClassObject();
    for (Aspect baseAspect : baseAspects) {
        if (baseAspect.getDefinition().getRequiredProviders().isSatisfiedBy(ruleClass.getAdvertisedProviders())) {
            filteredAspectPath.add(baseAspect);
            visibleAspects.add(baseAspect.getDescriptor());
        }
    }
}
Also used : Aspect(com.google.devtools.build.lib.packages.Aspect) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Aggregations

Aspect (com.google.devtools.build.lib.packages.Aspect)22 Test (org.junit.Test)14 AspectCycleOnPathException (com.google.devtools.build.lib.analysis.AspectCollection.AspectCycleOnPathException)5 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)5 Attribute (com.google.devtools.build.lib.packages.Attribute)4 Rule (com.google.devtools.build.lib.packages.Rule)4 ImmutableList (com.google.common.collect.ImmutableList)3 Label (com.google.devtools.build.lib.cmdline.Label)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Package (com.google.devtools.build.lib.packages.Package)2 Target (com.google.devtools.build.lib.packages.Target)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 AspectDeps (com.google.devtools.build.lib.analysis.AspectCollection.AspectDeps)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 ConfiguredAspectFactory (com.google.devtools.build.lib.analysis.ConfiguredAspectFactory)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)1 MergedConfiguredTarget (com.google.devtools.build.lib.analysis.MergedConfiguredTarget)1