// 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.
   *
   * <p>This method should be considered only a heuristic, and should not be used during the
   * analysis phase.
   *
   * <p>(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<FileTarget> 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<String> 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<String> 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<FileTarget> getSrcAndHdrTargets(EventHandler eventHandler, Rule rule,
                                                     TargetProvider provider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException  {
    ImmutableSet<String> srcAndHdrAttributes = ImmutableSet.<String>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<FileTarget> 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<FileTarget> 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<FileTarget> getTargets(EventHandler eventHandler,
      Rule rule,
      ImmutableSet<String> attributes,
      Set<Label> visitedRuleLabels,
      TargetProvider targetProvider)
      throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
    List<Label> srcLabels = Lists.newArrayList();
    RawAttributeMapper attributeMapper = RawAttributeMapper.of(rule);
    for (String attrName : attributes) {
      if (rule.isAttrDefined(attrName, BuildType.LABEL_LIST)) {
        // Note that we simply flatten configurable attributes here, which is not the correct
        // behavior. However, it seems to be a good approximation of what is needed in most use
        // cases.
        srcLabels.addAll(attributeMapper.getMergedValues(attrName, BuildType.LABEL_LIST));
      } else if (rule.isAttrDefined(attrName, BuildType.LABEL)
          && !rule.isConfigurableAttribute(attrName)) {
        Label srcLabel = attributeMapper.get(attrName, BuildType.LABEL);
        if (srcLabel != null) {
          srcLabels.add(srcLabel);
        }
      }
    }
    if (srcLabels.isEmpty()) {
      return ImmutableList.of();
    }
    List<FileTarget> srcTargets = new ArrayList<>();
    for (Label label : srcLabels) {
      Target target = targetProvider.getTarget(eventHandler, label);
      if (target instanceof FileTarget) {
        srcTargets.add((FileTarget) target);
      } else {
        Rule srcRule = target.getAssociatedRule();
        if (srcRule != null && !visitedRuleLabels.contains(srcRule.getLabel())) {
          visitedRuleLabels.add(srcRule.getLabel());
          if ("filegroup".equals(srcRule.getRuleClass())) {
            srcTargets.addAll(getTargets(eventHandler, srcRule, FILEGROUP_ATTRIBUTES,
                visitedRuleLabels, targetProvider));
          } else {
            srcTargets.addAll(srcRule.getOutputFiles());
          }
        }
      }
    }
    return ImmutableList.copyOf(srcTargets);
  }
}

@@ -2955,1890 +2955,144 @@
  // 
-Attribute referring to %22headers%22.%0A  private static final String HEADER_ATTRIBUTE = %22hdrs%22;%0A%0A  // Attribute referring to %22textual headers%22.%0A  private static final String TEXTUAL_HEADER_ATTRIBUTE = %22textual_hdrs%22;%0A%0A  // The attribute to search in filegroups.%0A  private static final ImmutableSet%3CString%3E FILEGROUP_ATTRIBUTES =%0A      ImmutableSet.of(%22srcs%22);%0A%0A  /**%0A   * Same as %7B@link #getSrcTargets%7D, but for both source and headers (i.e. also traversing%0A   * the %22hdrs%22 attribute).%0A   */%0A  @ThreadSafety.ThreadSafe%0A  public static List%3CFileTarget%3E getSrcAndHdrTargets(EventHandler eventHandler, Rule rule,%0A                                                     TargetProvider provider)%0A      throws NoSuchTargetException, NoSuchPackageException, InterruptedException  %7B%0A    ImmutableSet%3CString%3E srcAndHdrAttributes = ImmutableSet.%3CString%3Ebuilder()%0A        .addAll(SOURCE_ATTRIBUTES)%0A        .add(HEADER_ATTRIBUTE)%0A        .add(TEXTUAL_HEADER_ATTRIBUTE)%0A        .build();%0A    return getTargets(eventHandler, rule, srcAndHdrAttributes, Sets.newHashSet(rule.getLabel()),%0A        provider);%0A  %7D%0A%0A  @ThreadSafety.ThreadSafe%0A  public static List%3CFileTarget%3E getHdrTargets(EventHandler eventHandler, Rule rule,%0A                                                     TargetProvider provider)%0A      throws NoSuchTargetException, NoSuchPackageException, InterruptedException  %7B%0A    return getTargets(%0A        eventHandler, rule, ImmutableSet.of(HEADER_ATTRIBUTE), Sets.newHashSet(rule.getLabel()),%0A        provider);%0A  %7D%0A%0A  @ThreadSafety.ThreadSafe%0A  public static List%3CFileTarget%3E getTextualHdrTargets(%0A      EventHandler eventHandler, Rule rule, TargetProvider provider)%0A      throws NoSuchTargetException, NoSuchPackageException, InterruptedException %7B%0A    return getTargets(eventHandler, rule, ImmutableSet.of(TEXTUAL_HEADER_ATTRIBUTE),%0A        Sets.newHashSet(rule.getLabel()), provider);%0A  %7D
+The attribute to search in filegroups.%0A  private static final ImmutableSet%3CString%3E FILEGROUP_ATTRIBUTES =%0A      ImmutableSet.of(%22srcs%22);
 %0A%0A  

Remove unused code.