Search in sources :

Example 11 with RawAttributeMapper

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

the class ConfiguredTargetFunction method getConfigConditions.

/**
   * Returns the set of {@link ConfigMatchingProvider}s that key the configurable attributes used by
   * this rule.
   *
   * <p>>If the configured targets supplying those providers aren't yet resolved by the dependency
   * resolver, returns null.
   */
@Nullable
static ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions(Target target, Environment env, SkyframeDependencyResolver resolver, TargetAndConfiguration ctgValue, NestedSetBuilder<Package> transitivePackages, NestedSetBuilder<Label> transitiveLoadingRootCauses) throws DependencyEvaluationException, InterruptedException {
    if (!(target instanceof Rule)) {
        return NO_CONFIG_CONDITIONS;
    }
    Map<Label, ConfigMatchingProvider> configConditions = new LinkedHashMap<>();
    // Collect the labels of the configured targets we need to resolve.
    OrderedSetMultimap<Attribute, Label> configLabelMap = OrderedSetMultimap.create();
    RawAttributeMapper attributeMap = RawAttributeMapper.of(((Rule) target));
    for (Attribute a : ((Rule) target).getAttributes()) {
        for (Label configLabel : attributeMap.getConfigurabilityKeys(a.getName(), a.getType())) {
            if (!BuildType.Selector.isReservedLabel(configLabel)) {
                configLabelMap.put(a, target.getLabel().resolveRepositoryRelative(configLabel));
            }
        }
    }
    if (configLabelMap.isEmpty()) {
        return NO_CONFIG_CONDITIONS;
    }
    // Collect the corresponding Skyframe configured target values. Abort early if they haven't
    // been computed yet.
    Collection<Dependency> configValueNames = null;
    try {
        configValueNames = resolver.resolveRuleLabels(ctgValue, configLabelMap, transitiveLoadingRootCauses);
    } catch (InconsistentAspectOrderException e) {
        throw new DependencyEvaluationException(e);
    }
    if (env.valuesMissing()) {
        return null;
    }
    // No need to get new configs from Skyframe - config_setting rules always use the current
    // target's config.
    // TODO(bazel-team): remove the need for this special transformation. We can probably do this by
    // simply passing this through trimConfigurations.
    BuildConfiguration targetConfig = ctgValue.getConfiguration();
    if (useDynamicConfigurations(targetConfig)) {
        ImmutableList.Builder<Dependency> staticConfigs = ImmutableList.builder();
        for (Dependency dep : configValueNames) {
            staticConfigs.add(Dependency.withConfigurationAndAspects(dep.getLabel(), targetConfig, dep.getAspects()));
        }
        configValueNames = staticConfigs.build();
    }
    Map<SkyKey, ConfiguredTarget> configValues = resolveConfiguredTargetDependencies(env, configValueNames, transitivePackages, transitiveLoadingRootCauses);
    if (configValues == null) {
        return null;
    }
    // Get the configured targets as ConfigMatchingProvider interfaces.
    for (Dependency entry : configValueNames) {
        ConfiguredTarget value = configValues.get(TO_KEYS.apply(entry));
        // The code above guarantees that value is non-null here.
        ConfigMatchingProvider provider = value.getProvider(ConfigMatchingProvider.class);
        if (provider != null) {
            configConditions.put(entry.getLabel(), provider);
        } else {
            // Not a valid provider for configuration conditions.
            String message = entry.getLabel() + " is not a valid configuration key for " + target.getLabel();
            env.getListener().handle(Event.error(TargetUtils.getLocationMaybe(target), message));
            throw new DependencyEvaluationException(new ConfiguredValueCreationException(message, target.getLabel()));
        }
    }
    return ImmutableMap.copyOf(configConditions);
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) SkyKey(com.google.devtools.build.skyframe.SkyKey) Attribute(com.google.devtools.build.lib.packages.Attribute) ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Dependency(com.google.devtools.build.lib.analysis.Dependency) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) LinkedHashMap(java.util.LinkedHashMap) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Rule(com.google.devtools.build.lib.packages.Rule) Nullable(javax.annotation.Nullable)

Example 12 with RawAttributeMapper

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

the class PostConfiguredTargetFunction method getConfigurableAttributeConditions.

/**
   * Returns the configurable attribute conditions necessary to evaluate the given configured
   * target, or null if not all dependencies have yet been SkyFrame-evaluated.
   */
