C++: Completely removes ctx from compile.

No instances of RuleContext left.

#4570

RELNOTES:none
PiperOrigin-RevId: 233590830
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
index 1777564..e34f134 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
@@ -1443,6 +1443,11 @@
   }
 
   @Override
+  public RuleErrorConsumer getRuleErrorConsumer() {
+    return this;
+  }
+
+  @Override
   public String toString() {
     return "RuleContext(" + getLabel() + ", " + getConfiguration() + ")";
   }
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java
index ff54a8f..f68abcf 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/ActionConstructionContext.java
@@ -22,6 +22,7 @@
 import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
 import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
 import com.google.devtools.build.lib.packages.ImplicitOutputsFunction;
+import com.google.devtools.build.lib.packages.RuleErrorConsumer;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import javax.annotation.Nullable;
 
@@ -156,4 +157,10 @@
   /** Returns the {@link PlatformInfo} describing the execution platform this action should use. */
   @Nullable
   PlatformInfo getExecutionPlatform();
+
+  /**
+   * Returns the {@link com.google.devtools.build.lib.packages.RuleErrorConsumer} for reporting rule
+   * errors.
+   */
+  RuleErrorConsumer getRuleErrorConsumer();
 }
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java
index 4e7abde..f6d58e4 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java
@@ -16,6 +16,7 @@
 
 import com.google.devtools.build.lib.actions.Artifact;
 import com.google.devtools.build.lib.analysis.skylark.SkylarkRuleContext;
+import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.rules.cpp.CcCompilationContext;
 import com.google.devtools.build.lib.rules.cpp.CcCompilationHelper.CompilationInfo;
 import com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs;
@@ -62,7 +63,8 @@
       SkylarkList<Artifact> headers,
       Object skylarkIncludes,
       Object skylarkCopts,
-      SkylarkList<CcCompilationContext> ccCompilationContexts)
+      SkylarkList<CcCompilationContext> ccCompilationContexts,
+      Location location)
       throws EvalException, InterruptedException {
     return BazelCcModule.compile(
         BazelCppSemantics.INSTANCE,
@@ -78,7 +80,8 @@
         /* skylarkAdditionalCompilationInputs= */ Runtime.NONE,
         /* skylarkAdditionalIncludeScanningRoots= */ Runtime.NONE,
         ccCompilationContexts,
-        /* purpose= */ Runtime.NONE);
+        /* purpose= */ Runtime.NONE,
+        location);
   }
 
   @Override
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
index 355c6a6..cfd7054 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
@@ -318,7 +318,14 @@
     }
     CcCompilationHelper compilationHelper =
         new CcCompilationHelper(
-                ruleContext, semantics, featureConfiguration, ccToolchain, fdoContext)
+                ruleContext,
+                ruleContext,
+                ruleContext.getLabel(),
+                CppHelper.getGrepIncludes(ruleContext),
+                semantics,
+                featureConfiguration,
+                ccToolchain,
+                fdoContext)
             .fromCommon(common, /* additionalCopts= */ ImmutableList.of())
             .addPrivateHeaders(common.getPrivateHeaders())
             .addSources(common.getSources())
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
index 18a000e..e468836 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
@@ -269,94 +269,61 @@
   private final ActionRegistry actionRegistry;
   private final ActionConstructionContext actionConstructionContext;
   private final Label label;
-  private final Artifact grepIncludes;
+  @Nullable private final Artifact grepIncludes;
 
