Skip to content

Commit

Permalink
Allow rust_bindgen rules to disable rustfmt with an attr (bazelbuild#…
Browse files Browse the repository at this point in the history
…1024)

* Disable rustfmt when toolchain lacks it as documented

* Add a parameter to rust_bindgen instead

* Regenerate documentation

* Also add to rust_bindgen_library

* Regenerate documentation

* Include an unformatted rust_bindgen_library test

* Override crate names to fix build
  • Loading branch information
djmarcin authored and ddeville committed Nov 22, 2021
1 parent f04baac commit a27160a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
13 changes: 10 additions & 3 deletions bindgen/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def rust_bindgen_library(
cc_lib,
bindgen_flags = None,
clang_flags = None,
rustfmt = True,
**kwargs):
"""Generates a rust source file for `header`, and builds a rust_library.
Expand All @@ -51,6 +52,7 @@ def rust_bindgen_library(
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.
clang_flags (list, optional): Flags to pass directly to the clang executable.
rustfmt (bool, optional): Enable or disable running rustfmt on the generated file.
**kwargs: Arguments to forward to the underlying `rust_library` rule.
"""

Expand All @@ -68,6 +70,7 @@ def rust_bindgen_library(
cc_lib = cc_lib,
bindgen_flags = bindgen_flags or [],
clang_flags = clang_flags or [],
rustfmt = rustfmt,
tags = tags,
)

Expand Down Expand Up @@ -109,7 +112,7 @@ def _rust_bindgen_impl(ctx):
system_include_directories = cc_lib[CcInfo].compilation_context.system_includes.to_list()

# Vanilla usage of bindgen produces formatted output, here we do the same if we have `rustfmt` in our toolchain.
if rustfmt_bin:
if ctx.attr.rustfmt and rustfmt_bin:
unformatted_output = ctx.actions.declare_file(output.basename + ".unformatted")
else:
unformatted_output = output
Expand Down Expand Up @@ -158,7 +161,7 @@ def _rust_bindgen_impl(ctx):
tools = [clang_bin],
)

if rustfmt_bin:
if ctx.attr.rustfmt and rustfmt_bin:
rustfmt_args = ctx.actions.args()
rustfmt_args.add("--stdout-file", output.path)
rustfmt_args.add("--")
Expand Down Expand Up @@ -194,6 +197,10 @@ rust_bindgen = rule(
doc = "The .h file to generate bindings for.",
allow_single_file = True,
),
"rustfmt": attr.bool(
doc = "Enable or disable running rustfmt on the generated file.",
default = True,
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
Expand Down Expand Up @@ -249,7 +256,7 @@ rust_bindgen_toolchain = rule(
mandatory = False,
),
"rustfmt": attr.label(
doc = "The label of a `rustfmt` executable. If this is provided, generated sources will be formatted.",
doc = "The label of a `rustfmt` executable. If this is not provided, falls back to the rust_toolchain rustfmt.",
executable = True,
cfg = "exec",
mandatory = False,
Expand Down
8 changes: 5 additions & 3 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Hello world
## rust_bindgen

<pre>
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>)
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>)
</pre>

Generates a rust source file from a cc_library and a header.
Expand All @@ -356,6 +356,7 @@ Generates a rust source file from a cc_library and a header.
| <a id="rust_bindgen-cc_lib"></a>cc_lib | The cc_library that contains the .h file. This is used to find the transitive includes. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | [] |
| <a id="rust_bindgen-header"></a>header | The .h file to generate bindings for. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen-rustfmt"></a>rustfmt | Enable or disable running rustfmt on the generated file. | Boolean | optional | True |


<a id="#rust_bindgen_toolchain"></a>
Expand All @@ -378,7 +379,7 @@ The tools required for the `rust_bindgen` rule.
| <a id="rust_bindgen_toolchain-clang"></a>clang | The label of a <code>clang</code> executable. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen_toolchain-libclang"></a>libclang | A cc_library that provides bindgen's runtime dependency on libclang. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <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/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen_toolchain-rustfmt"></a>rustfmt | The label of a <code>rustfmt</code> executable. If this is provided, generated sources will be formatted. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen_toolchain-rustfmt"></a>rustfmt | The label of a <code>rustfmt</code> executable. If this is not provided, falls back to the rust_toolchain rustfmt. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |


<a id="#rust_clippy"></a>
Expand Down Expand Up @@ -1675,7 +1676,7 @@ str: A json encoded struct of crate overrides
## 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-clang_flags">clang_flags</a>, <a href="#rust_bindgen_library-rustfmt">rustfmt</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
</pre>

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


Expand Down
8 changes: 5 additions & 3 deletions docs/rust_bindgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
## rust_bindgen

<pre>
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>)
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>)
</pre>

Generates a rust source file from a cc_library and a header.
Expand All @@ -26,6 +26,7 @@ Generates a rust source file from a cc_library and a header.
| <a id="rust_bindgen-cc_lib"></a>cc_lib | The cc_library that contains the .h file. This is used to find the transitive includes. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | [] |
| <a id="rust_bindgen-header"></a>header | The .h file to generate bindings for. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen-rustfmt"></a>rustfmt | Enable or disable running rustfmt on the generated file. | Boolean | optional | True |


<a id="#rust_bindgen_toolchain"></a>
Expand All @@ -48,15 +49,15 @@ The tools required for the `rust_bindgen` rule.
| <a id="rust_bindgen_toolchain-clang"></a>clang | The label of a <code>clang</code> executable. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen_toolchain-libclang"></a>libclang | A cc_library that provides bindgen's runtime dependency on libclang. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <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/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen_toolchain-rustfmt"></a>rustfmt | The label of a <code>rustfmt</code> executable. If this is provided, generated sources will be formatted. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_bindgen_toolchain-rustfmt"></a>rustfmt | The label of a <code>rustfmt</code> executable. If this is not provided, falls back to the rust_toolchain rustfmt. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |


<a id="#rust_bindgen_library"></a>

## 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-clang_flags">clang_flags</a>, <a href="#rust_bindgen_library-rustfmt">rustfmt</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>)
</pre>

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


Expand Down
24 changes: 24 additions & 0 deletions examples/bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,27 @@ rust_test(
name = "simple_test",
crate = ":simple_example",
)

# Same as above, except disabling formatting on bindgen.
rust_bindgen_library(
name = "simple_bindgen_unformatted",
bindgen_flags = [
"--allowlist-var=SIMPLE_.*",
],
cc_lib = ":simple",
crate_name = "simple_bindgen",
header = "simple.h",
rustfmt = False,
)

rust_binary(
name = "simple_example_unformatted",
srcs = ["main.rs"],
crate_name = "simple_example",
deps = [":simple_bindgen_unformatted"],
)

rust_test(
name = "simple_test_unformatted",
crate = ":simple_example_unformatted",
)

0 comments on commit a27160a

Please sign in to comment.