// 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.pkgcache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.FileTarget;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.packages.NoSuchTargetException;
import com.google.devtools.build.lib.packages.RawAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* A helper class for getting source and header files from a given {@link Rule}.
*/
public final class SrcTargetUtil {
private SrcTargetUtil() {
}
/**
* Given a Rule, returns an immutable list of FileTarget for its sources, in the order they appear
* in its "srcs", "src" or "srcjar" attribute, and any filegroups or other rules it references.
* An empty list is returned if no "srcs" or "src" attribute exists for this rule. The list may
* contain OutputFiles if the sources were generated by another rule.
*
*
This method should be considered only a heuristic, and should not be used during the
* analysis phase.
*
*
(We could remove the throws clauses if we restrict the results to srcs within the same
* package.)
*
* @throws NoSuchTargetException or NoSuchPackageException when a source label cannot be resolved
* to a Target
*/
@ThreadSafety.ThreadSafe
public static List getSrcTargets(EventHandler eventHandler, Rule rule,
TargetProvider provider)
throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
return getTargets(eventHandler, rule, SOURCE_ATTRIBUTES, Sets.newHashSet(rule.getLabel()),
provider);
}
// Attributes referring to "sources".
private static final ImmutableSet SOURCE_ATTRIBUTES =
ImmutableSet.of("srcs", "src", "srcjar");
// Attribute referring to "headers".
private static final String HEADER_ATTRIBUTE = "hdrs";
// Attribute referring to "textual headers".
private static final String TEXTUAL_HEADER_ATTRIBUTE = "textual_hdrs";
// The attribute to search in filegroups.
private static final ImmutableSet FILEGROUP_ATTRIBUTES =
ImmutableSet.of("srcs");
/**
* Same as {@link #getSrcTargets}, but for both source and headers (i.e. also traversing
* the "hdrs" attribute).
*/
@ThreadSafety.ThreadSafe
public static List getSrcAndHdrTargets(EventHandler eventHandler, Rule rule,
TargetProvider provider)
throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
ImmutableSet srcAndHdrAttributes = ImmutableSet.builder()
.addAll(SOURCE_ATTRIBUTES)
.add(HEADER_ATTRIBUTE)
.add(TEXTUAL_HEADER_ATTRIBUTE)
.build();
return getTargets(eventHandler, rule, srcAndHdrAttributes, Sets.newHashSet(rule.getLabel()),
provider);
}
@ThreadSafety.ThreadSafe
public static List getHdrTargets(EventHandler eventHandler, Rule rule,
TargetProvider provider)
throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
return getTargets(
eventHandler, rule, ImmutableSet.of(HEADER_ATTRIBUTE), Sets.newHashSet(rule.getLabel()),
provider);
}
@ThreadSafety.ThreadSafe
public static List getTextualHdrTargets(
EventHandler eventHandler, Rule rule, TargetProvider provider)
throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
return getTargets(eventHandler, rule, ImmutableSet.of(TEXTUAL_HEADER_ATTRIBUTE),
Sets.newHashSet(rule.getLabel()), provider);
}
/**
* @see #getSrcTargets(EventHandler, Rule, TargetProvider)
*/
private static List getTargets(EventHandler eventHandler,
Rule rule,
ImmutableSet attributes,
Set