Skip to content

Commit ca750fa

Browse files
authored
Eliminate Rustfmt action in Bindgen rules. Bindgen can run rustfmt (#2025)
* Eliminate Rustfmt action in Bindgen rules. Bindgen can run rustfmt * Remove conflicting attributes. * Regenerate documentation
1 parent c55ec0c commit ca750fa

File tree

4 files changed

+30
-97
lines changed

4 files changed

+30
-97
lines changed

bindgen/bindgen.bzl

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def rust_bindgen_library(
4141
cc_lib,
4242
bindgen_flags = None,
4343
clang_flags = None,
44-
rustfmt = True,
4544
**kwargs):
4645
"""Generates a rust source file for `header`, and builds a rust_library.
4746
@@ -53,7 +52,6 @@ def rust_bindgen_library(
5352
cc_lib (str): The label of the cc_library that contains the .h file. This is used to find the transitive includes.
5453
bindgen_flags (list, optional): Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details.
5554
clang_flags (list, optional): Flags to pass directly to the clang executable.
56-
rustfmt (bool, optional): Enable or disable running rustfmt on the generated file.
5755
**kwargs: Arguments to forward to the underlying `rust_library` rule.
5856
"""
5957

@@ -71,7 +69,6 @@ def rust_bindgen_library(
7169
cc_lib = cc_lib,
7270
bindgen_flags = bindgen_flags or [],
7371
clang_flags = clang_flags or [],
74-
rustfmt = rustfmt,
7572
tags = tags,
7673
)
7774

@@ -85,7 +82,6 @@ def rust_bindgen_library(
8582

8683
def _rust_bindgen_impl(ctx):
8784
toolchain = ctx.toolchains[Label("//bindgen:toolchain_type")]
88-
rustfmt_toolchain = ctx.toolchains[Label("//rust/rustfmt:toolchain_type")]
8985

9086
# nb. We can't grab the cc_library`s direct headers, so a header must be provided.
9187
cc_lib = ctx.attr.cc_lib
@@ -96,57 +92,58 @@ def _rust_bindgen_impl(ctx):
9692

9793
toolchain = ctx.toolchains[Label("//bindgen:toolchain_type")]
9894
bindgen_bin = toolchain.bindgen
99-
rustfmt_bin = rustfmt_toolchain.rustfmt
10095
clang_bin = toolchain.clang
10196
libclang = toolchain.libclang
10297
libstdcxx = toolchain.libstdcxx
10398

104-
# rustfmt is not where bindgen expects to find it, so we format manually
105-
bindgen_args = ["--no-rustfmt-bindings"] + ctx.attr.bindgen_flags
106-
clang_args = ctx.attr.clang_flags
107-
10899
output = ctx.outputs.out
100+
tools = depset([clang_bin])
109101

110102
# libclang should only have 1 output file
111103
libclang_dir = _get_libs_for_static_executable(libclang).to_list()[0].dirname
112104
include_directories = cc_lib[CcInfo].compilation_context.includes.to_list()
113105
quote_include_directories = cc_lib[CcInfo].compilation_context.quote_includes.to_list()
114106
system_include_directories = cc_lib[CcInfo].compilation_context.system_includes.to_list()
115107

116-
# Vanilla usage of bindgen produces formatted output, here we do the same if we have `rustfmt` in our toolchain.
117-
run_rustfmt = toolchain.default_rustfmt or ctx.attr.rustfmt
118-
if run_rustfmt:
119-
unformatted_output = ctx.actions.declare_file(output.basename + ".unformatted")
120-
else:
121-
unformatted_output = output
108+
env = {
109+
"CLANG_PATH": clang_bin.path,
110+
"LIBCLANG_PATH": libclang_dir,
111+
"RUST_BACKTRACE": "1",
112+
}
122113

123114
args = ctx.actions.args()
124-
args.add_all(bindgen_args)
115+
116+
# Configure Bindgen Arguments
117+
args.add_all(ctx.attr.bindgen_flags)
125118
args.add(header.path)
126-
args.add("--output", unformatted_output)
119+
args.add("--output", output)
120+
121+
# Vanilla usage of bindgen produces formatted output, here we do the same if we have `rustfmt` in our toolchain.
122+
rustfmt_toolchain = ctx.toolchains[Label("//rust/rustfmt:toolchain_type")]
123+
if toolchain.default_rustfmt:
124+
# Bindgen is able to find rustfmt using the RUSTFMT environment variable
125+
env.update({"RUSTFMT": rustfmt_toolchain.rustfmt.path})
126+
tools = depset(transitive = [tools, rustfmt_toolchain.all_files])
127+
else:
128+
args.add("--no-rustfmt-bindings")
129+
130+
# Configure Clang Arguments
127131
args.add("--")
128132
args.add_all(include_directories, before_each = "-I")
129133
args.add_all(quote_include_directories, before_each = "-iquote")
130134
args.add_all(system_include_directories, before_each = "-isystem")
131-
args.add_all(clang_args)
135+
args.add_all(ctx.attr.clang_flags)
132136

133-
env = {
134-
"CLANG_PATH": clang_bin.path,
135-
"LIBCLANG_PATH": libclang_dir,
136-
"RUST_BACKTRACE": "1",
137-
}
138137
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
139138
_, _, linker_env = get_linker_and_args(ctx, ctx.attr, "bin", cc_toolchain, feature_configuration, None)
140139
env.update(**linker_env)
141140

142-
tools = depset([clang_bin])
143-
144141
# Allow sysroots configured by the toolchain to be added to Clang arguments.
145142
if "no-rust-bindgen-cc-sysroot" not in ctx.features:
146143
if cc_toolchain.sysroot:
147144
tools = depset(transitive = [tools, cc_toolchain.all_files])
148145
sysroot_args = ["--sysroot", cc_toolchain.sysroot]
149-
for arg in clang_args:
146+
for arg in ctx.attr.clang_flags:
150147
if arg.startswith("--sysroot"):
151148
sysroot_args = []
152149
break
@@ -169,32 +166,14 @@ def _rust_bindgen_impl(ctx):
169166
_get_libs_for_static_executable(libstdcxx),
170167
] if libstdcxx else []),
171168
),
172-
outputs = [unformatted_output],
169+
outputs = [output],
173170
mnemonic = "RustBindgen",
174171
progress_message = "Generating bindings for {}..".format(header.path),
175172
env = env,
176173
arguments = [args],
177174
tools = tools,
178175
)
179176

