Search in sources :

Example 11 with AspectDescriptor

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

the class BuildView method update.

@ThreadCompatible
public AnalysisResult update(LoadingResult loadingResult, BuildConfigurationCollection configurations, List<String> aspects, Options viewOptions, TopLevelArtifactContext topLevelOptions, ExtendedEventHandler eventHandler, EventBus eventBus) throws ViewCreationFailedException, InterruptedException {
    LOG.info("Starting analysis");
    pollInterruptedStatus();
    skyframeBuildView.resetEvaluatedConfiguredTargetKeysSet();
    Collection<Target> targets = loadingResult.getTargets();
    eventBus.post(new AnalysisPhaseStartedEvent(targets));
    skyframeBuildView.setConfigurations(configurations);
    // Determine the configurations.
    List<TargetAndConfiguration> topLevelTargetsWithConfigs = nodesForTopLevelTargets(configurations, targets, eventHandler);
    List<ConfiguredTargetKey> topLevelCtKeys = Lists.transform(topLevelTargetsWithConfigs, new Function<TargetAndConfiguration, ConfiguredTargetKey>() {

        @Override
        public ConfiguredTargetKey apply(TargetAndConfiguration node) {
            return new ConfiguredTargetKey(node.getLabel(), node.getConfiguration());
        }
    });
    List<AspectValueKey> aspectKeys = new ArrayList<>();
    for (String aspect : aspects) {
        // Syntax: label%aspect
        int delimiterPosition = aspect.indexOf('%');
        if (delimiterPosition >= 0) {
            // TODO(jfield): For consistency with Skylark loads, the aspect should be specified
            // as an absolute path. Also, we probably need to do at least basic validation of
            // path well-formedness here.
            String bzlFileLoadLikeString = aspect.substring(0, delimiterPosition);
            if (!bzlFileLoadLikeString.startsWith("//") && !bzlFileLoadLikeString.startsWith("@")) {
                // "Legacy" behavior of '--aspects' parameter.
                bzlFileLoadLikeString = new PathFragment("/" + bzlFileLoadLikeString).toString();
                if (bzlFileLoadLikeString.endsWith(".bzl")) {
                    bzlFileLoadLikeString = bzlFileLoadLikeString.substring(0, bzlFileLoadLikeString.length() - ".bzl".length());
                }
            }
            SkylarkImport skylarkImport;
            try {
                skylarkImport = SkylarkImports.create(bzlFileLoadLikeString);
            } catch (SkylarkImportSyntaxException e) {
                throw new ViewCreationFailedException(String.format("Invalid aspect '%s': %s", aspect, e.getMessage()), e);
            }
            String skylarkFunctionName = aspect.substring(delimiterPosition + 1);
            for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) {
                if (!(targetSpec.getTarget() instanceof Rule)) {
                    continue;
                }
                aspectKeys.add(AspectValue.createSkylarkAspectKey(targetSpec.getLabel(), // aspect and the base target while the top-level configuration is untrimmed.
                targetSpec.getConfiguration(), targetSpec.getConfiguration(), skylarkImport, skylarkFunctionName));
            }
        } else {
            final NativeAspectClass aspectFactoryClass = ruleClassProvider.getNativeAspectClassMap().get(aspect);
            if (aspectFactoryClass != null) {
                for (TargetAndConfiguration targetSpec : topLevelTargetsWithConfigs) {
                    if (!(targetSpec.getTarget() instanceof Rule)) {
                        continue;
                    }
                    // For invoking top-level aspects, use the top-level configuration for both the
                    // aspect and the base target while the top-level configuration is untrimmed.
                    BuildConfiguration configuration = targetSpec.getConfiguration();
                    aspectKeys.add(AspectValue.createAspectKey(targetSpec.getLabel(), configuration, new AspectDescriptor(aspectFactoryClass, AspectParameters.EMPTY), configuration));
                }
            } else {
                throw new ViewCreationFailedException("Aspect '" + aspect + "' is unknown");
            }
        }
    }
    skyframeExecutor.injectWorkspaceStatusData(loadingResult.getWorkspaceName());
    SkyframeAnalysisResult skyframeAnalysisResult;
    try {
        skyframeAnalysisResult = skyframeBuildView.configureTargets(eventHandler, topLevelCtKeys, aspectKeys, eventBus, viewOptions.keepGoing, viewOptions.loadingPhaseThreads);
        setArtifactRoots(skyframeAnalysisResult.getPackageRoots());
    } finally {
        skyframeBuildView.clearInvalidatedConfiguredTargets();
    }
    int numTargetsToAnalyze = topLevelTargetsWithConfigs.size();
    int numSuccessful = skyframeAnalysisResult.getConfiguredTargets().size();
    if (0 < numSuccessful && numSuccessful < numTargetsToAnalyze) {
        String msg = String.format("Analysis succeeded for only %d of %d top-level targets", numSuccessful, numTargetsToAnalyze);
        eventHandler.handle(Event.info(msg));
        LOG.info(msg);
    }
    AnalysisResult result = createResult(eventHandler, loadingResult, topLevelOptions, viewOptions, skyframeAnalysisResult);
    LOG.info("Finished analysis");
    return result;
}
Also used : SkylarkImportSyntaxException(com.google.devtools.build.lib.syntax.SkylarkImports.SkylarkImportSyntaxException) SkyframeAnalysisResult(com.google.devtools.build.lib.skyframe.SkyframeAnalysisResult) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) SkyframeAnalysisResult(com.google.devtools.build.lib.skyframe.SkyframeAnalysisResult) SkylarkImport(com.google.devtools.build.lib.syntax.SkylarkImport) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) Target(com.google.devtools.build.lib.packages.Target) AspectValueKey(com.google.devtools.build.lib.skyframe.AspectValue.AspectValueKey) NativeAspectClass(com.google.devtools.build.lib.packages.NativeAspectClass) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Rule(com.google.devtools.build.lib.packages.Rule) ConfiguredTargetKey(com.google.devtools.build.lib.skyframe.ConfiguredTargetKey) ThreadCompatible(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible)

