Search in sources :

Example 6 with AspectDescriptor

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

the class DependencyTest method withConfigurationAndAspects_BasicAccessors.

@Test
public void withConfigurationAndAspects_BasicAccessors() throws Exception {
    update();
    AspectDescriptor simpleAspect = new AspectDescriptor(TestAspects.SIMPLE_ASPECT);
    AspectDescriptor attributeAspect = new AspectDescriptor(TestAspects.ATTRIBUTE_ASPECT);
    AspectCollection twoAspects = AspectCollection.createForTests(ImmutableSet.of(simpleAspect, attributeAspect));
    Dependency targetDep = Dependency.withConfigurationAndAspects(Label.parseAbsolute("//a"), getTargetConfiguration(), twoAspects);
    assertThat(targetDep.getLabel()).isEqualTo(Label.parseAbsolute("//a"));
    assertThat(targetDep.hasStaticConfiguration()).isTrue();
    assertThat(targetDep.getConfiguration()).isEqualTo(getTargetConfiguration());
    assertThat(targetDep.getAspects()).isEqualTo(twoAspects);
    assertThat(targetDep.getAspectConfiguration(simpleAspect)).isEqualTo(getTargetConfiguration());
    assertThat(targetDep.getAspectConfiguration(attributeAspect)).isEqualTo(getTargetConfiguration());
    try {
        targetDep.getTransition();
        fail("withConfigurationAndAspects-created Dependencies should throw ISE on getTransition()");
    } catch (IllegalStateException ex) {
    // good. that's what I WANTED to happen.
    }
}
Also used : AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Test(org.junit.Test)

Example 7 with AspectDescriptor

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

the class ToplevelSkylarkAspectFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws LoadSkylarkAspectFunctionException, InterruptedException {
    SkylarkAspectLoadingKey aspectLoadingKey = (SkylarkAspectLoadingKey) skyKey.argument();
    String skylarkValueName = aspectLoadingKey.getSkylarkValueName();
    SkylarkImport extensionFile = aspectLoadingKey.getSkylarkImport();
    // Find label corresponding to skylark file, if one exists.
    ImmutableMap<String, Label> labelLookupMap;
    try {
        labelLookupMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(ImmutableList.of(extensionFile), Label.parseAbsoluteUnchecked("//:empty"), env);
    } catch (SkylarkImportFailedException e) {
        env.getListener().handle(Event.error(e.getMessage()));
        throw new LoadSkylarkAspectFunctionException(new AspectCreationException(e.getMessage()));
    }
    if (labelLookupMap == null) {
        return null;
    }
    SkylarkAspect skylarkAspect;
    Label extensionFileLabel = Iterables.getOnlyElement(labelLookupMap.values());
    try {
        skylarkAspect = AspectFunction.loadSkylarkAspect(env, extensionFileLabel, skylarkValueName);
        if (skylarkAspect == null) {
            return null;
        }
        if (!skylarkAspect.getParamAttributes().isEmpty()) {
            throw new AspectCreationException("Cannot instantiate parameterized aspect " + skylarkAspect.getName() + " at the top level.", extensionFileLabel);
        }
    } catch (AspectCreationException e) {
        throw new LoadSkylarkAspectFunctionException(e);
    }
    SkyKey aspectKey = ActionLookupValue.key(AspectValue.createAspectKey(aspectLoadingKey.getTargetLabel(), aspectLoadingKey.getTargetConfiguration(), new AspectDescriptor(skylarkAspect.getAspectClass(), AspectParameters.EMPTY), aspectLoadingKey.getAspectConfiguration()));
    return env.getValue(aspectKey);
}
Also used : SkylarkImport(com.google.devtools.build.lib.syntax.SkylarkImport) AspectCreationException(com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException) SkyKey(com.google.devtools.build.skyframe.SkyKey) SkylarkAspectLoadingKey(com.google.devtools.build.lib.skyframe.AspectValue.SkylarkAspectLoadingKey) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) Label(com.google.devtools.build.lib.cmdline.Label) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Nullable(javax.annotation.Nullable)

Example 8 with AspectDescriptor

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

the class SkyframeExecutor method getConfiguredTargetMap.

/**
   * Returns a map from {@link Dependency} inputs to the {@link ConfiguredTarget}s corresponding to
   * those dependencies.
   *
   * <p>For use for legacy support and tests calling through {@code BuildView} only.
   *
   * <p>If a requested configured target is in error, the corresponding value is omitted from the
   * returned list.
   */
@ThreadSafety.ThreadSafe
public ImmutableMultimap<Dependency, ConfiguredTarget> getConfiguredTargetMap(ExtendedEventHandler eventHandler, BuildConfiguration originalConfig, Iterable<Dependency> keys, boolean useOriginalConfig) {
    checkActive();
    Multimap<Dependency, BuildConfiguration> configs;
    if (originalConfig != null) {
        if (useOriginalConfig) {
            // This flag is used because of some unfortunate complexity in the configuration machinery:
            // Most callers of this method pass a <Label, Configuration> pair to directly create a
            // ConfiguredTarget from, but happen to use the Dependency data structure to pass that
            // info (even though the data has nothing to do with dependencies). If this configuration
            // includes a split transition, a dynamic configuration created from it will *not*
            // include that transition (because dynamic configurations don't embed transitions to
            // other configurations. In that case, we need to preserve the original configuration.
            // TODO(bazel-team); make this unnecessary once split transition logic is properly ported
            // out of configurations.
            configs = ArrayListMultimap.<Dependency, BuildConfiguration>create();
            configs.put(Iterables.getOnlyElement(keys), originalConfig);
        } else {
            configs = getConfigurations(eventHandler, originalConfig.getOptions(), keys);
        }
    } else {
        configs = ArrayListMultimap.<Dependency, BuildConfiguration>create();
        for (Dependency key : keys) {
            configs.put(key, null);
        }
    }
    final List<SkyKey> skyKeys = new ArrayList<>();
    for (Dependency key : keys) {
        if (!configs.containsKey(key)) {
            // it couldn't be loaded). Exclude it from the results.
            continue;
        }
        for (BuildConfiguration depConfig : configs.get(key)) {
            skyKeys.add(ConfiguredTargetValue.key(key.getLabel(), depConfig));
            for (AspectDescriptor aspectDescriptor : key.getAspects().getAllAspects()) {
                skyKeys.add(ActionLookupValue.key(AspectValue.createAspectKey(key.getLabel(), depConfig, aspectDescriptor, depConfig)));
            }
        }
    }
    EvaluationResult<SkyValue> result = evaluateSkyKeys(eventHandler, skyKeys);
    for (Map.Entry<SkyKey, ErrorInfo> entry : result.errorMap().entrySet()) {
        reportCycles(eventHandler, entry.getValue().getCycleInfo(), entry.getKey());
    }
    ImmutableMultimap.Builder<Dependency, ConfiguredTarget> cts = ImmutableMultimap.<Dependency, ConfiguredTarget>builder();
    DependentNodeLoop: for (Dependency key : keys) {
        if (!configs.containsKey(key)) {
            // it couldn't be loaded). Exclude it from the results.
            continue;
        }
        for (BuildConfiguration depConfig : configs.get(key)) {
            SkyKey configuredTargetKey = ConfiguredTargetValue.key(key.getLabel(), depConfig);
            if (result.get(configuredTargetKey) == null) {
                continue;
            }
            ConfiguredTarget configuredTarget = ((ConfiguredTargetValue) result.get(configuredTargetKey)).getConfiguredTarget();
            List<ConfiguredAspect> configuredAspects = new ArrayList<>();
            for (AspectDescriptor aspectDescriptor : key.getAspects().getAllAspects()) {
                SkyKey aspectKey = ActionLookupValue.key(AspectValue.createAspectKey(key.getLabel(), depConfig, aspectDescriptor, depConfig));
                if (result.get(aspectKey) == null) {
                    continue DependentNodeLoop;
                }
                configuredAspects.add(((AspectValue) result.get(aspectKey)).getConfiguredAspect());
            }
            try {
                cts.put(key, MergedConfiguredTarget.of(configuredTarget, configuredAspects));
            } catch (DuplicateException e) {
                throw new IllegalStateException(String.format("Error creating %s", configuredTarget.getTarget().getLabel()), e);
            }
        }
    }
    return cts.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ErrorInfo(com.google.devtools.build.skyframe.ErrorInfo) ArrayList(java.util.ArrayList) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Dependency(com.google.devtools.build.lib.analysis.Dependency) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) SkyValue(com.google.devtools.build.skyframe.SkyValue) DuplicateException(com.google.devtools.build.lib.analysis.MergedConfiguredTarget.DuplicateException) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 9 with AspectDescriptor

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

