// 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.skyframe; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment; import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory; import com.google.devtools.build.lib.analysis.config.FragmentOptions; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.packages.AspectDefinition; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy; import com.google.devtools.build.lib.packages.DependencyFilter; import com.google.devtools.build.lib.packages.NoSuchPackageException; import com.google.devtools.build.lib.packages.NoSuchTargetException; import com.google.devtools.build.lib.packages.NoSuchThingException; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.RuleClassProvider; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.packages.TargetUtils; import com.google.devtools.build.lib.skyframe.TransitiveTargetFunction.TransitiveTargetValueBuilder; import com.google.devtools.build.skyframe.SkyKey; import com.google.devtools.build.skyframe.SkyValue; import com.google.devtools.build.skyframe.ValueOrException2; import com.google.devtools.common.options.Option; import java.lang.reflect.Field; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import javax.annotation.Nullable; /** * This class builds transitive Target values such that evaluating a Target value is similar to * running it through the LabelVisitor. */ public class TransitiveTargetFunction extends TransitiveBaseTraversalFunction { private final ConfiguredRuleClassProvider ruleClassProvider; /** * Maps build option names to matching config fragments. This is used to determine correct * fragment requirements for config_setting rules, which are unique in that their dependencies * are triggered by string representations of option names. */ private final Map> optionsToFragmentMap; TransitiveTargetFunction(RuleClassProvider ruleClassProvider) { this.ruleClassProvider = (ConfiguredRuleClassProvider) ruleClassProvider; this.optionsToFragmentMap = computeOptionsToFragmentMap(this.ruleClassProvider); } /** * Computes the option name --> config fragments map. Note that this mapping is technically * one-to-many: a single option may be required by multiple fragments (e.g. Java options are * used by both JavaConfiguration and Jvm). In such cases, we arbitrarily choose one fragment * since that's all that's needed to satisfy the config_setting. */ private static Map> computeOptionsToFragmentMap( ConfiguredRuleClassProvider ruleClassProvider) { Map> result = new LinkedHashMap<>(); Map, Integer> visitedOptionsClasses = new HashMap<>(); for (ConfigurationFragmentFactory factory : ruleClassProvider.getConfigurationFragments()) { Set> requiredOpts = factory.requiredOptions(); for (Class optionsClass : requiredOpts) { Integer previousBest = visitedOptionsClasses.get(optionsClass); if (previousBest != null && previousBest <= requiredOpts.size()) { // Multiple config fragments may require the same options class, but we only need one of // them to guarantee that class makes it into the configuration. Pick one that depends // on as few options classes as possible (not necessarily unique). continue; } visitedOptionsClasses.put(optionsClass, requiredOpts.size()); for (Field field : optionsClass.getFields()) { if (field.isAnnotationPresent(Option.class)) { result.put(field.getAnnotation(Option.class).name(), factory.creates()); } } } } return result; } @Override Label argumentFromKey(SkyKey key) { return ((TransitiveTargetKey) key).getLabel(); } @Override SkyKey getKey(Label label) { return TransitiveTargetKey.of(label); } @Override TransitiveTargetValueBuilder processTarget(Label label, TargetAndErrorIfAny targetAndErrorIfAny) { Target target = targetAndErrorIfAny.getTarget(); boolean packageLoadedSuccessfully = targetAndErrorIfAny.isPackageLoadedSuccessfully(); return new TransitiveTargetValueBuilder(label, target, packageLoadedSuccessfully); } @Override void processDeps( TransitiveTargetValueBuilder builder, EventHandler eventHandler, TargetAndErrorIfAny targetAndErrorIfAny, Iterable>> depEntries) throws InterruptedException { boolean successfulTransitiveLoading = builder.isSuccessfulTransitiveLoading(); Target target = targetAndErrorIfAny.getTarget(); NestedSetBuilder