Allow sysroots from cc_toolchains to be added to bindgen actions (#2024)
diff --git a/bindgen/bindgen.bzl b/bindgen/bindgen.bzl
index 8adf552..6f20c3e 100644
--- a/bindgen/bindgen.bzl
+++ b/bindgen/bindgen.bzl
@@ -139,6 +139,19 @@
_, _, linker_env = get_linker_and_args(ctx, ctx.attr, "bin", cc_toolchain, feature_configuration, None)
env.update(**linker_env)
+ tools = depset([clang_bin])
+
+ # Allow sysroots configured by the toolchain to be added to Clang arguments.
+ if "no-rust-bindgen-cc-sysroot" not in ctx.features:
+ if cc_toolchain.sysroot:
+ tools = depset(transitive = [tools, cc_toolchain.all_files])
+ sysroot_args = ["--sysroot", cc_toolchain.sysroot]
+ for arg in clang_args:
+ if arg.startswith("--sysroot"):
+ sysroot_args = []
+ break
+ args.add_all(sysroot_args)
+
# Set the dynamic linker search path so that clang uses the libstdcxx from the toolchain.
# DYLD_LIBRARY_PATH is LD_LIBRARY_PATH on macOS.
if libstdcxx:
@@ -161,7 +174,7 @@
progress_message = "Generating bindings for {}..".format(header.path),
env = env,
arguments = [args],
- tools = [clang_bin],
+ tools = tools,
)
if run_rustfmt: