// 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.analysis; import com.google.common.base.Objects; import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.actions.ActionKeyContext; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.packages.PackageSpecification.PackageGroupContents; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.skyframe.BuildConfigurationValue; import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; import java.util.List; import java.util.Set; import javax.annotation.Nullable; /** * A helper class for building {@link ConfiguredTarget} instances, in particular for non-rule ones. * For {@link RuleConfiguredTarget} instances, use {@link RuleContext} instead, * which is a subclass of this class. * *

The class is intended to be sub-classed by RuleContext, in order to share the code. However, * it's not intended for sub-classing beyond that, and the constructor is intentionally package * private to enforce that. */ public class TargetContext { private final AnalysisEnvironment env; private final Target target; private final BuildConfiguration configuration; /** * This list only contains prerequisites that are not declared in rule attributes, with the * exception of visibility (i.e., visibility is represented here, even though it is a rule * attribute in case of a rule). Rule attributes are handled by the {@link RuleContext} subclass. */ private final List directPrerequisites; private final NestedSet visibility; /** * The constructor is intentionally package private. * *

directPrerequisites is expected to be ordered. */ TargetContext( AnalysisEnvironment env, Target target, BuildConfiguration configuration, Set directPrerequisites, NestedSet visibility) { this.env = env; this.target = target; this.configuration = configuration; this.directPrerequisites = ImmutableList.copyOf(directPrerequisites); this.visibility = visibility; } public AnalysisEnvironment getAnalysisEnvironment() { return env; } public ActionKeyContext getActionKeyContext() { return env.getActionKeyContext(); } public Target getTarget() { return target; } public Label getLabel() { return target.getLabel(); } /** * Returns the configuration for this target. This may return null if the target is supposed to be * configuration-independent (like an input file, or a visibility rule). However, this is * guaranteed to be non-null for rules and for output files. */ @Nullable public BuildConfiguration getConfiguration() { return configuration; } public BuildConfigurationValue.Key getConfigurationKey() { return BuildConfigurationValue.key(configuration); } public NestedSet getVisibility() { return visibility; } /** * Returns the prerequisite with the given label and configuration. Throws a RuntimeException if * no such prerequisite exists. */ TransitiveInfoCollection findDirectPrerequisite(Label label, BuildConfiguration config) { return Verify.verifyNotNull(maybeFindDirectPrerequisite(label, config), "Could not find prerequisite %s in the expected configuration", label); } /** * Returns the prerequisite with the given label and configuration, or null if no such * prerequisite exists. */ public TransitiveInfoCollection maybeFindDirectPrerequisite(Label label, BuildConfiguration config) { for (ConfiguredTargetAndData prerequisite : directPrerequisites) { if (prerequisite.getTarget().getLabel().equals(label) && (Objects.equal(prerequisite.getConfiguration(), config))) { return prerequisite.getConfiguredTarget(); } } return null; } }