Search in sources :

Example 36 with BuildConfiguration

use of com.google.devtools.build.lib.analysis.config.BuildConfiguration 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 37 with BuildConfiguration

use of com.google.devtools.build.lib.analysis.config.BuildConfiguration in project bazel by bazelbuild.

the class ConfigurationCollectionFunction method createConfiguration.

@Nullable
private BuildConfiguration createConfiguration(Cache<String, BuildConfiguration> cache, ExtendedEventHandler originalEventListener, PackageProviderForConfigurations loadedPackageProvider, BuildOptions buildOptions, String cpuOverride) throws InvalidConfigurationException, InterruptedException {
    ErrorSensingEventHandler eventHandler = new ErrorSensingEventHandler(originalEventListener);
    if (cpuOverride != null) {
        // TODO(bazel-team): Options classes should be immutable. This is a bit of a hack.
        buildOptions = buildOptions.clone();
        buildOptions.get(BuildConfiguration.Options.class).cpu = cpuOverride;
        buildOptions.get(BuildConfiguration.Options.class).experimentalMultiCpuDistinguisher = cpuOverride;
    }
    BuildConfiguration targetConfig = configurationFactory.get().createConfigurations(cache, loadedPackageProvider, buildOptions, eventHandler);
    if (targetConfig == null) {
        return null;
    }
    // --keep_going. If so, we throw an error here.
    if (eventHandler.hasErrors()) {
        throw new InvalidConfigurationException("Build options are invalid");
    }
    return targetConfig;
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ErrorSensingEventHandler(com.google.devtools.build.lib.events.ErrorSensingEventHandler) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) Nullable(javax.annotation.Nullable)

Example 38 with BuildConfiguration

use of com.google.devtools.build.lib.analysis.config.BuildConfiguration in project bazel by bazelbuild.

the class ConfigurationCollectionFunction method getConfigurations.

