tree: 46df0be7c2bc002d5dba8780e66e2a98961e35b1 [path history] [tgz]
  1. raze/
  2. bindgen.bzl
  3. BUILD
  4. README.md
  5. repositories.bzl
bindgen/README.md

Rust Bindgen Rules

Overview

These rules are for using Bindgen to generate Rust bindings to C (and some C++) libraries.

See the bindgen example for a more complete example of use.

Setup

To use the Rust bindgen rules, add the following to your WORKSPACE file to add the external repositories for the Rust bindgen toolchain (in addition to the rust rules setup):

load("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_repositories")

rust_bindgen_repositories()

This makes the default toolchain defined in @rules_rust available.

It will load crate dependencies of bindgen that are generated using cargo raze inside the rules_rust repository. However, using those dependencies might conflict with other uses of cargo raze. If you need to change those dependencies, please see the dedicated section below.

For additional information, see the Bazel toolchains documentation.

Customizing dependencies

These rules depends on the bindgen binary crate, and it in turn depends on both a clang binary and the clang library. To obtain these dependencies, rust_bindgen_repositories imports bindgen and its dependencies using BUILD files generated with cargo raze, along with a tarball of clang.

If you want to change the bindgen used, or use cargo raze in a more complex scenario (with more dependencies), you must redefine those dependencies.

To do this, once you've imported the needed dependencies (see our Cargo.toml file to see the default dependencies), you need to create your own toolchain. To do so you can create a BUILD file with your rust_bindgen_toolchain definition, for example:

load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")

rust_bindgen_toolchain(
    name = "bindgen-toolchain-impl",
    bindgen = "//my/raze:cargo_bin_bindgen",
    clang = "//my/clang:clang",
    libclang = "//my/clang:libclang.so",
    libstdcxx = "//my/cpp:libstdc++",
)

toolchain(
    name = "bindgen-toolchain",
    toolchain = "bindgen-toolchain-impl",
    toolchain_type = "@rules_rust//bindgen:bindgen_toolchain",
)

Now that you have your own toolchain, you need to register it by inserting the following statement in your WORKSPACE file:

register_toolchains("//my/toolchains:bindgen-toolchain")