xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/rust_bindgen.vm (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan#[[
2*d4726bddSHONG Yifan## Overview
3*d4726bddSHONG Yifan
4*d4726bddSHONG YifanThese rules are for using [Bindgen][bindgen] to generate [Rust][rust] bindings to C (and some C++) libraries.
5*d4726bddSHONG Yifan
6*d4726bddSHONG Yifan[rust]: http://www.rust-lang.org/
7*d4726bddSHONG Yifan[bindgen]: https://github.com/rust-lang/rust-bindgen
8*d4726bddSHONG Yifan
9*d4726bddSHONG YifanSee the [bindgen example](https://github.com/bazelbuild/rules_rust/tree/main/examples/bindgen/BUILD.bazel) for a more complete example of use.
10*d4726bddSHONG Yifan
11*d4726bddSHONG Yifan### Setup
12*d4726bddSHONG Yifan
13*d4726bddSHONG YifanTo use the Rust bindgen rules, add the following to your `WORKSPACE` file to add the
14*d4726bddSHONG Yifanexternal repositories for the Rust bindgen toolchain (in addition to the [rust rules setup](https://bazelbuild.github.io/rules_rust/#setup)):
15*d4726bddSHONG Yifan
16*d4726bddSHONG Yifan```python
17*d4726bddSHONG Yifanload("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_dependencies", "rust_bindgen_register_toolchains")
18*d4726bddSHONG Yifan
19*d4726bddSHONG Yifanrust_bindgen_dependencies()
20*d4726bddSHONG Yifan
21*d4726bddSHONG Yifanrust_bindgen_register_toolchains()
22*d4726bddSHONG Yifan
23*d4726bddSHONG Yifanload("@rules_rust//bindgen:transitive_repositories.bzl", "rust_bindgen_transitive_dependencies")
24*d4726bddSHONG Yifan
25*d4726bddSHONG Yifanrust_bindgen_transitive_dependencies()
26*d4726bddSHONG Yifan```
27*d4726bddSHONG Yifan
28*d4726bddSHONG YifanBindgen aims to be as hermetic as possible so will end up building `libclang` from [llvm-project][llvm_proj] from
29*d4726bddSHONG Yifansource. If this is found to be undesirable then no Bindgen related calls should be added to your WORKSPACE and instead
30*d4726bddSHONG Yifanusers should define their own repositories using something akin to [crate_universe][cra_uni] and define their own
31*d4726bddSHONG Yifantoolchains following the instructions for [rust_bindgen_toolchain](#rust_bindgen_toolchain).
32*d4726bddSHONG Yifan
33*d4726bddSHONG Yifan[llvm_proj]: https://github.com/llvm/llvm-project
34*d4726bddSHONG Yifan[cra_uni]: https://bazelbuild.github.io/rules_rust/crate_universe.html
35*d4726bddSHONG Yifan
36*d4726bddSHONG Yifan---
37*d4726bddSHONG Yifan
38*d4726bddSHONG Yifan---
39*d4726bddSHONG Yifan
40*d4726bddSHONG Yifan]]#
41