Use ProtoCommon.declareGeneratedFiles in cc_proto_library.

PiperOrigin-RevId: 447384067
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 c30d309..4a44ed7 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
@@ -408,12 +408,11 @@
           ccToolchainType);
     }
 
-    private ImmutableSet<Artifact> getOutputFiles(Iterable<String> suffixes) {
+    private ImmutableSet<Artifact> getOutputFiles(Iterable<String> suffixes)
+        throws RuleErrorException, InterruptedException {
       ImmutableSet.Builder<Artifact> result = ImmutableSet.builder();
       for (String suffix : suffixes) {
-        result.addAll(
-            ProtoCommon.getGeneratedOutputs(
-                ruleContext, protoInfo.getDirectProtoSources(), suffix));
+        result.addAll(ProtoCommon.declareGeneratedFiles(ruleContext, protoTarget, suffix));
       }
       return result.build();
     }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
index 916219e..d1c48ac 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
@@ -19,7 +19,6 @@
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Interner;
 import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.actions.ArtifactRoot;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.FilesToRunProvider;
 import com.google.devtools.build.lib.analysis.RuleContext;
@@ -34,7 +33,6 @@
 import com.google.devtools.build.lib.concurrent.BlazeInterners;
 import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
 import com.google.devtools.build.lib.packages.StarlarkInfo;
-import com.google.devtools.build.lib.vfs.FileSystemUtils;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import javax.annotation.Nullable;
 import net.starlark.java.eval.Dict;
@@ -80,31 +78,6 @@
   // Protocol compiler invocation stuff.
 
   /**
-   * Each language-specific initialization method will call this to construct Artifacts representing
-   * its protocol compiler outputs.
-   *
-   * @param extension Remove ".proto" and replace it with this to produce the output file name, e.g.
-   *     ".pb.cc".
-   */
-  public static ImmutableList<Artifact> getGeneratedOutputs(
-      RuleContext ruleContext, ImmutableList<Artifact> protoSources, String extension) {
-    ImmutableList.Builder<Artifact> outputsBuilder = new ImmutableList.Builder<>();
-    ArtifactRoot genfiles = ruleContext.getGenfilesDirectory();
-    for (Artifact src : protoSources) {
-      PathFragment srcPath =
-          src.getOutputDirRelativePath(ruleContext.getConfiguration().isSiblingRepositoryLayout());
-
-      // Note that two proto_library rules can have the same source file, so this is actually a
-      // shared action. NB: This can probably result in action conflicts if the proto_library rules
-      // are not the same.
-      outputsBuilder.add(
-          ruleContext.getShareableArtifact(
-              FileSystemUtils.replaceExtension(srcPath, extension), genfiles));
-    }
-    return outputsBuilder.build();
-  }
-
-  /**
    * Decides whether this proto_library should check for strict proto deps.
    *
    * <p>Only takes into account the command-line flag --strict_proto_deps.
diff --git a/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl b/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl
index d966725..608c1fb 100644
--- a/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl
+++ b/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl
@@ -293,6 +293,7 @@
 proto_common = struct(
     create_proto_compile_action = _create_proto_compile_action,
     incompatible_py_uses_bindir = True,
+    incompatible_cc_uses_bindir = True,
 )
 
 proto_common_do_not_use = struct(