1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build --> 2*d4726bddSHONG Yifan# Rust Bindgen 3*d4726bddSHONG Yifan 4*d4726bddSHONG Yifan* [rust_bindgen_library](#rust_bindgen_library) 5*d4726bddSHONG Yifan* [rust_bindgen](#rust_bindgen) 6*d4726bddSHONG Yifan* [rust_bindgen_toolchain](#rust_bindgen_toolchain) 7*d4726bddSHONG Yifan* [rust_bindgen_dependencies](#rust_bindgen_dependencies) 8*d4726bddSHONG Yifan* [rust_bindgen_register_toolchains](#rust_bindgen_register_toolchains) 9*d4726bddSHONG Yifan 10*d4726bddSHONG Yifan 11*d4726bddSHONG Yifan## Overview 12*d4726bddSHONG Yifan 13*d4726bddSHONG YifanThese rules are for using [Bindgen][bindgen] to generate [Rust][rust] bindings to C (and some C++) libraries. 14*d4726bddSHONG Yifan 15*d4726bddSHONG Yifan[rust]: http://www.rust-lang.org/ 16*d4726bddSHONG Yifan[bindgen]: https://github.com/rust-lang/rust-bindgen 17*d4726bddSHONG Yifan 18*d4726bddSHONG YifanSee the [bindgen example](https://github.com/bazelbuild/rules_rust/tree/main/examples/bindgen/BUILD.bazel) for a more complete example of use. 19*d4726bddSHONG Yifan 20*d4726bddSHONG Yifan### Setup 21*d4726bddSHONG Yifan 22*d4726bddSHONG YifanTo use the Rust bindgen rules, add the following to your `WORKSPACE` file to add the 23*d4726bddSHONG Yifanexternal repositories for the Rust bindgen toolchain (in addition to the [rust rules setup](https://bazelbuild.github.io/rules_rust/#setup)): 24*d4726bddSHONG Yifan 25*d4726bddSHONG Yifan```python 26*d4726bddSHONG Yifanload("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_dependencies", "rust_bindgen_register_toolchains") 27*d4726bddSHONG Yifan 28*d4726bddSHONG Yifanrust_bindgen_dependencies() 29*d4726bddSHONG Yifan 30*d4726bddSHONG Yifanrust_bindgen_register_toolchains() 31*d4726bddSHONG Yifan 32*d4726bddSHONG Yifanload("@rules_rust//bindgen:transitive_repositories.bzl", "rust_bindgen_transitive_dependencies") 33*d4726bddSHONG Yifan 34*d4726bddSHONG Yifanrust_bindgen_transitive_dependencies() 35*d4726bddSHONG Yifan``` 36*d4726bddSHONG Yifan 37*d4726bddSHONG YifanBindgen aims to be as hermetic as possible so will end up building `libclang` from [llvm-project][llvm_proj] from 38*d4726bddSHONG Yifansource. If this is found to be undesirable then no Bindgen related calls should be added to your WORKSPACE and instead 39*d4726bddSHONG Yifanusers should define their own repositories using something akin to [crate_universe][cra_uni] and define their own 40*d4726bddSHONG Yifantoolchains following the instructions for [rust_bindgen_toolchain](#rust_bindgen_toolchain). 41*d4726bddSHONG Yifan 42*d4726bddSHONG Yifan[llvm_proj]: https://github.com/llvm/llvm-project 43*d4726bddSHONG Yifan[cra_uni]: https://bazelbuild.github.io/rules_rust/crate_universe.html 44*d4726bddSHONG Yifan 45*d4726bddSHONG Yifan--- 46*d4726bddSHONG Yifan 47*d4726bddSHONG Yifan--- 48*d4726bddSHONG Yifan 49*d4726bddSHONG Yifan 50*d4726bddSHONG Yifan 51*d4726bddSHONG Yifan<a id="rust_bindgen"></a> 52*d4726bddSHONG Yifan 53*d4726bddSHONG Yifan## rust_bindgen 54*d4726bddSHONG Yifan 55*d4726bddSHONG Yifan<pre> 56*d4726bddSHONG Yifanrust_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-wrap_static_fns">wrap_static_fns</a>) 57*d4726bddSHONG Yifan</pre> 58*d4726bddSHONG Yifan 59*d4726bddSHONG YifanGenerates a rust source file from a cc_library and a header. 60*d4726bddSHONG Yifan 61*d4726bddSHONG Yifan**ATTRIBUTES** 62*d4726bddSHONG Yifan 63*d4726bddSHONG Yifan 64*d4726bddSHONG Yifan| Name | Description | Type | Mandatory | Default | 65*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- | 66*d4726bddSHONG Yifan| <a id="rust_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | 67*d4726bddSHONG Yifan| <a id="rust_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details. | List of strings | optional | `[]` | 68*d4726bddSHONG Yifan| <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/concepts/labels">Label</a> | required | | 69*d4726bddSHONG Yifan| <a id="rust_bindgen-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | `[]` | 70*d4726bddSHONG Yifan| <a id="rust_bindgen-header"></a>header | The `.h` file to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | | 71*d4726bddSHONG Yifan| <a id="rust_bindgen-wrap_static_fns"></a>wrap_static_fns | Whether to create a separate .c file for static fns. Requires nightly toolchain, and a header that actually needs this feature (otherwise bindgen won't generate the file and Bazel complains). | Boolean | optional | `False` | 72*d4726bddSHONG Yifan 73*d4726bddSHONG Yifan 74*d4726bddSHONG Yifan<a id="rust_bindgen_toolchain"></a> 75*d4726bddSHONG Yifan 76*d4726bddSHONG Yifan## rust_bindgen_toolchain 77*d4726bddSHONG Yifan 78*d4726bddSHONG Yifan<pre> 79*d4726bddSHONG Yifanrust_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>) 80*d4726bddSHONG Yifan</pre> 81*d4726bddSHONG Yifan 82*d4726bddSHONG YifanThe tools required for the `rust_bindgen` rule. 83*d4726bddSHONG Yifan 84*d4726bddSHONG YifanThis rule depends on the [`bindgen`](https://crates.io/crates/bindgen) binary crate, and it 85*d4726bddSHONG Yifanin turn depends on both a clang binary and the clang library. To obtain these dependencies, 86*d4726bddSHONG Yifan`rust_bindgen_dependencies` imports bindgen and its dependencies. 87*d4726bddSHONG Yifan 88*d4726bddSHONG Yifan```python 89*d4726bddSHONG Yifanload("@rules_rust//bindgen:defs.bzl", "rust_bindgen_toolchain") 90*d4726bddSHONG Yifan 91*d4726bddSHONG Yifanrust_bindgen_toolchain( 92*d4726bddSHONG Yifan name = "bindgen_toolchain_impl", 93*d4726bddSHONG Yifan bindgen = "//my/rust:bindgen", 94*d4726bddSHONG Yifan clang = "//my/clang:clang", 95*d4726bddSHONG Yifan libclang = "//my/clang:libclang.so", 96*d4726bddSHONG Yifan libstdcxx = "//my/cpp:libstdc++", 97*d4726bddSHONG Yifan) 98*d4726bddSHONG Yifan 99*d4726bddSHONG Yifantoolchain( 100*d4726bddSHONG Yifan name = "bindgen_toolchain", 101*d4726bddSHONG Yifan toolchain = "bindgen_toolchain_impl", 102*d4726bddSHONG Yifan toolchain_type = "@rules_rust//bindgen:toolchain_type", 103*d4726bddSHONG Yifan) 104*d4726bddSHONG Yifan``` 105*d4726bddSHONG Yifan 106*d4726bddSHONG YifanThis toolchain will then need to be registered in the current `WORKSPACE`. 107*d4726bddSHONG YifanFor additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html). 108*d4726bddSHONG Yifan 109*d4726bddSHONG Yifan**ATTRIBUTES** 110*d4726bddSHONG Yifan 111*d4726bddSHONG Yifan 112*d4726bddSHONG Yifan| Name | Description | Type | Mandatory | Default | 113*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- | 114*d4726bddSHONG Yifan| <a id="rust_bindgen_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | 115*d4726bddSHONG Yifan| <a id="rust_bindgen_toolchain-bindgen"></a>bindgen | The label of a `bindgen` executable. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` | 116*d4726bddSHONG Yifan| <a id="rust_bindgen_toolchain-clang"></a>clang | The label of a `clang` executable. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` | 117*d4726bddSHONG Yifan| <a id="rust_bindgen_toolchain-default_rustfmt"></a>default_rustfmt | If set, `rust_bindgen` targets will always format generated sources with `rustfmt`. | Boolean | optional | `True` | 118*d4726bddSHONG Yifan| <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 | `None` | 119*d4726bddSHONG Yifan| <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 | `None` | 120*d4726bddSHONG Yifan 121*d4726bddSHONG Yifan 122*d4726bddSHONG Yifan<a id="rust_bindgen_dependencies"></a> 123*d4726bddSHONG Yifan 124*d4726bddSHONG Yifan## rust_bindgen_dependencies 125*d4726bddSHONG Yifan 126*d4726bddSHONG Yifan<pre> 127*d4726bddSHONG Yifanrust_bindgen_dependencies() 128*d4726bddSHONG Yifan</pre> 129*d4726bddSHONG Yifan 130*d4726bddSHONG YifanDeclare dependencies needed for bindgen. 131*d4726bddSHONG Yifan 132*d4726bddSHONG Yifan 133*d4726bddSHONG Yifan**RETURNS** 134*d4726bddSHONG Yifan 135*d4726bddSHONG Yifanlist[struct(repo=str, is_dev_dep=bool)]: A list of the repositories 136*d4726bddSHONG Yifan defined by this macro. 137*d4726bddSHONG Yifan 138*d4726bddSHONG Yifan 139*d4726bddSHONG Yifan<a id="rust_bindgen_library"></a> 140*d4726bddSHONG Yifan 141*d4726bddSHONG Yifan## rust_bindgen_library 142*d4726bddSHONG Yifan 143*d4726bddSHONG Yifan<pre> 144*d4726bddSHONG Yifanrust_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>, 145*d4726bddSHONG Yifan <a href="#rust_bindgen_library-wrap_static_fns">wrap_static_fns</a>, <a href="#rust_bindgen_library-kwargs">kwargs</a>) 146*d4726bddSHONG Yifan</pre> 147*d4726bddSHONG Yifan 148*d4726bddSHONG YifanGenerates a rust source file for `header`, and builds a rust_library. 149*d4726bddSHONG Yifan 150*d4726bddSHONG YifanArguments are the same as `rust_bindgen`, and `kwargs` are passed directly to rust_library. 151*d4726bddSHONG Yifan 152*d4726bddSHONG Yifan 153*d4726bddSHONG Yifan**PARAMETERS** 154*d4726bddSHONG Yifan 155*d4726bddSHONG Yifan 156*d4726bddSHONG Yifan| Name | Description | Default Value | 157*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | 158*d4726bddSHONG Yifan| <a id="rust_bindgen_library-name"></a>name | A unique name for this target. | none | 159*d4726bddSHONG Yifan| <a id="rust_bindgen_library-header"></a>header | The label of the .h file to generate bindings for. | none | 160*d4726bddSHONG Yifan| <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 | 161*d4726bddSHONG Yifan| <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*d4726bddSHONG Yifan| <a id="rust_bindgen_library-bindgen_features"></a>bindgen_features | The `features` attribute for the `rust_bindgen` target. | `None` | 163*d4726bddSHONG Yifan| <a id="rust_bindgen_library-clang_flags"></a>clang_flags | Flags to pass directly to the clang executable. | `None` | 164*d4726bddSHONG Yifan| <a id="rust_bindgen_library-wrap_static_fns"></a>wrap_static_fns | Whether to create a separate .c file for static fns. Requires nightly toolchain, and a header that actually needs this feature (otherwise bindgen won't generate the file and Bazel complains", | `False` | 165*d4726bddSHONG Yifan| <a id="rust_bindgen_library-kwargs"></a>kwargs | Arguments to forward to the underlying `rust_library` rule. | none | 166*d4726bddSHONG Yifan 167*d4726bddSHONG Yifan 168*d4726bddSHONG Yifan<a id="rust_bindgen_register_toolchains"></a> 169*d4726bddSHONG Yifan 170*d4726bddSHONG Yifan## rust_bindgen_register_toolchains 171*d4726bddSHONG Yifan 172*d4726bddSHONG Yifan<pre> 173*d4726bddSHONG Yifanrust_bindgen_register_toolchains(<a href="#rust_bindgen_register_toolchains-register_toolchains">register_toolchains</a>) 174*d4726bddSHONG Yifan</pre> 175*d4726bddSHONG Yifan 176*d4726bddSHONG YifanRegisters the default toolchains for the `rules_rust` [bindgen][bg] rules. 177*d4726bddSHONG Yifan 178*d4726bddSHONG Yifan[bg]: https://rust-lang.github.io/rust-bindgen/ 179*d4726bddSHONG Yifan 180*d4726bddSHONG Yifan 181*d4726bddSHONG Yifan**PARAMETERS** 182*d4726bddSHONG Yifan 183*d4726bddSHONG Yifan 184*d4726bddSHONG Yifan| Name | Description | Default Value | 185*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | 186*d4726bddSHONG Yifan| <a id="rust_bindgen_register_toolchains-register_toolchains"></a>register_toolchains | Whether or not to register toolchains. | `True` | 187*d4726bddSHONG Yifan 188*d4726bddSHONG Yifan 189