Disable Crubit on grpc and targets that disable header parsing.

This allows Crubit to work on targets that depend on grpc, as long as they do not transitively include the headers. So, that's a workaround, at least.

This is not "strictly correct": a target might disable header parsing because it has a bad header in `srcs`, which isn't relevant to Crubit which only reads `hdrs`. However, it's almost certainly (also) broken in `srcs`. If ever we need to enable Crubit on a target that has a broken srcs header, but not a broken.

As far as grpc itself goes, we just don't correctly handle those headers right now. If grpc shows up anywhere in the transitive dependency graph, it breaks Crubit. That's a problem, and this works around that by disabling Crubit.

(Similarly for openssl.)

In the longer term, we should not have the grpc denylist, and I've filed b/349750606 and b/349750659 to track some of this work.

PiperOrigin-RevId: 647810496
Change-Id: Ic75dd706304d4373afa6c565633dd9c397b042cc
diff --git a/rs_bindings_from_cc/bazel_support/rust_bindings_from_cc_aspect.bzl b/rs_bindings_from_cc/bazel_support/rust_bindings_from_cc_aspect.bzl
index d44ce85..9cd8fa1 100644
--- a/rs_bindings_from_cc/bazel_support/rust_bindings_from_cc_aspect.bzl
+++ b/rs_bindings_from_cc/bazel_support/rust_bindings_from_cc_aspect.bzl
@@ -43,10 +43,18 @@
 def _filter_hdrs(input_list):
     return [hdr for hdr in input_list if _is_hdr(hdr)]
 
-# Targets which do not receive rust bindings at all.
+# Targets which do not receive rust bindings at all. Most significantly, the header is not
+# attributed to belonging to this target. So, the main use for this list is to resolve
+# ambiguously-owned headers by disabling one of the targets.
 targets_to_remove = [
 ]
 
+# If a target starts with any of these strings, Crubit will not parse the bindings.
+disabled_prefixes = [
+    "//third_party/grpc/",
+    "//net/grpc/",
+]
+
 # Specific headers, in specific targets, which do not receive Rust bindings.
 #
 # This is mainly for if the same header is in two different targets, only one of which is canonical.
@@ -55,7 +63,21 @@
 private_headers_to_remove = {
 }
 
-def _collect_hdrs(ctx):
+def _enabled(target, ctx):
+    """Returns True if Crubit is enabled, False if Crubit should not generate bindings for it."""
+
+    # Targets/packages with -parse_headers cannot work with Crubit, which parses headers.
+    # We maintain the target ownership (the target_args), but we do not actually generate bindings
+    # for the headers, by pretending the headers were private.
+    if "parse_headers" in ctx.disabled_features:
+        return False
+    label = str(target.label)
+    for prefix in disabled_prefixes:
+        if label.startswith(prefix):
+            return False
+    return True
+
+def _collect_hdrs(target, ctx):
     public_hdrs = _filter_hdrs(ctx.rule.files.hdrs)
     private_hdrs = _filter_hdrs(ctx.rule.files.srcs) if hasattr(ctx.rule.attr, "srcs") else []
     label = str(ctx.label)
@@ -71,6 +93,9 @@
     ]
 
     all_standalone_hdrs = depset(public_hdrs + private_hdrs).to_list()
+
+    if not _enabled(target, ctx):
+        public_hdrs = []
     return public_hdrs, all_standalone_hdrs
 
 def _is_proto_library(target):
@@ -114,7 +139,7 @@
     public_hdrs = []
     all_standalone_hdrs = []
     if hasattr(ctx.rule.attr, "hdrs"):
-        public_hdrs, all_standalone_hdrs = _collect_hdrs(ctx)
+        public_hdrs, all_standalone_hdrs = _collect_hdrs(target, ctx)
     elif _is_proto_library(target):
         #TODO(b/232199093): Ideally we would get this information from a proto-specific provider,
         # but ProtoCcFilesProvider is private currently. Use it once public.
diff --git a/rs_bindings_from_cc/cmdline.cc b/rs_bindings_from_cc/cmdline.cc
index ee66904..832cdb4 100644
--- a/rs_bindings_from_cc/cmdline.cc
+++ b/rs_bindings_from_cc/cmdline.cc
@@ -60,7 +60,8 @@
 ABSL_FLAG(std::vector<std::string>, public_headers, std::vector<std::string>(),
           "public headers of the cc_library this tool should generate bindings "
           "for, in a format suitable for usage in google3-relative quote "
-          "include (#include \"\").");
+          "include (#include \"\"). Note that this can be different than the "
+          "headers attributed to the target via target_args.");
 ABSL_FLAG(std::string, target, "", "The target to generate bindings for.");
 ABSL_FLAG(std::string, target_args, "",
           "Per-target Crubit arguments, encoded as a JSON array. This contains "
diff --git a/rs_bindings_from_cc/importers/BUILD b/rs_bindings_from_cc/importers/BUILD
index 1b14b2b..4a76aab 100644
--- a/rs_bindings_from_cc/importers/BUILD
+++ b/rs_bindings_from_cc/importers/BUILD
@@ -88,6 +88,7 @@
         "//rs_bindings_from_cc:cc_ir",
         "//rs_bindings_from_cc:decl_importer",
         "//rs_bindings_from_cc:recording_diagnostic_consumer",
+        "@abseil-cpp//absl/log",
         "@abseil-cpp//absl/log:check",
         "@abseil-cpp//absl/status:statusor",
         "@abseil-cpp//absl/strings",
diff --git a/rs_bindings_from_cc/importers/function.cc b/rs_bindings_from_cc/importers/function.cc
index 5d4b3d3..c2b1905 100644
--- a/rs_bindings_from_cc/importers/function.cc
+++ b/rs_bindings_from_cc/importers/function.cc
@@ -13,6 +13,7 @@
 #include <vector>
 
 #include "absl/log/check.h"
+#include "absl/log/log.h"
 #include "absl/status/statusor.h"
 #include "absl/strings/str_cat.h"
 #include "absl/strings/substitute.h"