// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.packages;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.CollectionUtils;
import com.google.devtools.build.lib.packages.BuildType.Selector;
import com.google.devtools.build.lib.packages.BuildType.SelectorList;
import com.google.devtools.build.lib.syntax.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
/**
* {@link AttributeMap} implementation that provides the ability to retrieve all possible
* values an attribute might take.
*/
public class AggregatingAttributeMapper extends AbstractAttributeMapper {
/**
* Store for all of this rule's attributes that are non-configurable. These are
* unconditionally available to computed defaults no matter what dependencies
* they've declared.
*/
private final List nonconfigurableAttributes;
private AggregatingAttributeMapper(Rule rule) {
super(rule.getPackage(), rule.getRuleClassObject(), rule.getLabel(),
rule.getAttributeContainer());
ImmutableList.Builder nonconfigurableAttributesBuilder = ImmutableList.builder();
for (Attribute attr : rule.getAttributes()) {
if (!attr.isConfigurable()) {
nonconfigurableAttributesBuilder.add(attr.getName());
}
}
nonconfigurableAttributes = nonconfigurableAttributesBuilder.build();
}
public static AggregatingAttributeMapper of(Rule rule) {
return new AggregatingAttributeMapper(rule);
}
/**
* Override that also visits the rule's configurable attribute keys (which are
* themselves labels).
*
*
Note that we directly parse the selectors rather than just calling {@link #visitAttribute}
* to iterate over all possible values. That's because {@link #visitAttribute} can grow
* exponentially with respect to the number of selects (e.g. if an attribute uses three selects
* with three conditions each, it can take nine possible values). So we want to avoid that code
* path whenever actual value iteration isn't specifically needed.
*/
@Override
protected void visitLabels(Attribute attribute, AcceptsLabelAttribute observer) {
visitLabels(attribute, true, observer);
}
private void visitLabels(Attribute attribute, boolean includeSelectKeys,
AcceptsLabelAttribute observer) {
Type> type = attribute.getType();
SelectorList> selectorList = getSelectorList(attribute.getName(), type);
if (selectorList == null) {
if (getComputedDefault(attribute.getName(), attribute.getType()) != null) {
// Computed defaults are a special pain: we have no choice but to iterate through their
// (computed) values and look for labels.
for (Object value : visitAttribute(attribute.getName(), attribute.getType())) {
if (value != null) {
for (Label label : extractLabels(type, value)) {
observer.acceptLabelAttribute(label, attribute);
}
}
}
} else {
super.visitLabels(attribute, observer);
}
} else {
for (Selector> selector : selectorList.getSelectors()) {
for (Map.Entry