Example 12 with AspectDescriptor

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

the class DependencyResolver method requiredAspects.

private AspectCollection requiredAspects(Iterable<Aspect> aspectPath, AttributeAndOwner attributeAndOwner, final Target target, Rule originalRule) throws InconsistentAspectOrderException {
    if (!(target instanceof Rule)) {
        return AspectCollection.EMPTY;
    }
    if (attributeAndOwner.ownerAspect != null) {
        // Do not propagate aspects along aspect attributes.
        return AspectCollection.EMPTY;
    }
    ImmutableList.Builder<Aspect> filteredAspectPath = ImmutableList.builder();
    ImmutableSet.Builder<AspectDescriptor> visibleAspects = ImmutableSet.builder();
    Attribute attribute = attributeAndOwner.attribute;
    collectOriginatingAspects(originalRule, attribute, (Rule) target, filteredAspectPath, visibleAspects);
    collectPropagatingAspects(aspectPath, attribute, (Rule) target, filteredAspectPath, visibleAspects);
    try {
        return AspectCollection.create(filteredAspectPath.build(), visibleAspects.build());
    } catch (AspectCycleOnPathException e) {
        throw new InconsistentAspectOrderException(originalRule, attribute, target, e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Attribute(com.google.devtools.build.lib.packages.Attribute) AspectCycleOnPathException(com.google.devtools.build.lib.analysis.AspectCollection.AspectCycleOnPathException) ImmutableList(com.google.common.collect.ImmutableList) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Rule(com.google.devtools.build.lib.packages.Rule) Aspect(com.google.devtools.build.lib.packages.Aspect)

Example 13 with AspectDescriptor

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

the class DependencyResolverTest method hasAllAttributesAspect.

@Test
public void hasAllAttributesAspect() throws Exception {
    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.SimpleRule());
    pkg("a", "simple(name='a', foo=[':b'])", "simple(name='b', foo=[])");
    OrderedSetMultimap<Attribute, Dependency> map = dependentNodeMap("//a:a", TestAspects.ALL_ATTRIBUTES_ASPECT);
    assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.ALL_ATTRIBUTES_ASPECT));
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) TestAspects(com.google.devtools.build.lib.analysis.util.TestAspects) Test(org.junit.Test)

Example 14 with AspectDescriptor

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

the class DependencyResolverTest method hasAspectsRequiredByRule.

@Test
public void hasAspectsRequiredByRule() throws Exception {
    setRulesAvailableInTests(new AspectRequiringRule(), new TestAspects.BaseRule());
    pkg("a", "aspect(name='a', foo=[':b'])", "aspect(name='b', foo=[])");
    OrderedSetMultimap<Attribute, Dependency> map = dependentNodeMap("//a:a", null);
    assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.SIMPLE_ASPECT));
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AspectRequiringRule(com.google.devtools.build.lib.analysis.util.TestAspects.AspectRequiringRule) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) TestAspects(com.google.devtools.build.lib.analysis.util.TestAspects) Test(org.junit.Test)

Example 15 with AspectDescriptor

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

the class DependencyTest method withTransitionAndAspects_BasicAccessors.

@Test
public void withTransitionAndAspects_BasicAccessors() throws Exception {
    AspectDescriptor simpleAspect = new AspectDescriptor(TestAspects.SIMPLE_ASPECT);
    AspectDescriptor attributeAspect = new AspectDescriptor(TestAspects.ATTRIBUTE_ASPECT);
    AspectCollection twoAspects = AspectCollection.createForTests(ImmutableSet.of(simpleAspect, attributeAspect));
    Dependency hostDep = Dependency.withTransitionAndAspects(Label.parseAbsolute("//a"), ConfigurationTransition.HOST, twoAspects);
    assertThat(hostDep.getLabel()).isEqualTo(Label.parseAbsolute("//a"));
    assertThat(hostDep.hasStaticConfiguration()).isFalse();
    assertThat(hostDep.getAspects().getAllAspects()).containsExactlyElementsIn(twoAspects.getAllAspects());
    assertThat(hostDep.getTransition()).isEqualTo(ConfigurationTransition.HOST);
    try {
        hostDep.getConfiguration();
        fail("withTransitionAndAspects-created Dependencies should throw ISE on getConfiguration()");
    } catch (IllegalStateException ex) {
    // good. I knew you would do that.
    }
    try {
        hostDep.getAspectConfiguration(simpleAspect);
        fail("withTransitionAndAspects-created Dependencies should throw ISE on " + "getAspectConfiguration()");
    } catch (IllegalStateException ex) {
    // good. you're so predictable.
    }
    try {
        hostDep.getAspectConfiguration(attributeAspect);
        fail("withTransitionAndAspects-created Dependencies should throw ISE on " + "getAspectConfiguration()");
    } catch (IllegalStateException ex) {
    // good. you're so predictable.
    }
}
Also used : AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Test(org.junit.Test)

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