180-
if run_rustfmt:
181-
rustfmt_args = ctx.actions.args()
182-
rustfmt_args.add("--stdout-file", output)
183-
rustfmt_args.add("--")
184-
rustfmt_args.add(rustfmt_bin)
185-
rustfmt_args.add("--emit", "stdout")
186-
rustfmt_args.add("--quiet")
187-
rustfmt_args.add(unformatted_output)
188-
189-
ctx.actions.run(
190-
executable = ctx.executable._process_wrapper,
191-
inputs = [unformatted_output],
192-
outputs = [output],
193-
arguments = [rustfmt_args],
194-
tools = [rustfmt_toolchain.all_files],
195-
mnemonic = "RustfmtBindgen",
196-
)
197-
198177
rust_bindgen = rule(
199178
doc = "Generates a rust source file from a cc_library and a header.",
200179
implementation = _rust_bindgen_impl,
@@ -213,10 +192,6 @@ rust_bindgen = rule(
213192
doc = "The `.h` file to generate bindings for.",
214193
allow_single_file = True,
215194
),
216-
"rustfmt": attr.bool(
217-
doc = "Enable or disable running rustfmt on the generated file.",
218-
default = True,
219-
),
220195
"_cc_toolchain": attr.label(
221196
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
222197
),
@@ -239,12 +214,6 @@ rust_bindgen = rule(
239214
)
240215

241216
def _rust_bindgen_toolchain_impl(ctx):
242-
if ctx.attr.rustfmt:
243-
# buildifier: disable=print
244-
print("The `rustfmt` attribute is deprecated. Please remove it on {} and register a `rustfmt_toolchain` instead.".format(
245-
ctx.label,
246-
))
247-
248217
return platform_common.ToolchainInfo(
249218
bindgen = ctx.executable.bindgen,
250219
clang = ctx.executable.clang,
@@ -309,11 +278,5 @@ For additional information, see the [Bazel toolchains documentation](https://doc
309278
providers = [CcInfo],
310279
mandatory = False,
311280
),
312-
"rustfmt": attr.label(
313-
doc = "**Deprecated**: Instead, register a `rustfmt_toolchain` and refer to the `rust_bindgen_toolchain.default_rustfmt` and `rust_bindgen.rustfmt` attributes.",
314-
executable = True,
315-
cfg = "exec",
316-
mandatory = False,
317-
),
318281
},
319282
)