@Nullable
private static ImmutableMap<Label, ConfigMatchingProvider> getConfigurableAttributeConditions(TargetAndConfiguration ctg, Environment env) throws InterruptedException {
    if (!(ctg.getTarget() instanceof Rule)) {
        return ImmutableMap.of();
    }
    Rule rule = (Rule) ctg.getTarget();
    RawAttributeMapper mapper = RawAttributeMapper.of(rule);
    Set<SkyKey> depKeys = new LinkedHashSet<>();
    for (Attribute attribute : rule.getAttributes()) {
        for (Label label : mapper.getConfigurabilityKeys(attribute.getName(), attribute.getType())) {
            if (!BuildType.Selector.isReservedLabel(label)) {
                depKeys.add(ConfiguredTargetValue.key(label, ctg.getConfiguration()));
            }
        }
    }
    Map<SkyKey, SkyValue> cts = env.getValues(depKeys);
    if (env.valuesMissing()) {
        return null;
    }
    Map<Label, ConfigMatchingProvider> conditions = new LinkedHashMap<>();
    for (Map.Entry<SkyKey, SkyValue> entry : cts.entrySet()) {
        Label label = ((ConfiguredTargetKey) entry.getKey().argument()).getLabel();
        ConfiguredTarget ct = ((ConfiguredTargetValue) entry.getValue()).getConfiguredTarget();
        conditions.put(label, Preconditions.checkNotNull(ct.getProvider(ConfigMatchingProvider.class)));
    }
    return ImmutableMap.copyOf(conditions);
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) LinkedHashMap(java.util.LinkedHashMap) SkyValue(com.google.devtools.build.skyframe.SkyValue) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Rule(com.google.devtools.build.lib.packages.Rule) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 13 with RawAttributeMapper

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

the class BuildViewTestCase method getImplicitOutputArtifact.

protected Artifact getImplicitOutputArtifact(ConfiguredTarget target, SafeImplicitOutputsFunction outputFunction) {
    Rule associatedRule = target.getTarget().getAssociatedRule();
    RepositoryName repository = associatedRule.getRepository();
    BuildConfiguration configuration = target.getConfiguration();
    Root root;
    if (associatedRule.hasBinaryOutput()) {
        root = configuration.getBinDirectory(repository);
    } else {
        root = configuration.getGenfilesDirectory(repository);
    }
    ArtifactOwner owner = new ConfiguredTargetKey(target.getTarget().getLabel(), target.getConfiguration());
    RawAttributeMapper attr = RawAttributeMapper.of(associatedRule);
    String path = Iterables.getOnlyElement(outputFunction.getImplicitOutputs(attr));
    return view.getArtifactFactory().getDerivedArtifact(target.getTarget().getLabel().getPackageFragment().getRelative(path), root, owner);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) ArtifactOwner(com.google.devtools.build.lib.actions.ArtifactOwner) Root(com.google.devtools.build.lib.actions.Root) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) Rule(com.google.devtools.build.lib.packages.Rule) ConfiguredTargetKey(com.google.devtools.build.lib.skyframe.ConfiguredTargetKey)

Example 14 with RawAttributeMapper

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

the class RawAttributeMapperTest method testGetAttribute.

@Test
public void testGetAttribute() throws Exception {
    RawAttributeMapper rawMapper = RawAttributeMapper.of(setupGenRule());
    List<Label> value = rawMapper.get("data", BuildType.LABEL_LIST);
    assertNotNull(value);
    assertThat(value).containsExactly(Label.create("@//x", "data_a"), Label.create("@//x", "data_b"));
    // type mismatch exception.
    try {
        rawMapper.get("srcs", BuildType.LABEL_LIST);
        fail("Expected srcs lookup to fail since the returned type is a SelectorList and not a list");
    } catch (IllegalArgumentException e) {
        assertThat(e.getCause().getMessage()).contains("SelectorList cannot be cast to java.util.List");
    }
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) Label(com.google.devtools.build.lib.cmdline.Label) Test(org.junit.Test)

Example 15 with RawAttributeMapper

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

the class RawAttributeMapperTest method testVisitLabels.

/**
   * Tests that RawAttributeMapper can't handle label visitation with configurable attributes.
   */
@Test
public void testVisitLabels() throws Exception {
    RawAttributeMapper rawMapper = RawAttributeMapper.of(setupGenRule());
    try {
        rawMapper.visitLabels(new AttributeMap.AcceptsLabelAttribute() {

            @Override
            public void acceptLabelAttribute(Label label, Attribute attribute) {
            // Nothing to do.
            }
        });
        fail("Expected label visitation to fail since one attribute is configurable");
    } catch (IllegalArgumentException e) {
        assertThat(e.getCause().getMessage()).contains("SelectorList cannot be cast to java.util.List");
    }
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) Test(org.junit.Test)

Aggregations

RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)16 Label (com.google.devtools.build.lib.cmdline.Label)10 Rule (com.google.devtools.build.lib.packages.Rule)9 Test (org.junit.Test)7 Attribute (com.google.devtools.build.lib.packages.Attribute)4 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)3 LinkedHashMap (java.util.LinkedHashMap)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)2 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)2 FileTarget (com.google.devtools.build.lib.packages.FileTarget)2 Target (com.google.devtools.build.lib.packages.Target)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 Nullable (javax.annotation.Nullable)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArtifactOwner (com.google.devtools.build.lib.actions.ArtifactOwner)1 Root (com.google.devtools.build.lib.actions.Root)1 Dependency (com.google.devtools.build.lib.analysis.Dependency)1 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)1