Skip to content

rs: add rules_rust_bindgen extension#169

Open
bazaah wants to merge 1 commit into
hermeticbuild:mainfrom
bazaah:u/bazaah/bindgen-extension-trampoline
Open

rs: add rules_rust_bindgen extension#169
bazaah wants to merge 1 commit into
hermeticbuild:mainfrom
bazaah:u/bazaah/bindgen-extension-trampoline

Conversation

@bazaah

@bazaah bazaah commented Jul 1, 2026

Copy link
Copy Markdown

Note

This PR depends on the changes in hermeticbuild/rules_rust#31

This is basically a shim around the underlying extension in hermeticbuild/rules_rust, and notably we do not provide a default toolchain for bindgen -- users are expected to provide their own.

This can be easily done if using hermeticbuild/hermetic-llvm, as it provides targets for clang, libclang and libstdcxx.


I provide the toolchain myself via a crate extension for bindgen-cli and then @llvm for the llvm bits:

load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_toolchain")

package(default_visibility = ["//visibility:public"])

toolchain(
    name           = "toolchain",
    toolchain      = "bindgen_toolchain_impl",
    toolchain_type = "@rules_rust_bindgen//:toolchain_type",
)

rust_bindgen_toolchain(
    name      = "bindgen_toolchain_impl",
    bindgen   = ":bindgen",
    clang     = "@llvm-project//clang:clang",
    libclang  = "@llvm-project//clang:libclang",
    libstdcxx = ":libstdcxx",
)

alias(
    name = "bindgen",
    actual = "@bindgen_toolchain_tools//:bindgen-cli__bindgen",
)

cc_import(
    name            = "libstdcxx",
    hdrs            = ["@llvm//runtimes/libstdcxx:headers_include_search_directory"],
    shared_library  = "@llvm//runtimes/libstdcxx:libstdcxx.shared",
    visibility      = ["//visibility:public"],
)
@bindgen_toolchain_tools//:bindgen-cli__bindgen
# bindgen.MODULE.bazel
package = "<label of parent package where this MODULE.bazel is>"

bindgen_toolchain_tools = use_extension("@rules_rs//rs:extensions.bzl", "crate")

bindgen_toolchain_tools.annotation(
    crate = "bindgen-cli",
    gen_binaries = ["bindgen"],
)

bindgen_toolchain_tools.from_cargo(
    name = "bindgen_toolchain_tools",
    cargo_lock = package + ":Cargo.lock",
    cargo_toml = package + ":Cargo.toml",
    platform_triples = ["..."],
)

use_repo(
    bindgen_toolchain_tools,
    "bindgen_toolchain_tools",
)
# Cargo.toml
[package]
name    = "bindgen-tool"
version = "1.0.0"
edition = "2021"

[lib]
crate-type = ["lib"]
path = "empty.rs" # needed to satisfy @rules_rs

[dependencies.bindgen]
version  = "0.72"
features = [
  "runtime",
]

[dependencies.bindgen-cli]
version  = "0.72"
features = [
  "runtime",
]

[dependencies.clang-sys]
git = "https://github.com/KyleMayes/clang-sys"
rev = "a76fd89f1800155e06f99ec5e200a02aadb49e3f"
features = [
  "clang_20_0",
  "runtime",
]

[patch.crates-io]
clang-sys = { git = "https://github.com/KyleMayes/clang-sys", rev = "a76fd89f1800155e06f99ec5e200a02aadb49e3f" }

See https://bazelbuild.slack.com/archives/C05UNC5AANQ/p1782926171232299?thread_ts=1782822956.695739&cid=C05UNC5AANQ for the context of where this is coming from.

This is basically a shim around the underlying extension in
hermeticbuild/rules_rust, and notably we do not provide a default
toolchain for bindgen -- users are expected to provide their own.

This can be easily done if using hermeticbuild/hermetic-llvm, as
it provides targets for clang, libclang and libstdcxx.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant