// 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.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.base.Predicates; import com.google.common.base.Verify; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; import com.google.devtools.build.lib.cmdline.Label; 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.EvalException; import com.google.devtools.build.lib.syntax.Type; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * {@link AttributeMap} implementation that binds a rule's attribute as follows: * *
    *
  1. If the attribute is selectable (i.e. its BUILD declaration is of the form * "attr = { config1: "value1", "config2: "value2", ... }", returns the subset of values * chosen by the current configuration in accordance with Bazel's documented policy on * configurable attribute selection. *
  2. If the attribute is not selectable (i.e. its value is static), returns that value with * no additional processing. *
* *

Example usage: *

 *   Label fooLabel = ConfiguredAttributeMapper.of(ruleConfiguredTarget).get("foo", Type.LABEL);
 * 
*/ public class ConfiguredAttributeMapper extends AbstractAttributeMapper { private final Map configConditions; private Rule rule; private ConfiguredAttributeMapper(Rule rule, ImmutableMap configConditions) { super(Preconditions.checkNotNull(rule).getPackage(), rule.getRuleClassObject(), rule.getLabel(), rule.getAttributeContainer()); this.configConditions = configConditions; this.rule = rule; } /** * "Manual" constructor that requires the caller to pass the set of configurability conditions * that trigger this rule's configurable attributes. * *

If you don't know how to do this, you really want to use one of the "do-it-all" * constructors. */ @VisibleForTesting public static ConfiguredAttributeMapper of( Rule rule, ImmutableMap configConditions) { return new ConfiguredAttributeMapper(rule, configConditions); } /** * Checks that all attributes can be mapped to their configured values. This is * useful for checking that the configuration space in a configured attribute doesn't * contain unresolvable contradictions. * * @throws EvalException if any attribute's value can't be resolved under this mapper */ public void validateAttributes() throws EvalException { for (String attrName : getAttributeNames()) { getAndValidate(attrName, getAttributeType(attrName)); } } /** * Variation of {@link #get} that throws an informative exception if the attribute * can't be resolved due to intrinsic contradictions in the configuration. */ private T getAndValidate(String attributeName, Type type) throws EvalException { SelectorList selectorList = getSelectorList(attributeName, type); if (selectorList == null) { // This is a normal attribute. return super.get(attributeName, type); } List resolvedList = new ArrayList<>(); for (Selector selector : selectorList.getSelectors()) { ConfigKeyAndValue resolvedPath = resolveSelector(attributeName, selector); if (!selector.isValueSet(resolvedPath.configKey)) { // Use the default. We don't have access to the rule here, so pass null to // Attribute.getValue(). This has the result of making attributes with condition // predicates ineligible for "None" values. But no user-facing attributes should // do that anyway, so that isn't a loss. Attribute attr = getAttributeDefinition(attributeName); Verify.verify(attr.getCondition() == Predicates.alwaysTrue()); resolvedList.add((T) attr.getDefaultValue(null)); } else { resolvedList.add(resolvedPath.value); } } return resolvedList.size() == 1 ? resolvedList.get(0) : type.concat(resolvedList); } private static class ConfigKeyAndValue { Label configKey; T value; ConfigKeyAndValue(Label key, T value) { this.configKey = key; this.value = value; } } private ConfigKeyAndValue resolveSelector(String attributeName, Selector selector) throws EvalException { Map> matchingConditions = new LinkedHashMap<>(); Set