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);
}
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;
}
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);
}
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);
}
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));
}
Aggregations