Skip to content

Commit

Permalink
Allow RustBindgen action to get --gcc-toolchain from cc toolchains (
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre authored Jan 8, 2024
1 parent db03e3e commit 44a0161
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
18 changes: 12 additions & 6 deletions bindgen/private/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def rust_bindgen_library(
header,
cc_lib,
bindgen_flags = None,
bindgen_features = None,
clang_flags = None,
**kwargs):
"""Generates a rust source file for `header`, and builds a rust_library.
Expand All @@ -57,6 +58,7 @@ def rust_bindgen_library(
header (str): The label of the .h file to generate bindings for.
cc_lib (str): The label of the cc_library that contains the .h file. This is used to find the transitive includes.
bindgen_flags (list, optional): Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details.
bindgen_features (list, optional): The `features` attribute for the `rust_bindgen` target.
clang_flags (list, optional): Flags to pass directly to the clang executable.
**kwargs: Arguments to forward to the underlying `rust_library` rule.
"""
Expand All @@ -80,6 +82,7 @@ def rust_bindgen_library(
header = header,
cc_lib = cc_lib,
bindgen_flags = bindgen_flags or [],
features = bindgen_features,
clang_flags = clang_flags or [],
tags = sub_tags,
**bindgen_kwargs
Expand Down Expand Up @@ -168,7 +171,10 @@ def _rust_bindgen_impl(ctx):
libstdcxx = toolchain.libstdcxx

output = ctx.outputs.out
tools = depset([clang_bin])

cc_toolchain, feature_configuration = find_cc_toolchain(ctx = ctx)

tools = depset([clang_bin], transitive = [cc_toolchain.all_files])

# libclang should only have 1 output file
libclang_dir = _get_libs_for_static_executable(libclang).to_list()[0].dirname
Expand Down Expand Up @@ -198,7 +204,6 @@ def _rust_bindgen_impl(ctx):
# Configure Clang Arguments
args.add("--")

cc_toolchain, feature_configuration = find_cc_toolchain(ctx = ctx)
compile_variables = cc_common.create_compile_variables(
cc_toolchain = cc_toolchain,
feature_configuration = feature_configuration,
Expand All @@ -218,7 +223,7 @@ def _rust_bindgen_impl(ctx):
# Ideally we could depend on a more specific toolchain, requesting one which is specifically clang via some constraint.
# Unfortunately, we can't currently rely on this, so instead we filter only to flags we know clang supports.
# We can add extra flags here as needed.
flags_known_to_clang = ("-I", "-iquote", "-isystem", "--sysroot")
flags_known_to_clang = ("-I", "-iquote", "-isystem", "--sysroot", "--gcc-toolchain")
open_arg = False
for arg in compile_flags:
if open_arg:
Expand Down Expand Up @@ -258,9 +263,7 @@ def _rust_bindgen_impl(ctx):
_get_libs_for_static_executable(libclang),
] + ([
_get_libs_for_static_executable(libstdcxx),
] if libstdcxx else []) + [
cc_toolchain.all_files,
],
] if libstdcxx else []),
),
outputs = [output],
mnemonic = "RustBindgen",
Expand Down Expand Up @@ -381,6 +384,7 @@ For additional information, see the [Bazel toolchains documentation](https://doc
doc = "The label of a `clang` executable.",
executable = True,
cfg = "exec",
allow_files = True,
),
"default_rustfmt": attr.bool(
doc = "If set, `rust_bindgen` targets will always format generated sources with `rustfmt`.",
Expand All @@ -391,12 +395,14 @@ For additional information, see the [Bazel toolchains documentation](https://doc
doc = "A cc_library that provides bindgen's runtime dependency on libclang.",
cfg = "exec",
providers = [CcInfo],
allow_files = True,
),
"libstdcxx": attr.label(
doc = "A cc_library that satisfies libclang's libstdc++ dependency. This is used to make the execution of clang hermetic. If None, system libraries will be used instead.",
cfg = "exec",
providers = [CcInfo],
mandatory = False,
allow_files = True,
),
},
)
3 changes: 2 additions & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,7 @@ list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
## rust_bindgen_library

<pre>
rust_bindgen_library(<a href="#rust_bindgen_library-name">name</a>, <a href="#rust_bindgen_library-header">header</a>, <a href="#rust_bindgen_library-cc_lib">cc_lib</a>, <a href="#rust_bindgen_library-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen_library-clang_flags">clang_flags</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
rust_bindgen_library(<a href="#rust_bindgen_library-name">name</a>, <a href="#rust_bindgen_library-header">header</a>, <a href="#rust_bindgen_library-cc_lib">cc_lib</a>, <a href="#rust_bindgen_library-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen_library-bindgen_features">bindgen_features</a>, <a href="#rust_bindgen_library-clang_flags">clang_flags</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
</pre>

Generates a rust source file for `header`, and builds a rust_library.
Expand All @@ -1784,6 +1784,7 @@ Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to ru
| <a id="rust_bindgen_library-header"></a>header | The label of the .h file to generate bindings for. | none |
| <a id="rust_bindgen_library-cc_lib"></a>cc_lib | The label of the cc_library that contains the .h file. This is used to find the transitive includes. | none |
| <a id="rust_bindgen_library-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details. | `None` |
| <a id="rust_bindgen_library-bindgen_features"></a>bindgen_features | The <code>features</code> attribute for the <code>rust_bindgen</code> target. | `None` |
| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` |
| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying <code>rust_library</code> rule. | none |

Expand Down
3 changes: 2 additions & 1 deletion docs/rust_bindgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
## rust_bindgen_library

<pre>
rust_bindgen_library(<a href="#rust_bindgen_library-name">name</a>, <a href="#rust_bindgen_library-header">header</a>, <a href="#rust_bindgen_library-cc_lib">cc_lib</a>, <a href="#rust_bindgen_library-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen_library-clang_flags">clang_flags</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
rust_bindgen_library(<a href="#rust_bindgen_library-name">name</a>, <a href="#rust_bindgen_library-header">header</a>, <a href="#rust_bindgen_library-cc_lib">cc_lib</a>, <a href="#rust_bindgen_library-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen_library-bindgen_features">bindgen_features</a>, <a href="#rust_bindgen_library-clang_flags">clang_flags</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
</pre>

Generates a rust source file for `header`, and builds a rust_library.
Expand All @@ -159,6 +159,7 @@ Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to ru
| <a id="rust_bindgen_library-header"></a>header | The label of the .h file to generate bindings for. | none |
| <a id="rust_bindgen_library-cc_lib"></a>cc_lib | The label of the cc_library that contains the .h file. This is used to find the transitive includes. | none |
| <a id="rust_bindgen_library-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details. | `None` |
| <a id="rust_bindgen_library-bindgen_features"></a>bindgen_features | The <code>features</code> attribute for the <code>rust_bindgen</code> target. | `None` |
| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` |
| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying <code>rust_library</code> rule. | none |

Expand Down

0 comments on commit 44a0161

Please sign in to comment.