the class SkylarkAspectFactory method create.

@Override
public ConfiguredAspect create(ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters) throws InterruptedException {
    try (Mutability mutability = Mutability.create("aspect")) {
        AspectDescriptor aspectDescriptor = new AspectDescriptor(skylarkAspect.getAspectClass(), parameters);
        SkylarkRuleContext skylarkRuleContext;
        try {
            skylarkRuleContext = new SkylarkRuleContext(ruleContext, aspectDescriptor);
        } catch (EvalException e) {
            ruleContext.ruleError(e.getMessage());
            return null;
        }
        Environment env = Environment.builder(mutability).setGlobals(skylarkAspect.getFuncallEnv().getGlobals()).setEventHandler(ruleContext.getAnalysisEnvironment().getEventHandler()).build();
        // so we do *not* setLoadingPhase().
        Object aspectSkylarkObject;
        try {
            aspectSkylarkObject = skylarkAspect.getImplementation().call(ImmutableList.<Object>of(base, skylarkRuleContext), ImmutableMap.<String, Object>of(), /*ast=*/
            null, env);
            if (ruleContext.hasErrors()) {
                return null;
            } else if (!(aspectSkylarkObject instanceof SkylarkClassObject) && !(aspectSkylarkObject instanceof Iterable)) {
                ruleContext.ruleError(String.format("Aspect implementation should return a struct or a list, but got %s", SkylarkType.typeOf(aspectSkylarkObject)));
                return null;
            }
            return createAspect(aspectSkylarkObject, aspectDescriptor, ruleContext);
        } catch (EvalException e) {
            addAspectToStackTrace(base, e);
            ruleContext.ruleError("\n" + e.print());
            return null;
        }
    }
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Mutability(com.google.devtools.build.lib.syntax.Mutability) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Environment(com.google.devtools.build.lib.syntax.Environment) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext)

Example 10 with AspectDescriptor

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

the class AspectCollection method deduplicateAspects.

/**
   * Deduplicate aspects in path.
   *
   * @throws AspectCycleOnPathException if an aspect occurs twice on the path and
   *         the second occurrence sees a different set of aspects.
   */
private static LinkedHashMap<AspectDescriptor, Aspect> deduplicateAspects(Iterable<Aspect> aspectPath) throws AspectCycleOnPathException {
    LinkedHashMap<AspectDescriptor, Aspect> aspectMap = new LinkedHashMap<>();
    ArrayList<Aspect> seenAspects = new ArrayList<>();
    for (Aspect aspect : aspectPath) {
        if (!aspectMap.containsKey(aspect.getDescriptor())) {
            aspectMap.put(aspect.getDescriptor(), aspect);
            seenAspects.add(aspect);
        } else {
            validateDuplicateAspect(aspect, seenAspects);
        }
    }
    return aspectMap;
}
Also used : ArrayList(java.util.ArrayList) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Aspect(com.google.devtools.build.lib.packages.Aspect) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)21 Test (org.junit.Test)8 Aspect (com.google.devtools.build.lib.packages.Aspect)5 Attribute (com.google.devtools.build.lib.packages.Attribute)5 SkyKey (com.google.devtools.build.skyframe.SkyKey)5 ImmutableList (com.google.common.collect.ImmutableList)4 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)4 AspectDeps (com.google.devtools.build.lib.analysis.AspectCollection.AspectDeps)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)3 MergedConfiguredTarget (com.google.devtools.build.lib.analysis.MergedConfiguredTarget)3 TestAspects (com.google.devtools.build.lib.analysis.util.TestAspects)3 Label (com.google.devtools.build.lib.cmdline.Label)3 Rule (com.google.devtools.build.lib.packages.Rule)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)2 Dependency (com.google.devtools.build.lib.analysis.Dependency)2 DuplicateException (com.google.devtools.build.lib.analysis.MergedConfiguredTarget.DuplicateException)2