Bazel: Add full support for virtual includes

This change adds support for using the `includes`, `include_prefix` and
`strip_include_prefix` attributes of the `cc_library()` rule. Without it,
the libtooling based header generator will not be able to find all
necessary includes as it is much stricter than the current libclang based
one in that regard.

PiperOrigin-RevId: 491574088
Change-Id: Icb9f7d2719472ee1afa5df85b185c527a3c64994
This commit is contained in:
Christian Blichmann 2022-11-29 02:17:03 -08:00 committed by Copybara-Service
parent e5971312eb
commit 92a8247777

View File

@ -70,6 +70,34 @@ def sort_deps(deps):
other_deps = [x for x in deps if not x.startswith(":")]
return sorted(colon_deps) + sorted(other_deps)
def cc_library_virtual_includes(target):
"""Checks a target for virtual includes.
Those can be created by the deprecated `cc_inc_library` rule, or by using
a combination of `cc_library()`s `includes`, `include_prefix` and
`strip_include_prefix` attributes.
Args:
target: The Target to analyze
Returns:
A depset with include paths generated by cc_inc_library targets.
"""
cc_ctx = target[CcInfo].compilation_context
includes = []
for f in cc_ctx.headers.to_list():
p = f.path
if not p.startswith("blaze-out") and not p.startswith("bazel-out"):
continue
for path_marker in ["/_virtual_includes/", "/_/"]:
i = p.find(path_marker)
if i == -1:
continue
includes.append(p[:i] + path_marker +
p[i + len(path_marker):].split("/", 1)[0])
return depset(includes)
def _sapi_interface_impl(ctx):
cpp_toolchain = find_cpp_toolchain(ctx)
generator = select_generator(ctx)
@ -106,7 +134,9 @@ def _sapi_interface_impl(ctx):
# Append all headers as dependencies
input_files += cc_ctx.headers.to_list()
quote_includes = cc_ctx.quote_includes.to_list()
# Gather direct include paths as well as virtual ones
quote_includes = (cc_ctx.quote_includes.to_list() +
cc_library_virtual_includes(ctx.attr.lib).to_list())
if use_clang_generator:
input_files += cpp_toolchain.all_files.to_list()