Set --cpp_out and include strip prefix correctly in C++ compile actions for
.pb.cc when strip_import_prefix= is present on the proto_library rule.

Fixes #7325 .

RELNOTES: None.
PiperOrigin-RevId: 238226377
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 ba9510b..5ae822d 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
@@ -312,6 +312,24 @@
               .addQuoteIncludeDirs(cppSemantics.getQuoteIncludes(ruleContext));
       // Don't instrument the generated C++ files even when --collect_code_coverage is set.
       helper.setCodeCoverageEnabled(false);
+
+      String protoRoot = protoInfo.getDirectProtoSourceRoot();
+      PathFragment repositoryRoot =
+          ruleContext.getLabel().getPackageIdentifier().getRepository().getSourceRoot();
+      if (protoRoot.equals(".") || protoRoot.equals(repositoryRoot.getPathString())) {
+        return helper;
+      }
+
+      PathFragment protoRootFragment = PathFragment.create(protoRoot);
+      PathFragment binOrGenfiles = ruleContext.getBinOrGenfilesDirectory().getExecPath();
+      if (protoRootFragment.startsWith(binOrGenfiles)) {
+        protoRootFragment = protoRootFragment.relativeTo(binOrGenfiles);
+      }
+
+      String stripIncludePrefix =
+          PathFragment.create("//").getRelative(protoRootFragment).toString();
+      helper.setStripIncludePrefix(stripIncludePrefix);
+
       return helper;
     }
 
@@ -376,13 +394,14 @@
     }
 
     private void createProtoCompileAction(Collection<Artifact> outputs) {
-      String protoRoot = protoInfo.getDirectProtoSourceRoot();
-      String genfilesPath =
-          ruleContext
-              .getConfiguration()
-              .getGenfilesFragment()
-              .getRelative(protoRoot)
-              .getPathString();
+      PathFragment protoRootFragment = PathFragment.create(protoInfo.getDirectProtoSourceRoot());
+      String genfilesPath;
+      PathFragment genfilesFragment = ruleContext.getConfiguration().getGenfilesFragment();
+      if (protoRootFragment.startsWith(genfilesFragment)) {
+        genfilesPath = protoRootFragment.getPathString();
+      } else {
+        genfilesPath = genfilesFragment.getRelative(protoRootFragment).getPathString();
+      }
 
       ImmutableList.Builder<ToolchainInvocation> invocations = ImmutableList.builder();
       invocations.add(
diff --git a/src/test/shell/bazel/bazel_proto_library_test.sh b/src/test/shell/bazel/bazel_proto_library_test.sh
index 04784ab..f211683 100755
--- a/src/test/shell/bazel/bazel_proto_library_test.sh
+++ b/src/test/shell/bazel/bazel_proto_library_test.sh
@@ -432,6 +432,62 @@
   bazel build @main_repo//src:all_protos >& "$TEST_log" || fail "Expected success"
 }
 
+function test_cc_proto_library() {
+  write_workspace ""
+  mkdir -p a
+  cat > a/BUILD <<EOF
+proto_library(name='p', srcs=['p.proto'])
+cc_proto_library(name='cp', deps=[':p'])
+cc_library(name='c', srcs=['c.cc'], deps=[':cp'])
+EOF
+
+  cat > a/p.proto <<EOF
+syntax = "proto2";
+package a;
+message A {
+  optional int32 a = 1;
+}
+EOF
+
+  cat > a/c.cc <<EOF
+#include "a/p.pb.h"
+
+void f() {
+  a::A a;
+}
+EOF
+
+  bazel build //a:c || fail "build failed"
+}
+
+function test_cc_proto_library_import_prefix_stripping() {
+  write_workspace ""
+  mkdir -p a/dir
+  cat > a/BUILD <<EOF
+proto_library(name='p', srcs=['dir/p.proto'], strip_import_prefix='/a')
+cc_proto_library(name='cp', deps=[':p'])
+cc_library(name='c', srcs=['c.cc'], deps=[':cp'])
+EOF
+
+  cat > a/dir/p.proto <<EOF
+syntax = "proto2";
+package a;
+message A {
+  optional int32 a = 1;
+}
+EOF
+
+  cat > a/c.cc <<EOF
+#include "dir/p.pb.h"
+
+void f() {
+  a::A a;
+}
+EOF
+
+  bazel build //a:c || fail "build failed"
+}
+
 function test_import_prefix_stripping() {
   mkdir -p e
   touch e/WORKSPACE