Include cc configure headers in the cache key
This PR adds a file containing builtin include directories paths as an input to every C++ compilation action. The result is that whenever these headers change (as a result of reconfiguring `cc_configure` repository rule), we include this file in the action cache key.
Fixes https://github.com/bazelbuild/bazel/issues/9296.
Closes #9295.
PiperOrigin-RevId: 266783158
diff --git a/tools/cpp/lib_cc_configure.bzl b/tools/cpp/lib_cc_configure.bzl
index 789018f..fbce23b7 100644
--- a/tools/cpp/lib_cc_configure.bzl
+++ b/tools/cpp/lib_cc_configure.bzl
@@ -209,6 +209,7 @@
return "\n".join([" flag: '" + flag + "'" for flag in flags])
def get_starlark_list(values):
+ """Convert a list of string into a string that can be passed to a rule attribute."""
if not values:
return ""
return "\"" + "\",\n \"".join(values) + "\""
@@ -217,3 +218,26 @@
"""Output warning message when CC_CONFIGURE_DEBUG is enabled."""
if is_cc_configure_debug(repository_ctx):
auto_configure_warning(msg)
+
+def write_builtin_include_directory_paths(repository_ctx, cc, directories, file_suffix = ""):
+ """Generate output file named 'builtin_include_directory_paths' in the root of the repository."""
+ if get_env_var(repository_ctx, "BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS", "0", False) == "1":
+ repository_ctx.file(
+ "builtin_include_directory_paths" + file_suffix,
+ """This file is generated by cc_configure and normally contains builtin include directories
+that C++ compiler reported. But because BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS was set to 1,
+header include directory paths are intentionally not put there.
+""",
+ )
+ else:
+ repository_ctx.file(
+ "builtin_include_directory_paths" + file_suffix,
+ """This file is generated by cc_configure and contains builtin include directories
+that %s reported. This file is a dependency of every compilation action and
+changes to it will be reflected in the action cache key. When some of these
+paths change, Bazel will make sure to rerun the action, even though none of
+declared action inputs or the action commandline changes.
+
+%s
+""" % (cc, "\n".join(directories)),
+ )