<commit_before>// Copyright 2016 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.rules.objc;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.Builder;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.ValueSequence;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.VariablesExtension;
import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider;
import com.google.devtools.build.lib.rules.cpp.CppRuleClasses;
import com.google.devtools.build.lib.rules.cpp.PrecompiledFiles;

import java.util.Collection;

/**
 * Implementation for experimental_objc_library.
 */
public class ExperimentalObjcLibrary implements RuleConfiguredTargetFactory {

  private static final String PCH_FILE_VARIABLE_NAME = "pch_file";
  private static final String FRAMEWORKS_VARIABLE_NAME = "framework_paths";
  private static final String MODULES_MAPS_DIR_NAME = "module_maps_dir";
  private static final String OBJC_MODULE_CACHE_DIR_NAME = "_objc_module_cache";
  private static final String OBJC_MODULE_CACHE_KEY = "modules_cache_path";
  private static final String OBJC_MODULE_FEATURE_NAME = "use_objc_modules";
  private static final Iterable<String> ACTIVATED_ACTIONS =
      ImmutableList.of("objc-compile", "objc++-compile");

  /**
   * Build variable extensions for templating a toolchain for objc builds.
   */
  private static class ObjcVariablesExtension implements VariablesExtension {

    private final RuleContext ruleContext;
    private final ObjcProvider objcProvider;

    public ObjcVariablesExtension(RuleContext ruleContext, ObjcProvider objcProvider) {
      this.ruleContext = ruleContext;
      this.objcProvider = objcProvider;
    }

    @Override
    public void addVariables(Builder builder) {
      addPchVariables(builder);
      addFrameworkVariables(builder);
      if (ObjcCommon.shouldUseObjcModules(ruleContext)) {
        addModuleMapVariables(builder);
      }
    }

    private void addPchVariables(Builder builder) {
       if (ruleContext.getPrerequisiteArtifact("pch", Mode.TARGET) != null) {
        builder.addVariable(
            PCH_FILE_VARIABLE_NAME,
            ruleContext.getPrerequisiteArtifact("pch", Mode.TARGET).getExecPathString());
      }
    }

    private void addFrameworkVariables(Builder builder) {
      ValueSequence.Builder frameworkSequence = new ValueSequence.Builder();
      AppleConfiguration appleConfig = ruleContext.getFragment(AppleConfiguration.class);
      for (String framework :
          CompilationSupport.commonFrameworkNames(objcProvider, appleConfig)) {
        frameworkSequence.addValue(framework);
      }
      builder.addSequence(FRAMEWORKS_VARIABLE_NAME, frameworkSequence.build());
    }

    private void addModuleMapVariables(Builder builder) {
      builder.addVariable(
          MODULES_MAPS_DIR_NAME,
          ObjcRuleClasses.intermediateArtifacts(ruleContext)
              .moduleMap()
              .getArtifact()
              .getExecPath()
              .getParentDirectory()
              .toString());
      builder.addVariable(
          OBJC_MODULE_CACHE_KEY,
          ruleContext.getConfiguration().getGenfilesFragment() + "/" + OBJC_MODULE_CACHE_DIR_NAME);
    }
  }