-  /**
-   * Creates a CcCompilationHelper.
-   *
-   * @param ruleContext the RuleContext for the rule being built
-   * @param semantics CppSemantics for the build
-   * @param featureConfiguration activated features and action configs for the build
-   * @param sourceCatagory the candidate source types for the build
-   * @param ccToolchain the C++ toolchain provider for the build
-   * @param fdoContext the C++ FDO optimization support provider for the build
-   */
+  /** Creates a CcCompilationHelper that outputs artifacts in a given configuration. */
   public CcCompilationHelper(
-      RuleContext ruleContext,
+      ActionRegistry actionRegistry,
+      ActionConstructionContext actionConstructionContext,
+      Label label,
+      @Nullable Artifact grepIncludes,
       CppSemantics semantics,
       FeatureConfiguration featureConfiguration,
-      SourceCategory sourceCatagory,
-      CcToolchainProvider ccToolchain,
-      FdoContext fdoContext) {
-    this(
-        ruleContext,
-        semantics,
-        featureConfiguration,
-        sourceCatagory,
-        ccToolchain,
-        fdoContext,
-        ruleContext.getConfiguration());
-  }
-
-  /**
-   * Creates a CcCompilationHelper that outputs artifacts in a given configuration.
-   *
-   * @param ruleContext the RuleContext for the rule being built
-   * @param semantics CppSemantics for the build
-   * @param featureConfiguration activated features and action configs for the build
-   * @param sourceCatagory the candidate source types for the build
-   * @param ccToolchain the C++ toolchain provider for the build
-   * @param fdoContext the C++ FDO optimization support provider for the build
-   * @param configuration the configuration that gives the directory of output artifacts
-   */
-  public CcCompilationHelper(
-      RuleContext ruleContext,
-      CppSemantics semantics,
-      FeatureConfiguration featureConfiguration,
-      SourceCategory sourceCatagory,
+      SourceCategory sourceCategory,
       CcToolchainProvider ccToolchain,
       FdoContext fdoContext,
-      BuildConfiguration configuration) {
+      BuildConfiguration buildConfiguration) {
     this.semantics = Preconditions.checkNotNull(semantics);
     this.featureConfiguration = Preconditions.checkNotNull(featureConfiguration);
-    this.sourceCategory = Preconditions.checkNotNull(sourceCatagory);
+    this.sourceCategory = Preconditions.checkNotNull(sourceCategory);
     this.ccToolchain = Preconditions.checkNotNull(ccToolchain);
     this.fdoContext = Preconditions.checkNotNull(fdoContext);
-    this.configuration = Preconditions.checkNotNull(configuration);
-    this.cppConfiguration =
-        Preconditions.checkNotNull(ruleContext.getFragment(CppConfiguration.class));
+    this.actionConstructionContext = Preconditions.checkNotNull(actionConstructionContext);
+    this.configuration = buildConfiguration;
+    this.cppConfiguration = configuration.getFragment(CppConfiguration.class);
     setGenerateNoPicAction(
         !ccToolchain.usePicForDynamicLibraries(featureConfiguration)
             || !CppHelper.usePicForBinaries(ccToolchain, featureConfiguration));
     setGeneratePicAction(
         ccToolchain.usePicForDynamicLibraries(featureConfiguration)
             || CppHelper.usePicForBinaries(ccToolchain, featureConfiguration));
-    ruleErrorConsumer = ruleContext;
-    actionRegistry = ruleContext;
-    actionConstructionContext = ruleContext;
-    label = ruleContext.getLabel();
-    grepIncludes =
-        ruleContext.attributes().has("$grep_includes")
-            ? ruleContext.getPrerequisiteArtifact("$grep_includes", Mode.HOST)
-            : null;
+    this.ruleErrorConsumer = actionConstructionContext.getRuleErrorConsumer();
+    this.actionRegistry = Preconditions.checkNotNull(actionRegistry);
+    this.label = Preconditions.checkNotNull(label);
+    this.grepIncludes = grepIncludes;
   }
 
-  /**
-   * Creates a CcCompilationHelper for cpp source files.
-   *
-   * @param ruleContext the RuleContext for the rule being built
-   * @param semantics CppSemantics for the build
-   * @param featureConfiguration activated features and action configs for the build
-   * @param ccToolchain the C++ toolchain provider for the build
-   * @param fdoContext the C++ FDO optimization support provider for the build
-   */
+  /** Creates a CcCompilationHelper for cpp source files. */
   public CcCompilationHelper(
-      RuleContext ruleContext,
+      ActionRegistry actionRegistry,
+      ActionConstructionContext actionConstructionContext,
+      Label label,
+      @Nullable Artifact grepIncludes,
       CppSemantics semantics,
       FeatureConfiguration featureConfiguration,
       CcToolchainProvider ccToolchain,
       FdoContext fdoContext) {
-    this(ruleContext, semantics, featureConfiguration, SourceCategory.CC, ccToolchain, fdoContext);
+    this(
+        actionRegistry,
+        actionConstructionContext,
+        label,
+        grepIncludes,
+        semantics,
+        featureConfiguration,
+        SourceCategory.CC,
+        ccToolchain,
+        fdoContext,
+        actionConstructionContext.getConfiguration());
   }
 
   /** Sets fields that overlap for cc_library and cc_binary rules. */
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java
index 082ce9f..06f601a 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java
@@ -160,6 +160,9 @@
     CompilationInfo compilationInfo =
         new CcCompilationHelper(
                 ruleContext,
+                ruleContext,
+                ruleContext.getLabel(),
+                CppHelper.getGrepIncludes(ruleContext),
                 semantics,
                 featureConfiguration,
                 ccToolchain,
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java
index e8d387f..93d170e 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java
@@ -144,7 +144,14 @@
     }
     CcCompilationHelper compilationHelper =
         new CcCompilationHelper(
-                ruleContext, semantics, featureConfiguration, ccToolchain, fdoContext)
+                ruleContext,
+                ruleContext,
+                ruleContext.getLabel(),
+                CppHelper.getGrepIncludes(ruleContext),
+                semantics,
+                featureConfiguration,
+                ccToolchain,
+                fdoContext)
             .fromCommon(common, additionalCopts)
             .addSources(common.getSources())
             .addPrivateHeaders(common.getPrivateHeaders())
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
index 21eb6f2..5de1bae 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
@@ -24,6 +24,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.devtools.build.lib.actions.Artifact;
 import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
 import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
 import com.google.devtools.build.lib.analysis.skylark.BazelStarlarkContext;
 import com.google.devtools.build.lib.analysis.skylark.SkylarkActionFactory;
