Skip to content

Commit 44a0161

Browse files
authored
Allow RustBindgen action to get --gcc-toolchain from cc toolchains (#2400)
1 parent db03e3e commit 44a0161

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

bindgen/private/bindgen.bzl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def rust_bindgen_library(
4646
header,
4747
cc_lib,
4848
bindgen_flags = None,
49+
bindgen_features = None,
4950
clang_flags = None,
5051
**kwargs):
5152
"""Generates a rust source file for `header`, and builds a rust_library.
@@ -57,6 +58,7 @@ def rust_bindgen_library(
5758
header (str): The label of the .h file to generate bindings for.
5859
cc_lib (str): The label of the cc_library that contains the .h file. This is used to find the transitive includes.
5960
bindgen_flags (list, optional): Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details.
61+
bindgen_features (list, optional): The `features` attribute for the `rust_bindgen` target.
6062
clang_flags (list, optional): Flags to pass directly to the clang executable.
6163
**kwargs: Arguments to forward to the underlying `rust_library` rule.
6264
"""
@@ -80,6 +82,7 @@ def rust_bindgen_library(
8082
header = header,
8183
cc_lib = cc_lib,
8284
bindgen_flags = bindgen_flags or [],
85+
features = bindgen_features,
8386
clang_flags = clang_flags or [],
8487
tags = sub_tags,
8588
**bindgen_kwargs
@@ -168,7 +171,10 @@ def _rust_bindgen_impl(ctx):
168171
libstdcxx = toolchain.libstdcxx
169172

170173
output = ctx.outputs.out
171-
tools = depset([clang_bin])
174+
175+
cc_toolchain, feature_configuration = find_cc_toolchain(ctx = ctx)
176+
177+
tools = depset([clang_bin], transitive = [cc_toolchain.all_files])
172178

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

201-
cc_toolchain, feature_configuration = find_cc_toolchain(ctx = ctx)
202207
compile_variables = cc_common.create_compile_variables(
203208
cc_toolchain = cc_toolchain,
204209
feature_configuration = feature_configuration,
@@ -218,7 +223,7 @@ def _rust_bindgen_impl(ctx):
218223
# Ideally we could depend on a more specific toolchain, requesting one which is specifically clang via some constraint.
219224
# Unfortunately, we can't currently rely on this, so instead we filter only to flags we know clang supports.
220225
# We can add extra flags here as needed.
221-
flags_known_to_clang = ("-I", "-iquote", "-isystem", "--sysroot")
226+
flags_known_to_clang = ("-I", "-iquote", "-isystem", "--sysroot", "--gcc-toolchain")
222227
open_arg = False
223228
for arg in compile_flags:
224229
if open_arg:
@@ -258,9 +263,7 @@ def _rust_bindgen_impl(ctx):
258263
_get_libs_for_static_executable(libclang),
259264
] + ([
260265
_get_libs_for_static_executable(libstdcxx),
261-
] if libstdcxx else []) + [
262-
cc_toolchain.all_files,
263-
],
266+
] if libstdcxx else []),
264267
),
265268
outputs = [output],
266269
mnemonic = "RustBindgen",
@@ -381,6 +384,7 @@ For additional information, see the [Bazel toolchains documentation](https://doc
381384
doc = "The label of a `clang` executable.",
382385
executable = True,
383386
cfg = "exec",
387+
allow_files = True,
384388
),
385389
"default_rustfmt": attr.bool(
386390
doc = "If set, `rust_bindgen` targets will always format generated sources with `rustfmt`.",
@@ -391,12 +395,14 @@ For additional information, see the [Bazel toolchains documentation](https://doc
391395
doc = "A cc_library that provides bindgen's runtime dependency on libclang.",
392396
cfg = "exec",
393397
providers = [CcInfo],
398+
allow_files = True,
394399
),
395400
"libstdcxx": attr.label(
396401
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.",
397402
cfg = "exec",
398403
providers = [CcInfo],
399404
mandatory = False,
405+
allow_files = True,
400406
),
401407
},
402408
)

docs/flatten.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
17671767
## rust_bindgen_library
17681768

17691769
<pre>
1770-
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>)
1770+
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>)
17711771
</pre>
17721772

17731773
Generates a rust source file for `header`, and builds a rust_library.
@@ -1784,6 +1784,7 @@ Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to ru
17841784
| <a id="rust_bindgen_library-header"></a>header | The label of the .h file to generate bindings for. | none |
17851785
| <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 |
17861786
| <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` |
1787+
| <a id="rust_bindgen_library-bindgen_features"></a>bindgen_features | The <code>features</code> attribute for the <code>rust_bindgen</code> target. | `None` |
17871788
| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` |
17881789
| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying <code>rust_library</code> rule. | none |
17891790

docs/rust_bindgen.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
142142
## rust_bindgen_library
143143

144144
<pre>
145-
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>)
145+
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>)
146146
</pre>
147147

148148
Generates a rust source file for `header`, and builds a rust_library.
@@ -159,6 +159,7 @@ Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to ru
159159
| <a id="rust_bindgen_library-header"></a>header | The label of the .h file to generate bindings for. | none |
160160
| <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 |
161161
| <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` |
162+
| <a id="rust_bindgen_library-bindgen_features"></a>bindgen_features | The <code>features</code> attribute for the <code>rust_bindgen</code> target. | `None` |
162163
| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` |
163164
| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying <code>rust_library</code> rule. | none |
164165

0 commit comments

Comments
 (0)