/** Create the build configurations with the given options. */
private BuildConfigurationCollection getConfigurations(Environment env, PackageProviderForConfigurations loadedPackageProvider, BuildOptions buildOptions, ImmutableSet<String> multiCpu) throws InvalidConfigurationException, InterruptedException {
    // We cache all the related configurations for this target configuration in a cache that is
    // dropped at the end of this method call. We instead rely on the cache for entire collections
    // for caching the target and related configurations, and on a dedicated host configuration
    // cache for the host configuration.
    Cache<String, BuildConfiguration> cache = CacheBuilder.newBuilder().<String, BuildConfiguration>build();
    List<BuildConfiguration> targetConfigurations = new ArrayList<>();
    if (!multiCpu.isEmpty()) {
        for (String cpu : multiCpu) {
            BuildConfiguration targetConfiguration = createConfiguration(cache, env.getListener(), loadedPackageProvider, buildOptions, cpu);
            if (targetConfiguration == null || targetConfigurations.contains(targetConfiguration)) {
                continue;
            }
            targetConfigurations.add(targetConfiguration);
        }
        if (loadedPackageProvider.valuesMissing()) {
            return null;
        }
    } else {
        BuildConfiguration targetConfiguration = createConfiguration(cache, env.getListener(), loadedPackageProvider, buildOptions, null);
        if (targetConfiguration == null) {
            return null;
        }
        targetConfigurations.add(targetConfiguration);
    }
    BuildConfiguration hostConfiguration = getHostConfiguration(env, targetConfigurations.get(0));
    if (hostConfiguration == null) {
        return null;
    }
    return new BuildConfigurationCollection(targetConfigurations, hostConfiguration);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ArrayList(java.util.ArrayList) BuildConfigurationCollection(com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection)

Example 39 with BuildConfiguration

use of com.google.devtools.build.lib.analysis.config.BuildConfiguration in project bazel by bazelbuild.

the class LicensesProviderImpl method of.

/**
   * Create the appropriate {@link LicensesProvider} for a rule based on its {@code RuleContext}
   */
public static LicensesProvider of(RuleContext ruleContext) {
    if (!ruleContext.getConfiguration().checkLicenses()) {
        return EMPTY;
    }
    NestedSetBuilder<TargetLicense> builder = NestedSetBuilder.linkOrder();
    BuildConfiguration configuration = ruleContext.getConfiguration();
    Rule rule = ruleContext.getRule();
    AttributeMap attributes = ruleContext.attributes();
    License toolOutputLicense = rule.getToolOutputLicense(attributes);
    TargetLicense outputLicenses = toolOutputLicense == null ? null : new TargetLicense(rule.getLabel(), toolOutputLicense);
    if (configuration.isHostConfiguration() && toolOutputLicense != null) {
        if (toolOutputLicense != License.NO_LICENSE) {
            builder.add(outputLicenses);
        }
    } else {
        if (rule.getLicense() != License.NO_LICENSE) {
            builder.add(new TargetLicense(rule.getLabel(), rule.getLicense()));
        }
        ListMultimap<String, ? extends TransitiveInfoCollection> configuredMap = ruleContext.getConfiguredTargetMap();
        for (String depAttrName : attributes.getAttributeNames()) {
            // Only add the transitive licenses for the attributes that do not have the output_licenses.
            Attribute attribute = attributes.getAttributeDefinition(depAttrName);
            for (TransitiveInfoCollection dep : configuredMap.get(depAttrName)) {
                LicensesProvider provider = dep.getProvider(LicensesProvider.class);
                if (provider == null) {
                    continue;
                }
                if (useOutputLicenses(attribute, configuration) && provider.hasOutputLicenses()) {
                    builder.add(provider.getOutputLicenses());
                } else {
                    builder.addTransitive(provider.getTransitiveLicenses());
                }
            }
        }
    }
    return new LicensesProviderImpl(builder.build(), outputLicenses);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Attribute(com.google.devtools.build.lib.packages.Attribute) License(com.google.devtools.build.lib.packages.License) Rule(com.google.devtools.build.lib.packages.Rule)

Example 40 with BuildConfiguration

use of com.google.devtools.build.lib.analysis.config.BuildConfiguration in project bazel by bazelbuild.

the class BuildView method getDirectPrerequisiteDependenciesForTesting.

@VisibleForTesting
public OrderedSetMultimap<Attribute, Dependency> getDirectPrerequisiteDependenciesForTesting(final ExtendedEventHandler eventHandler, final ConfiguredTarget ct, BuildConfigurationCollection configurations) throws EvalException, InvalidConfigurationException, InterruptedException, InconsistentAspectOrderException {
    if (!(ct.getTarget() instanceof Rule)) {
        return OrderedSetMultimap.create();
    }
    class SilentDependencyResolver extends DependencyResolver {

        @Override
        protected void invalidVisibilityReferenceHook(TargetAndConfiguration node, Label label) {
            throw new RuntimeException("bad visibility on " + label + " during testing unexpected");
        }

        @Override
        protected void invalidPackageGroupReferenceHook(TargetAndConfiguration node, Label label) {
            throw new RuntimeException("bad package group on " + label + " during testing unexpected");
        }

        @Override
        protected void missingEdgeHook(Target from, Label to, NoSuchThingException e) {
            throw new RuntimeException("missing dependency from " + from.getLabel() + " to " + to + ": " + e.getMessage(), e);
        }

        @Override
        protected Target getTarget(Target from, Label label, NestedSetBuilder<Label> rootCauses) throws InterruptedException {
            try {
                return skyframeExecutor.getPackageManager().getTarget(eventHandler, label);
            } catch (NoSuchThingException e) {
                throw new IllegalStateException(e);
            }
        }

        @Override
        protected List<BuildConfiguration> getConfigurations(Set<Class<? extends BuildConfiguration.Fragment>> fragments, Iterable<BuildOptions> buildOptions) {
            Preconditions.checkArgument(ct.getConfiguration().fragmentClasses().equals(fragments));
            Dependency asDep = Dependency.withTransitionAndAspects(ct.getLabel(), Attribute.ConfigurationTransition.NONE, AspectCollection.EMPTY);
            ImmutableList.Builder<BuildConfiguration> builder = ImmutableList.builder();
            for (BuildOptions options : buildOptions) {
                builder.add(Iterables.getOnlyElement(skyframeExecutor.getConfigurations(eventHandler, options, ImmutableList.<Dependency>of(asDep)).values()));
            }
            return builder.build();
        }
    }
    DependencyResolver dependencyResolver = new SilentDependencyResolver();
    TargetAndConfiguration ctgNode = new TargetAndConfiguration(ct.getTarget(), ct.getConfiguration());
    return dependencyResolver.dependentNodeMap(ctgNode, configurations.getHostConfiguration(), /*aspect=*/
    null, getConfigurableAttributeKeysForTesting(eventHandler, ctgNode));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NestedSet(com.google.devtools.build.lib.collect.nestedset.NestedSet) ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) Rule(com.google.devtools.build.lib.packages.Rule) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)51 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)13 Label (com.google.devtools.build.lib.cmdline.Label)12 Artifact (com.google.devtools.build.lib.actions.Artifact)11 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 Test (org.junit.Test)8 Attribute (com.google.devtools.build.lib.packages.Attribute)7 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)6 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)6 Rule (com.google.devtools.build.lib.packages.Rule)6 Target (com.google.devtools.build.lib.packages.Target)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Nullable (javax.annotation.Nullable)6 Root (com.google.devtools.build.lib.actions.Root)5 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)5 HashMap (java.util.HashMap)5