docs/flatten.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`.
351351
## rust_bindgen
352352

353353
<pre>
354-
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>, <a href="#rust_bindgen-rustfmt">rustfmt</a>)
354+
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>)
355355
</pre>
356356

357357
Generates a rust source file from a cc_library and a header.
@@ -366,15 +366,14 @@ Generates a rust source file from a cc_library and a header.
366366
| <a id="rust_bindgen-cc_lib"></a>cc_lib | The cc_library that contains the <code>.h</code> file. This is used to find the transitive includes. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
367367
| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | <code>[]</code> |
368368
| <a id="rust_bindgen-header"></a>header | The <code>.h</code> file to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
369-
| <a id="rust_bindgen-rustfmt"></a>rustfmt | Enable or disable running rustfmt on the generated file. | Boolean | optional | <code>True</code> |
370369

371370

372371
<a id="rust_bindgen_toolchain"></a>
373372

374373
## rust_bindgen_toolchain
375374

376375
<pre>
377-
rust_bindgen_toolchain(<a href="#rust_bindgen_toolchain-name">name</a>, <a href="#rust_bindgen_toolchain-bindgen">bindgen</a>, <a href="#rust_bindgen_toolchain-clang">clang</a>, <a href="#rust_bindgen_toolchain-default_rustfmt">default_rustfmt</a>, <a href="#rust_bindgen_toolchain-libclang">libclang</a>, <a href="#rust_bindgen_toolchain-libstdcxx">libstdcxx</a>, <a href="#rust_bindgen_toolchain-rustfmt">rustfmt</a>)
376+
rust_bindgen_toolchain(<a href="#rust_bindgen_toolchain-name">name</a>, <a href="#rust_bindgen_toolchain-bindgen">bindgen</a>, <a href="#rust_bindgen_toolchain-clang">clang</a>, <a href="#rust_bindgen_toolchain-default_rustfmt">default_rustfmt</a>, <a href="#rust_bindgen_toolchain-libclang">libclang</a>, <a href="#rust_bindgen_toolchain-libstdcxx">libstdcxx</a>)
378377
</pre>
379378

380379
The tools required for the `rust_bindgen` rule.
@@ -416,7 +415,6 @@ For additional information, see the [Bazel toolchains documentation](https://doc
416415
| <a id="rust_bindgen_toolchain-default_rustfmt"></a>default_rustfmt | If set, <code>rust_bindgen</code> targets will always format generated sources with <code>rustfmt</code>. | Boolean | optional | <code>False</code> |
417416
| <a id="rust_bindgen_toolchain-libclang"></a>libclang | A cc_library that provides bindgen's runtime dependency on libclang. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
418417
| <a id="rust_bindgen_toolchain-libstdcxx"></a>libstdcxx | 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. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
419-
| <a id="rust_bindgen_toolchain-rustfmt"></a>rustfmt | **Deprecated**: Instead, register a <code>rustfmt_toolchain</code> and refer to the <code>rust_bindgen_toolchain.default_rustfmt</code> and <code>rust_bindgen.rustfmt</code> attributes. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
420418

421419

422420
<a id="rust_clippy"></a>
@@ -1752,7 +1750,7 @@ Declare dependencies needed for bindgen.
17521750
## rust_bindgen_library
17531751

17541752
<pre>
1755-
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-rustfmt">rustfmt</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
1753+
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>)
17561754
</pre>
17571755

17581756
Generates a rust source file for `header`, and builds a rust_library.
@@ -1770,7 +1768,6 @@ Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to ru
17701768
| <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 |
17711769
| <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` |
17721770
| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` |
1773-
| <a id="rust_bindgen_library-rustfmt"></a>rustfmt | Enable or disable running rustfmt on the generated file. | `True` |
17741771
| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying <code>rust_library</code> rule. | none |
17751772

17761773

docs/rust_bindgen.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ toolchains following the instructions for [rust_bindgen_toolchain](#rust_bindgen
5353
## rust_bindgen
5454

5555
<pre>
56-
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>, <a href="#rust_bindgen-rustfmt">rustfmt</a>)
56+
rust_bindgen(<a href="#rust_bindgen-name">name</a>, <a href="#rust_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_bindgen-cc_lib">cc_lib</a>, <a href="#rust_bindgen-clang_flags">clang_flags</a>, <a href="#rust_bindgen-header">header</a>)
5757
</pre>
5858

5959
Generates a rust source file from a cc_library and a header.
@@ -68,15 +68,14 @@ Generates a rust source file from a cc_library and a header.
6868
| <a id="rust_bindgen-cc_lib"></a>cc_lib | The cc_library that contains the <code>.h</code> file. This is used to find the transitive includes. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
6969
| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | <code>[]</code> |
7070
| <a id="rust_bindgen-header"></a>header | The <code>.h</code> file to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
71-
| <a id="rust_bindgen-rustfmt"></a>rustfmt | Enable or disable running rustfmt on the generated file. | Boolean | optional | <code>True</code> |
7271

7372

7473
<a id="rust_bindgen_toolchain"></a>
7574

7675
## rust_bindgen_toolchain
7776

7877
<pre>
79-
rust_bindgen_toolchain(<a href="#rust_bindgen_toolchain-name">name</a>, <a href="#rust_bindgen_toolchain-bindgen">bindgen</a>, <a href="#rust_bindgen_toolchain-clang">clang</a>, <a href="#rust_bindgen_toolchain-default_rustfmt">default_rustfmt</a>, <a href="#rust_bindgen_toolchain-libclang">libclang</a>, <a href="#rust_bindgen_toolchain-libstdcxx">libstdcxx</a>, <a href="#rust_bindgen_toolchain-rustfmt">rustfmt</a>)
78+
rust_bindgen_toolchain(<a href="#rust_bindgen_toolchain-name">name</a>, <a href="#rust_bindgen_toolchain-bindgen">bindgen</a>, <a href="#rust_bindgen_toolchain-clang">clang</a>, <a href="#rust_bindgen_toolchain-default_rustfmt">default_rustfmt</a>, <a href="#rust_bindgen_toolchain-libclang">libclang</a>, <a href="#rust_bindgen_toolchain-libstdcxx">libstdcxx</a>)
8079
</pre>
8180

8281
The tools required for the `rust_bindgen` rule.
@@ -118,7 +117,6 @@ For additional information, see the [Bazel toolchains documentation](https://doc
118117
| <a id="rust_bindgen_toolchain-default_rustfmt"></a>default_rustfmt | If set, <code>rust_bindgen</code> targets will always format generated sources with <code>rustfmt</code>. | Boolean | optional | <code>False</code> |
119118
| <a id="rust_bindgen_toolchain-libclang"></a>libclang | A cc_library that provides bindgen's runtime dependency on libclang. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
120119
| <a id="rust_bindgen_toolchain-libstdcxx"></a>libstdcxx | 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. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
121-
| <a id="rust_bindgen_toolchain-rustfmt"></a>rustfmt | **Deprecated**: Instead, register a <code>rustfmt_toolchain</code> and refer to the <code>rust_bindgen_toolchain.default_rustfmt</code> and <code>rust_bindgen.rustfmt</code> attributes. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
122120

123121

124122
<a id="rust_bindgen_dependencies"></a>
@@ -138,7 +136,7 @@ Declare dependencies needed for bindgen.
138136
## rust_bindgen_library
139137

140138
<pre>
141-
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-rustfmt">rustfmt</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
139+
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>)
142140
</pre>
143141

144142
Generates a rust source file for `header`, and builds a rust_library.
@@ -156,7 +154,6 @@ Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to ru
156154
| <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 |
157155
| <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` |
158156
| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` |
159-
| <a id="rust_bindgen_library-rustfmt"></a>rustfmt | Enable or disable running rustfmt on the generated file. | `True` |
160157
| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying <code>rust_library</code> rule. | none |
161158

162159

examples/bindgen/BUILD.bazel

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,3 @@ rust_test(
2626
name = "simple_test",
2727
crate = ":simple_example",
2828
)
29-
30-
# Same as above, except disabling formatting on bindgen.
31-
rust_bindgen_library(
32-
name = "simple_bindgen_unformatted",
33-
bindgen_flags = [
34-
"--allowlist-var=SIMPLE_.*",
35-
],
36-
cc_lib = ":simple",
37-
crate_name = "simple_bindgen",
38-
header = "simple.h",
39-
rustfmt = False,
40-
)
41-
42-
rust_binary(
43-
name = "simple_example_unformatted",
44-
srcs = ["main.rs"],
45-
crate_name = "simple_example",
46-
deps = [":simple_bindgen_unformatted"],
47-
)
48-
49-
rust_test(
50-
name = "simple_test_unformatted",
51-
crate = ":simple_example_unformatted",
52-
)

0 commit comments

Comments
 (0)