Search in sources :

Example 61 with Attribute

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

the class DumpCommand method dumpRuleClasses.

private void dumpRuleClasses(BlazeRuntime runtime, PrintStream out) {
    PackageFactory factory = runtime.getPackageFactory();
    List<String> ruleClassNames = new ArrayList<>(factory.getRuleClassNames());
    Collections.sort(ruleClassNames);
    for (String name : ruleClassNames) {
        if (name.startsWith("$")) {
            continue;
        }
        RuleClass ruleClass = factory.getRuleClass(name);
        out.print(ruleClass + "(");
        boolean first = true;
        for (Attribute attribute : ruleClass.getAttributes()) {
            if (attribute.isImplicit()) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                out.print(", ");
            }
            out.print(attribute.getName());
        }
        out.println(")");
    }
}
Also used : PackageFactory(com.google.devtools.build.lib.packages.PackageFactory) Attribute(com.google.devtools.build.lib.packages.Attribute) ArrayList(java.util.ArrayList) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Example 62 with Attribute

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

the class ConfiguredTargetFunction method mergeAspects.

/**
   * Merges the each direct dependency configured target with the aspects associated with it.
   *
   * <p>Note that the combination of a configured target and its associated aspects are not
   * represented by a Skyframe node. This is because there can possibly be many different
   * combinations of aspects for a particular configured target, so it would result in a
   * combinatiorial explosion of Skyframe nodes.
   */