  @Override
  public ConfiguredTarget create(RuleContext ruleContext)
      throws InterruptedException, RuleErrorException {
    validateAttributes(ruleContext);

    CompilationArtifacts compilationArtifacts =
        CompilationSupport.compilationArtifacts(ruleContext);
    CompilationAttributes compilationAttributes =
        CompilationAttributes.Builder.fromRuleContext(ruleContext).build();
    PrecompiledFiles precompiledFiles = new PrecompiledFiles(ruleContext);

    ObjcCommon common = common(ruleContext, compilationAttributes, compilationArtifacts);

    Collection<Artifact> arcSources = Sets.newHashSet(compilationArtifacts.getSrcs());
    Collection<Artifact> nonArcSources = Sets.newHashSet(compilationArtifacts.getNonArcSrcs());
    Collection<Artifact> privateHdrs = Sets.newHashSet(compilationArtifacts.getPrivateHdrs());
    Collection<Artifact> publicHdrs = Sets.newHashSet(compilationAttributes.hdrs());

    CcLibraryHelper helper =
        new CcLibraryHelper(
                ruleContext,
                new ObjcCppSemantics(common.getObjcProvider()),
                getFeatureConfiguration(ruleContext),
                CcLibraryHelper.SourceCategory.CC_AND_OBJC)
            .addSources(arcSources, ImmutableMap.of("objc_arc", ""))<commit_msg>Add platform-specific common compile and link flags to experimental_objc_library.<commit_after>// Copyright 2016 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.rules.objc;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.Platform;
import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.Builder;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.ValueSequence;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.VariablesExtension;
import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider;
import com.google.devtools.build.lib.rules.cpp.CppRuleClasses;
import com.google.devtools.build.lib.rules.cpp.PrecompiledFiles;

import java.util.Collection;

/**
 * Implementation for experimental_objc_library.
 */
public class ExperimentalObjcLibrary implements RuleConfiguredTargetFactory {

  private static final String PCH_FILE_VARIABLE_NAME = "pch_file";
  private static final String FRAMEWORKS_VARIABLE_NAME = "framework_paths";
  private static final String VERSION_MIN_VARIABLE_NAME = "version_min";
  private static final String MODULES_MAPS_DIR_NAME = "module_maps_dir";
  private static final String OBJC_MODULE_CACHE_DIR_NAME = "_objc_module_cache";
  private static final String OBJC_MODULE_CACHE_KEY = "modules_cache_path";
  private static final String OBJC_MODULE_FEATURE_NAME = "use_objc_modules";
  private static final Iterable<String> ACTIVATED_ACTIONS =
      ImmutableList.of("objc-compile", "objc++-compile");

  /**
   * Build variable extensions for templating a toolchain for objc builds.
   */
  private static class ObjcVariablesExtension implements VariablesExtension {

    private final RuleContext ruleContext;
    private final ObjcProvider objcProvider;
    
    private final AppleConfiguration appleConfiguration;
    private final ObjcConfiguration objcConfiguration;

    public ObjcVariablesExtension(RuleContext ruleContext, ObjcProvider objcProvider) {
      this.ruleContext = ruleContext;
      this.objcProvider = objcProvider;
      
      this.appleConfiguration = ruleContext.getFragment(AppleConfiguration.class);
      this.objcConfiguration = ruleContext.getFragment(ObjcConfiguration.class);
    }

    @Override
    public void addVariables(Builder builder) {
      addPchVariables(builder);
      addFrameworkVariables(builder);
      addArchVariables(builder);
      if (ObjcCommon.shouldUseObjcModules(ruleContext)) {
        addModuleMapVariables(builder);
      }
    }

    private void addPchVariables(Builder builder) {
       if (ruleContext.getPrerequisiteArtifact("pch", Mode.TARGET) != null) {
        builder.addVariable(
            PCH_FILE_VARIABLE_NAME,
            ruleContext.getPrerequisiteArtifact("pch", Mode.TARGET).getExecPathString());
      }
    }

    private void addFrameworkVariables(Builder builder) {
      ValueSequence.Builder frameworkSequence = new ValueSequence.Builder();
      AppleConfiguration appleConfig = ruleContext.getFragment(AppleConfiguration.class);
      for (String framework :
          CompilationSupport.commonFrameworkNames(objcProvider, appleConfig)) {
        frameworkSequence.addValue(framework);
      }
      builder.addSequence(FRAMEWORKS_VARIABLE_NAME, frameworkSequence.build());
    }

    private void addModuleMapVariables(Builder builder) {
      builder.addVariable(
          MODULES_MAPS_DIR_NAME,
          ObjcRuleClasses.intermediateArtifacts(ruleContext)
              .moduleMap()
              .getArtifact()
              .getExecPath()
              .getParentDirectory()
              .toString());
      builder.addVariable(
          OBJC_MODULE_CACHE_KEY,
          ruleContext.getConfiguration().getGenfilesFragment() + "/" + OBJC_MODULE_CACHE_DIR_NAME);
    }
    
    private void addArchVariables(Builder builder) {
      Platform platform = appleConfiguration.getSingleArchPlatform();
      switch (platform.getType()) {
        case IOS:
          builder.addVariable(
              VERSION_MIN_VARIABLE_NAME, objcConfiguration.getMinimumOs().toString());
          break;
        case WATCHOS:
          builder.addVariable(
              VERSION_MIN_VARIABLE_NAME,
              appleConfiguration.getSdkVersionForPlatform(platform).toString());
          break;
        default: // don't handle MACOS and TVOS
          throw new IllegalArgumentException("Unhandled platform: " + platform);
      }
    }
  }

  @Override
  public ConfiguredTarget create(RuleContext ruleContext)
      throws InterruptedException, RuleErrorException {
    validateAttributes(ruleContext);

    CompilationArtifacts compilationArtifacts =
        CompilationSupport.compilationArtifacts(ruleContext);
    CompilationAttributes compilationAttributes =
        CompilationAttributes.Builder.fromRuleContext(ruleContext).build();
    PrecompiledFiles precompiledFiles = new PrecompiledFiles(ruleContext);

    ObjcCommon common = common(ruleContext, compilationAttributes, compilationArtifacts);

    Collection<Artifact> arcSources = Sets.newHashSet(compilationArtifacts.getSrcs());
    Collection<Artifact> nonArcSources = Sets.newHashSet(compilationArtifacts.getNonArcSrcs());
    Collection<Artifact> privateHdrs = Sets.newHashSet(compilationArtifacts.getPrivateHdrs());
    Collection<Artifact> publicHdrs = Sets.newHashSet(compilationAttributes.hdrs());

    CcLibraryHelper helper =
        new CcLibraryHelper(
                ruleContext,
                new ObjcCppSemantics(common.getObjcProvider()),
                getFeatureConfiguration(ruleContext),
                CcLibraryHelper.SourceCategory.CC_AND_OBJC)
            .addSources(arcSources, ImmutableMap.of("objc_arc", ""))
{'fraction_non_alphanumeric': 0.0849283223556761, 'fraction_numerical': 0.0012398295234405268, 'mean_word_length': 4.131610337972167, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2019539', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3438, 'n_words': 691}