<commit_before>/*
 * Copyright 2015-present Facebook, Inc.
 *
 * 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.facebook.buck.cxx;

import com.facebook.buck.log.Logger;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.BuildTargets;
import com.facebook.buck.model.UnflavoredBuildTarget;
import com.facebook.buck.model.Flavor;
import com.facebook.buck.model.ImmutableFlavor;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.BuildTargetSourcePath;
import com.facebook.buck.rules.DependencyAggregation;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.facebook.buck.rules.SymlinkTree;
import com.facebook.buck.rules.coercer.FrameworkPath;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.util.immutables.BuckStyleImmutable;
import com.facebook.buck.util.immutables.BuckStyleTuple;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Suppliers;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;

import org.immutables.value.Value;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import javax.annotation.Nonnull;

@Value.Immutable
@BuckStyleImmutable
abstract class AbstractCxxSourceRuleFactory {

  private static final Logger LOG = Logger.get(AbstractCxxSourceRuleFactory.class);
  private static final String COMPILE_FLAVOR_PREFIX = "compile-";
  private static final Flavor AGGREGATED_PREPROCESS_DEPS_FLAVOR =
      ImmutableFlavor.of("preprocessor-deps");

  @Value.Parameter
  public abstract BuildRuleParams getParams();
  @Value.Parameter
  public abstract BuildRuleResolver getResolver();
  @Value.Parameter
  public abstract SourcePathResolver getPathResolver();
  @Value.Parameter
  public abstract SourcePathRuleFinder getRuleFinder();
  @Value.Parameter
  public abstract CxxBuckConfig getCxxBuckConfig();
  @Value.Parameter
  public abstract CxxPlatform getCxxPlatform();
  @Value.Parameter
  public abstract ImmutableList<CxxPreprocessorInput> getCxxPreprocessorInput();
  @Value.Parameter
  public abstract ImmutableMultimap<CxxSource.Type, String> getCompilerFlags();
  /** NOTE: {@code prefix_header} is incompatible with {@code precompiled_header}. */
  @Value.Parameter
  public abstract Optional<SourcePath> getPrefixHeader();
  /** NOTE: {@code precompiled_header} is incompatible with {@code prefix_header}. */
  @Value.Parameter
  public abstract Optional<SourcePath> getPrecompiledHeader();
  @Value.Parameter
  public abstract PicType getPicType();
  @Value.Parameter
  public abstract Optional<SymlinkTree> getSandboxTree();

  @Value.Check
  public void checkPrefixAndPrecompiledHeaderArgs() {
    if (getPrefixHeader().isPresent() && getPrecompiledHeader().isPresent()) {
      throw new HumanReadableException(
          "Cannot use `prefix_header` and `precompiled_header` in the same rule.");
    }
  }

  private ImmutableSortedSet<BuildRule> getPreprocessDeps() {
    ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
    for (CxxPreprocessorInput input : getCxxPreprocessorInput()) {
      builder.addAll(input.getDeps(getResolver(), getRuleFinder()));
    }
    if (getPrefixHeader().isPresent()) {
      builder.addAll(getRuleFinder().filterBuildRuleInputs(getPrefixHeader().get()));
    }
    if (getPrecompiledHeader().isPresent()) {
      builder.addAll(getRuleFinder().filterBuildRuleInputs(getPrecompiledHeader().get()));
    }
    if (getSandboxTree().isPresent()) {
      SymlinkTree tree = getSandboxTree().get();
      builder.add(tree);
      builder.addAll(getRuleFinder().filterBuildRuleInputs(tree.getLinks().values()));
    }
    return builder.build();
  }

  @Value.Lazy
  protected ImmutableSet<FrameworkPath> getFrameworks() {<commit_msg>Make Immutable accessors and methods protected instead of public in AbstractCxxSourceRuleFactory.<commit_after>/*
 * Copyright 2015-present Facebook, Inc.
 *
 * 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.facebook.buck.cxx;

import com.facebook.buck.log.Logger;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.BuildTargets;
import com.facebook.buck.model.UnflavoredBuildTarget;
import com.facebook.buck.model.Flavor;
import com.facebook.buck.model.ImmutableFlavor;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.BuildTargetSourcePath;
import com.facebook.buck.rules.DependencyAggregation;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.facebook.buck.rules.SymlinkTree;
import com.facebook.buck.rules.coercer.FrameworkPath;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.util.immutables.BuckStyleImmutable;
import com.facebook.buck.util.immutables.BuckStyleTuple;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Suppliers;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;

import org.immutables.value.Value;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import javax.annotation.Nonnull;

@Value.Immutable
@BuckStyleImmutable
abstract class AbstractCxxSourceRuleFactory {

  private static final Logger LOG = Logger.get(AbstractCxxSourceRuleFactory.class);
  private static final String COMPILE_FLAVOR_PREFIX = "compile-";
  private static final Flavor AGGREGATED_PREPROCESS_DEPS_FLAVOR =
      ImmutableFlavor.of("preprocessor-deps");

  @Value.Parameter
  protected abstract BuildRuleParams getParams();
  @Value.Parameter
  protected abstract BuildRuleResolver getResolver();
  @Value.Parameter
  protected abstract SourcePathResolver getPathResolver();
  @Value.Parameter
  protected abstract SourcePathRuleFinder getRuleFinder();
  @Value.Parameter
  protected abstract CxxBuckConfig getCxxBuckConfig();
  @Value.Parameter
  protected abstract CxxPlatform getCxxPlatform();
  @Value.Parameter
  protected abstract ImmutableList<CxxPreprocessorInput> getCxxPreprocessorInput();
  @Value.Parameter
  protected abstract ImmutableMultimap<CxxSource.Type, String> getCompilerFlags();
  /** NOTE: {@code prefix_header} is incompatible with {@code precompiled_header}. */
  @Value.Parameter
  protected abstract Optional<SourcePath> getPrefixHeader();
  /** NOTE: {@code precompiled_header} is incompatible with {@code prefix_header}. */
  @Value.Parameter
  protected abstract Optional<SourcePath> getPrecompiledHeader();
  @Value.Parameter
  protected abstract PicType getPicType();
  @Value.Parameter
  protected abstract Optional<SymlinkTree> getSandboxTree();

  @Value.Check
  protected void checkPrefixAndPrecompiledHeaderArgs() {
    if (getPrefixHeader().isPresent() && getPrecompiledHeader().isPresent()) {
      throw new HumanReadableException(
          "Cannot use `prefix_header` and `precompiled_header` in the same rule.");
    }
  }

  private ImmutableSortedSet<BuildRule> getPreprocessDeps() {
    ImmutableSortedSet.Builder<BuildRule> builder = ImmutableSortedSet.naturalOrder();
    for (CxxPreprocessorInput input : getCxxPreprocessorInput()) {
      builder.addAll(input.getDeps(getResolver(), getRuleFinder()));
    }
    if (getPrefixHeader().isPresent()) {
      builder.addAll(getRuleFinder().filterBuildRuleInputs(getPrefixHeader().get()));
    }
    if (getPrecompiledHeader().isPresent()) {
      builder.addAll(getRuleFinder().filterBuildRuleInputs(getPrecompiledHeader().get()));
    }
    if (getSandboxTree().isPresent()) {
      SymlinkTree tree = getSandboxTree().get();
      builder.add(tree);
      builder.addAll(getRuleFinder().filterBuildRuleInputs(tree.getLinks().values()));
    }
    return builder.build();
  }

  @Value.Lazy
  protected ImmutableSet<FrameworkPath> getFrameworks() {
{'fraction_non_alphanumeric': 0.10254685247477174, 'fraction_numerical': 0.001537722248918789, 'mean_word_length': 5.471393034825871, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5561420', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2933, 'n_words': 654}