@@ -583,10 +584,12 @@
       Object skylarkAdditionalCompilationInputs,
       Object skylarkAdditionalIncludeScanningRoots,
       SkylarkList<CcCompilationContext> ccCompilationContexts,
-      Object purpose)
+      Object purpose,
+      Location location)
       throws EvalException, InterruptedException {
     CcCommon.checkRuleWhitelisted(skylarkRuleContext);
     RuleContext ruleContext = skylarkRuleContext.getRuleContext();
+    SkylarkActionFactory actions = skylarkRuleContext.actions();
     CcToolchainProvider ccToolchainProvider = convertFromNoneable(skylarkCcToolchainProvider, null);
     FeatureConfiguration featureConfiguration =
         convertFromNoneable(skylarkFeatureConfiguration, null);
@@ -599,10 +602,14 @@
     List<String> includeDirs = convertSkylarkListOrNestedSetToList(skylarkIncludes, String.class);
     CcCompilationHelper helper =
         new CcCompilationHelper(
-                ruleContext,
+                actions.asActionRegistry(location, actions),
+                actions.getActionConstructionContext(),
+                ruleContext.getLabel(),
+                /* grepIncludes= */ ruleContext.attributes().has("$grep_includes")
+                    ? ruleContext.getPrerequisiteArtifact("$grep_includes", Mode.HOST)
+                    : null,
                 cppSemantics,
                 featureConfiguration,
-                CcCompilationHelper.SourceCategory.CC,
                 ccToolchainProvider,
                 fdoContext)
             .addPublicHeaders(headers)
@@ -639,7 +646,6 @@
       helper.setCopts(copts.getSet(String.class));
     }
 
-    Location location = ruleContext.getRule().getLocation();
     RegisterActions generateNoPicOption =
         RegisterActions.fromString(generateNoPicOutputs, location, "generate_no_pic_outputs");
     if (!generateNoPicOption.equals(RegisterActions.CONDITIONALLY)) {
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java
index 7b5ce08..5e601e6 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java
@@ -890,4 +890,10 @@
         .map(CcInfo::getCcCompilationContext)
         .collect(ImmutableList.toImmutableList());
   }
+
+  public static Artifact getGrepIncludes(RuleContext ruleContext) {
+    return ruleContext.attributes().has("$grep_includes")
+        ? ruleContext.getPrerequisiteArtifact("$grep_includes", Mode.HOST)
+        : null;
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java
index c0f7629..c4b1816 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java
@@ -299,6 +299,9 @@
       CcCompilationHelper helper =
           new CcCompilationHelper(
                   ruleContext,
+                  ruleContext,
+                  ruleContext.getLabel(),
+                  CppHelper.getGrepIncludes(ruleContext),
                   cppSemantics,
                   featureConfiguration,
                   toolchain,
diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
index 949d7fb..1fcba76 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
@@ -310,6 +310,9 @@
     CcCompilationHelper result =
         new CcCompilationHelper(
                 ruleContext,
+                ruleContext,
+                ruleContext.getLabel(),
+                CppHelper.getGrepIncludes(ruleContext),
                 semantics,
                 getFeatureConfiguration(ruleContext, ccToolchain, buildConfiguration, objcProvider),
                 CcCompilationHelper.SourceCategory.CC_AND_OBJC,
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/BazelCcModuleApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/BazelCcModuleApi.java
index eb43692..a97b3de 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/BazelCcModuleApi.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/BazelCcModuleApi.java
@@ -14,6 +14,7 @@
 
 package com.google.devtools.build.lib.skylarkbuildapi.cpp;
 
+import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
 import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleContextApi;
 import com.google.devtools.build.lib.skylarkinterface.Param;
@@ -56,6 +57,7 @@
   @SkylarkCallable(
       name = "compile",
       documented = false,
+      useLocation = true,
       parameters = {
         @Param(
             name = "ctx",
@@ -127,7 +129,8 @@
       SkylarkList<FileT> headers,
       Object skylarkIncludes,
       Object skylarkCopts,
-      SkylarkList<CcCompilationContextT> ccCompilationContexts)
+      SkylarkList<CcCompilationContextT> ccCompilationContexts,
+      Location location)
       throws EvalException, InterruptedException;
 
   @SkylarkCallable(
diff --git a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java
index 9cd14e6..c170eec 100644
--- a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java
+++ b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java
@@ -173,7 +173,8 @@
       SkylarkList<FileApi> headers,
       Object skylarkIncludes,
       Object skylarkCopts,
-      SkylarkList<CcCompilationContextApi> ccCompilationContexts) {
+      SkylarkList<CcCompilationContextApi> ccCompilationContexts,
+      Location location) {
     return null;
   }