Make grep_includes a public attribute (#838)

This allows users to provide this tool if they want to optimize this for
large files. No default is provided.

Closes #838

COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/838 from keith:ks/make-grep_includes-a-public-attribute 45c6e18f5ad27273b12ab18cd11a716e03058c38
PiperOrigin-RevId: 965251189
Change-Id: I5b54375b145c43cbdeab952c7964a0c6875fe755
diff --git a/cc/common/semantics.bzl b/cc/common/semantics.bzl
index 6d87995..0f06cbd 100644
--- a/cc/common/semantics.bzl
+++ b/cc/common/semantics.bzl
@@ -67,7 +67,10 @@
     )
 
 def _get_grep_includes():
-    return attr.label()
+    return attr.label(
+        allow_single_file = True,
+        cfg = "exec",
+    )
 
 def _get_runtimes_toolchain():
     return [
diff --git a/cc/private/rules_impl/cc_toolchain.bzl b/cc/private/rules_impl/cc_toolchain.bzl
index a6597b7..ab55e6b 100644
--- a/cc/private/rules_impl/cc_toolchain.bzl
+++ b/cc/private/rules_impl/cc_toolchain.bzl
@@ -85,10 +85,6 @@
     return None
 
 def _attributes(ctx):
-    grep_includes = None
-    if not semantics.is_bazel:
-        grep_includes = _single_file(ctx, "_grep_includes")
-
     latebound_libc = _latebound_libc(ctx, "libc_top", "_libc_top")
 
     if ctx.attr.toolchain_config:
@@ -146,7 +142,7 @@
         supports_header_parsing = ctx.attr.supports_header_parsing,
         all_files = all_files,
         link_dynamic_library_tool = ctx.file._link_dynamic_library_tool,
-        grep_includes = grep_includes,
+        grep_includes = _single_file(ctx, "grep_includes"),
         aggregate_ddi = _single_file(ctx, "_aggregate_ddi"),
         generate_modmap = _single_file(ctx, "_generate_modmap"),
         module_map = ctx.attr.module_map,
@@ -308,6 +304,7 @@
 Collection of all cc_toolchain artifacts required for coverage actions. If not specified,
 all_files are used.""",
         ),
+        "grep_includes": semantics.get_grep_includes(),
         "libc_top": attr.label(
             # TODO(b/78578234): Make this the default and remove the late-bound versions.
             allow_files = False,
@@ -358,7 +355,6 @@
         "_libc_top": attr.label(
             default = configuration_field(fragment = "cpp", name = "libc_top"),
         ),
-        "_grep_includes": semantics.get_grep_includes(),
         "_interface_library_builder": attr.label(
             default = "@bazel_tools//tools/cpp:interface_library_builder",
             allow_single_file = True,
diff --git a/cc/private/toolchain/BUILD b/cc/private/toolchain/BUILD
index 8f616ec..abd4e6b 100644
--- a/cc/private/toolchain/BUILD
+++ b/cc/private/toolchain/BUILD
@@ -59,11 +59,6 @@
 )
 
 filegroup(
-    name = "grep-includes",
-    srcs = ["grep-includes.sh"],
-)
-
-filegroup(
     name = "empty",
     srcs = [],
 )
diff --git a/cc/private/toolchain/grep-includes.sh b/cc/private/toolchain/grep-includes.sh
deleted file mode 100755
index c6a5383..0000000
--- a/cc/private/toolchain/grep-includes.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-#
-# Copyright 2018 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.
-#
-# TODO(bazel-team): Support include scanning and grep-includes in Bazel
-echo "grep-includes is not supported by Bazel"
-exit 1
diff --git a/cc/toolchains/toolchain.bzl b/cc/toolchains/toolchain.bzl
index 6637d44..0c9ab94 100644
--- a/cc/toolchains/toolchain.bzl
+++ b/cc/toolchains/toolchain.bzl
@@ -44,6 +44,7 @@
         legacy_tools = [],
         known_features = [],
         enabled_features = [],
+        grep_includes = None,
         libc_top = None,
         module_map = None,
         dynamic_runtime_lib = None,
@@ -114,6 +115,8 @@
             [features](https://bazel.build/docs/cc-toolchain-config-reference#features)
             to be disabled over the course of a build through other mechanisms. See the
             documentation for `cc_feature` for more information.
+        grep_includes: (Label) Executable used to extract include directives from C/C++ source
+            files.
         libc_top: (Label) A collection of artifacts for libc passed as inputs to compile/linking
             actions. See
             [`cc_toolchain.libc_top`](https://bazel.build/reference/be/c-cpp#cc_toolchain.libc_top)
@@ -187,6 +190,7 @@
             legacy_tools = legacy_tools,
             known_features = known_features,
             enabled_features = enabled_features,
+            grep_includes = grep_includes,
             compiler = compiler,
             target_libc = target_libc,
             cpu = cpu or _CPU,
diff --git a/docs/toolchain_api.md b/docs/toolchain_api.md
index 95f57af..6e2fb70 100644
--- a/docs/toolchain_api.md
+++ b/docs/toolchain_api.md
@@ -793,9 +793,9 @@
 load("@rules_cc//cc/toolchains/impl:documented_api.bzl", "cc_toolchain")
 
 cc_toolchain(*, <a href="#cc_toolchain-name">name</a>, <a href="#cc_toolchain-tool_map">tool_map</a>, <a href="#cc_toolchain-args">args</a>, <a href="#cc_toolchain-artifact_name_patterns">artifact_name_patterns</a>, <a href="#cc_toolchain-make_variables">make_variables</a>, <a href="#cc_toolchain-legacy_tools">legacy_tools</a>,
-             <a href="#cc_toolchain-known_features">known_features</a>, <a href="#cc_toolchain-enabled_features">enabled_features</a>, <a href="#cc_toolchain-libc_top">libc_top</a>, <a href="#cc_toolchain-module_map">module_map</a>, <a href="#cc_toolchain-dynamic_runtime_lib">dynamic_runtime_lib</a>,
-             <a href="#cc_toolchain-static_runtime_lib">static_runtime_lib</a>, <a href="#cc_toolchain-supports_header_parsing">supports_header_parsing</a>, <a href="#cc_toolchain-supports_param_files">supports_param_files</a>, <a href="#cc_toolchain-compiler">compiler</a>, <a href="#cc_toolchain-cpu">cpu</a>,
-             <a href="#cc_toolchain-target_system_name">target_system_name</a>, <a href="#cc_toolchain-kwargs">**kwargs</a>)
+             <a href="#cc_toolchain-known_features">known_features</a>, <a href="#cc_toolchain-enabled_features">enabled_features</a>, <a href="#cc_toolchain-grep_includes">grep_includes</a>, <a href="#cc_toolchain-libc_top">libc_top</a>, <a href="#cc_toolchain-module_map">module_map</a>,
+             <a href="#cc_toolchain-dynamic_runtime_lib">dynamic_runtime_lib</a>, <a href="#cc_toolchain-static_runtime_lib">static_runtime_lib</a>, <a href="#cc_toolchain-supports_header_parsing">supports_header_parsing</a>, <a href="#cc_toolchain-supports_param_files">supports_param_files</a>,
+             <a href="#cc_toolchain-compiler">compiler</a>, <a href="#cc_toolchain-cpu">cpu</a>, <a href="#cc_toolchain-target_system_name">target_system_name</a>, <a href="#cc_toolchain-kwargs">**kwargs</a>)
 </pre>
 
 A C/C++ toolchain configuration.
@@ -851,6 +851,7 @@
 | <a id="cc_toolchain-legacy_tools"></a>legacy_tools |  (List[Label]) A list of `cc_legacy_tool` rules that specify tools by filesystem path. These are used to populate the legacy `tool_paths` parameter of the toolchain configuration, which is required by some Bazel features (e.g. coverage).   |  `[]` |
 | <a id="cc_toolchain-known_features"></a>known_features |  (List[Label]) A list of [`cc_feature`](#cc_feature) rules that this toolchain supports. Whether or not these [features](https://bazel.build/docs/cc-toolchain-config-reference#features) are enabled may change over the course of a build. See the documentation for [`cc_feature`](#cc_feature) for more information.   |  `[]` |
 | <a id="cc_toolchain-enabled_features"></a>enabled_features |  (List[Label]) A list of [`cc_feature`](#cc_feature) rules whose initial state should be `enabled`. Note that it is still possible for these [features](https://bazel.build/docs/cc-toolchain-config-reference#features) to be disabled over the course of a build through other mechanisms. See the documentation for [`cc_feature`](#cc_feature) for more information.   |  `[]` |
+| <a id="cc_toolchain-grep_includes"></a>grep_includes |  (Label) Executable used to extract include directives from C/C++ source files.   |  `None` |
 | <a id="cc_toolchain-libc_top"></a>libc_top |  (Label) A collection of artifacts for libc passed as inputs to compile/linking actions. See [`cc_toolchain.libc_top`](https://bazel.build/reference/be/c-cpp#cc_toolchain.libc_top) for more information.   |  `None` |
 | <a id="cc_toolchain-module_map"></a>module_map |  (Label) Module map artifact to be used for modular builds. See [`cc_toolchain.module_map`](https://bazel.build/reference/be/c-cpp#cc_toolchain.module_map) for more information.   |  `None` |
 | <a id="cc_toolchain-dynamic_runtime_lib"></a>dynamic_runtime_lib |  (Label) Dynamic library to link when the `static_link_cpp_runtimes` and `dynamic_linking_mode` [features](https://bazel.build/docs/cc-toolchain-config-reference#features) are both enabled. See [`cc_toolchain.dynamic_runtime_lib`](https://bazel.build/reference/be/c-cpp#cc_toolchain.dynamic_runtime_lib) for more information.   |  `None` |