private static OrderedSetMultimap<Attribute, ConfiguredTarget> mergeAspects(OrderedSetMultimap<Attribute, Dependency> depValueNames, Map<SkyKey, ConfiguredTarget> depConfiguredTargetMap, OrderedSetMultimap<SkyKey, ConfiguredAspect> depAspectMap) throws DuplicateException {
    OrderedSetMultimap<Attribute, ConfiguredTarget> result = OrderedSetMultimap.create();
    for (Map.Entry<Attribute, Dependency> entry : depValueNames.entries()) {
        Dependency dep = entry.getValue();
        SkyKey depKey = TO_KEYS.apply(dep);
        ConfiguredTarget depConfiguredTarget = depConfiguredTargetMap.get(depKey);
        result.put(entry.getKey(), MergedConfiguredTarget.of(depConfiguredTarget, depAspectMap.get(depKey)));
    }
    return result;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Attribute(com.google.devtools.build.lib.packages.Attribute) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Dependency(com.google.devtools.build.lib.analysis.Dependency) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 63 with Attribute

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

the class ConfiguredTargetFunction method getDynamicConfigurations.

/**
   * Creates a dynamic configuration for each dep that's custom-fitted specifically for that dep.
   *
   * <p>More specifically: given a set of {@link Dependency} instances holding dynamic config
   * transition requests (e.g. {@link Dependency#hasStaticConfiguration()} == false}), returns
   * equivalent dependencies containing dynamically created configurations applying those
   * transitions. If {@link BuildConfiguration.Options#trimConfigurations()} is true, these
   * configurations only contain the fragments needed by the dep and its transitive closure. Else
   * the configurations unconditionally include all fragments.
   *
   * <p>This method is heavily performance-optimized. Because it, in aggregate, reads over every
   * edge in the configured target graph, small inefficiencies can have observable impact on
   * analysis time. Keep this in mind when making modifications and performance-test any changes you
   * make.
   *
   * @param env Skyframe evaluation environment
   * @param ctgValue the label and the configuration of the node
   * @param originalDeps the set of configuration transition requests for this target's attributes
   * @param hostConfiguration the host configuration
   * @param ruleClassProvider the rule class provider for determining the right configuration
   *    fragments to apply to deps
   *
   * @return a mapping from each attribute to the {@link BuildConfiguration}s and {@link Label}s
   *    to use for that attribute's deps. Returns null if not all Skyframe dependencies are
   *    available yet.
   */
@Nullable
static OrderedSetMultimap<Attribute, Dependency> getDynamicConfigurations(Environment env, TargetAndConfiguration ctgValue, OrderedSetMultimap<Attribute, Dependency> originalDeps, BuildConfiguration hostConfiguration, RuleClassProvider ruleClassProvider) throws DependencyEvaluationException, InterruptedException {
    // Maps each Skyframe-evaluated BuildConfiguration to the dependencies that need that
    // configuration. For cases where Skyframe isn't needed to get the configuration (e.g. when
    // we just re-used the original rule's configuration), we should skip this outright.
    Multimap<SkyKey, Map.Entry<Attribute, Dependency>> keysToEntries = LinkedListMultimap.create();
    // Stores the result of applying a dynamic transition to the current configuration using a
    // particular subset of fragments. By caching this, we save from redundantly computing the
    // same transition for every dependency edge that requests that transition. This can have
    // real effect on analysis time for commonly triggered transitions.
    //
    // Split transitions may map to multiple values. All other transitions map to one.
    Map<FragmentsAndTransition, List<BuildOptions>> transitionsMap = new LinkedHashMap<>();
    // The fragments used by the current target's configuration.
    Set<Class<? extends BuildConfiguration.Fragment>> ctgFragments = ctgValue.getConfiguration().fragmentClasses();
    BuildOptions ctgOptions = ctgValue.getConfiguration().getOptions();
    // Stores the dynamically configured versions of each dependency. This method must preserve the
    // original label ordering of each attribute. For example, if originalDeps.get("data") is
    // [":a", ":b"], the dynamic variant must also be [":a", ":b"] in the same order. Because we may
    // not actualize the results in order (some results need Skyframe-evaluated configurations while
    // others can be computed trivially), we dump them all into this map, then as a final step
    // iterate through the original list and pluck out values from here for the final value.
    //
    // For split transitions, originaldeps.get("data") = [":a", ":b"] can produce the output
    // [":a"<config1>, ":a"<config2>, ..., ":b"<config1>, ":b"<config2>, ...]. All instances of ":a"
    // still appear before all instances of ":b". But the [":a"<config1>, ":a"<config2>"] subset may
    // be in any (deterministic) order. In particular, this may not be the same order as
    // SplitTransition.split. If needed, this code can be modified to use that order, but that
    // involves more runtime in performance-critical code, so we won't make that change without a
    // clear need.
    //
    // This map is used heavily by all builds. Inserts and gets should be as fast as possible.
    Multimap<AttributeAndLabel, Dependency> dynamicDeps = LinkedHashMultimap.create();
    // Performance optimization: This method iterates over originalDeps twice. By storing
    // AttributeAndLabel instances in this list, we avoid having to recreate them the second time
    // (particularly avoid recomputing their hash codes). Profiling shows this shaves 25% off this
    // method's execution time (at the time of this comment).
    ArrayList<AttributeAndLabel> attributesAndLabels = new ArrayList<>(originalDeps.size());
    for (Map.Entry<Attribute, Dependency> depsEntry : originalDeps.entries()) {
        Dependency dep = depsEntry.getValue();
        AttributeAndLabel attributeAndLabel = new AttributeAndLabel(depsEntry.getKey(), dep.getLabel());
        attributesAndLabels.add(attributeAndLabel);
        // simple cc_binary show this saves ~1% of total analysis phase time.
        if (dep.hasStaticConfiguration()) {
            continue;
        }
        // Figure out the required fragments for this dep and its transitive closure.
        Set<Class<? extends BuildConfiguration.Fragment>> depFragments = getTransitiveFragments(env, dep.getLabel(), ctgValue.getConfiguration());
        if (depFragments == null) {
            return null;
        }
        // to 0.5% of total analysis time as profiled over a simple cc_binary).
        if (ctgValue.getConfiguration().trimConfigurations()) {
            checkForMissingFragments(env, ctgValue, attributeAndLabel.attribute.getName(), dep, depFragments);
        }
        boolean sameFragments = depFragments.equals(ctgFragments);
        Attribute.Transition transition = dep.getTransition();
        if (sameFragments) {
            if (transition == Attribute.ConfigurationTransition.NONE) {
                // The dep uses the same exact configuration.
                putOnlyEntry(dynamicDeps, attributeAndLabel, Dependency.withConfigurationAndAspects(dep.getLabel(), ctgValue.getConfiguration(), dep.getAspects()));
                continue;
            } else if (transition == HostTransition.INSTANCE) {
                // The current rule's host configuration can also be used for the dep. We short-circuit
                // the standard transition logic for host transitions because these transitions are
                // uniquely frequent. It's possible, e.g., for every node in the configured target graph
                // to incur multiple host transitions. So we aggressively optimize to avoid hurting
                // analysis time.
                putOnlyEntry(dynamicDeps, attributeAndLabel, Dependency.withConfigurationAndAspects(dep.getLabel(), hostConfiguration, dep.getAspects()));
                continue;
            }
        }
        // Apply the transition or use the cached result if it was already applied.
        FragmentsAndTransition transitionKey = new FragmentsAndTransition(depFragments, transition);
        List<BuildOptions> toOptions = transitionsMap.get(transitionKey);
        if (toOptions == null) {
            toOptions = getDynamicTransitionOptions(ctgOptions, transition, depFragments, ruleClassProvider, !sameFragments);
            transitionsMap.put(transitionKey, toOptions);
        }
        // configuration.
        if (sameFragments && toOptions.size() == 1 && Iterables.getOnlyElement(toOptions).equals(ctgOptions)) {
            putOnlyEntry(dynamicDeps, attributeAndLabel, Dependency.withConfigurationAndAspects(dep.getLabel(), ctgValue.getConfiguration(), dep.getAspects()));
            continue;
        }
        // If we get here, we have to get the configuration from Skyframe.
        for (BuildOptions options : toOptions) {
            keysToEntries.put(BuildConfigurationValue.key(depFragments, options), depsEntry);
        }
    }
    // Get all BuildConfigurations we need from Skyframe. While not every value might be available,
    // we don't call env.valuesMissing() here because that could be true from the earlier
    // resolver.dependentNodeMap call in computeDependencies, which also calls Skyframe. This method
    // doesn't need those missing values, but it still has to be called after
    // resolver.dependentNodeMap because it consumes that method's output. The reason the missing
    // values don't matter is because resolver.dependentNodeMap still returns "partial" results
    // and this method runs over whatever's available.
    //
    // While there would be no *correctness* harm in nulling out early, there's significant
    // *performance* harm. Profiling shows that putting "if (env.valuesMissing()) { return null; }"
    // here (or even after resolver.dependentNodeMap) produces a ~30% performance hit on the
    // analysis phase. That's because resolveConfiguredTargetDependencies and
    // resolveAspectDependencies don't get a chance to make their own Skyframe requests before
    // bailing out of this ConfiguredTargetFunction call. Ideally we could batch all requests
    // from all methods into a single Skyframe call, but there are enough subtle data flow
    // dependencies in ConfiguredTargetFucntion to make that impractical.
    Map<SkyKey, ValueOrException<InvalidConfigurationException>> depConfigValues = env.getValuesOrThrow(keysToEntries.keySet(), InvalidConfigurationException.class);
    // Now fill in the remaining unresolved deps with the now-resolved configurations.
    try {
        for (Map.Entry<SkyKey, ValueOrException<InvalidConfigurationException>> entry : depConfigValues.entrySet()) {
            SkyKey key = entry.getKey();
            ValueOrException<InvalidConfigurationException> valueOrException = entry.getValue();
            if (valueOrException.get() == null) {
                // null out on missing values from *this specific Skyframe request*.
                return null;
            }
            BuildConfigurationValue trimmedConfig = (BuildConfigurationValue) valueOrException.get();
            for (Map.Entry<Attribute, Dependency> info : keysToEntries.get(key)) {
                Dependency originalDep = info.getValue();
                AttributeAndLabel attr = new AttributeAndLabel(info.getKey(), originalDep.getLabel());
                Dependency resolvedDep = Dependency.withConfigurationAndAspects(originalDep.getLabel(), trimmedConfig.getConfiguration(), originalDep.getAspects());
                if (attr.attribute.hasSplitConfigurationTransition()) {
                    dynamicDeps.put(attr, resolvedDep);
                } else {
                    putOnlyEntry(dynamicDeps, attr, resolvedDep);
                }
            }
        }
    } catch (InvalidConfigurationException e) {
        throw new DependencyEvaluationException(e);
    }
    return sortDynamicallyConfiguredDeps(originalDeps, dynamicDeps, attributesAndLabels);
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SkyKey(com.google.devtools.build.skyframe.SkyKey) Dependency(com.google.devtools.build.lib.analysis.Dependency) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Nullable(javax.annotation.Nullable)

Example 64 with Attribute

use of com.google.devtools.build.lib.packages.Attribute 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 65 with Attribute

use of com.google.devtools.build.lib.packages.Attribute 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)

Aggregations

Attribute (com.google.devtools.build.lib.packages.Attribute)76 Test (org.junit.Test)37 Label (com.google.devtools.build.lib.cmdline.Label)20 Rule (com.google.devtools.build.lib.packages.Rule)20 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)7 Target (com.google.devtools.build.lib.packages.Target)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)6 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)6 RuleClass (com.google.devtools.build.lib.packages.RuleClass)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 LinkedHashSet (java.util.LinkedHashSet)6 Nullable (javax.annotation.Nullable)6 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)5 Map (java.util.Map)5 Dependency (com.google.devtools.build.lib.analysis.Dependency)4