-
Notifications
You must be signed in to change notification settings - Fork 489
bindgen: don't register_toolchains
in Bzlmod
#3231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bindgen: don't register_toolchains
in Bzlmod
#3231
Conversation
…, `--@rules_rust_bindgen//:libclang`
Thanks! Outside of bzlmod the expectation was that consumers could bring their own clang by defining their own toolchains and not registering the default ones via: rules_rust/extensions/bindgen/repositories.bzl Lines 91 to 100 in d256c7b
I think that would be a better approach than to have build flags users need to put in their |
Yes that's what we did as well so far in another repo that doesn't use the Bzlmod bindgen yet. I'm not a toolchains registering expert but https://bazel.build/external/migration#register-toolchains:~:text=You%20cannot%20call%20native.register_toolchains%20in%20a%20module%20extension. sounds like it is not possible to do this outside of MODULE.bazel |
Friendly bump on this @UebelAndre :) |
I think what you can do is set the toolchain registration as register_toolchains(
"@rules_rust_bindgen//toolchain",
) Or in your case register_toolchains(
"//my/bindgen/toolchain",
) |
Interesting idea! I tried this out and it works.
sgtm |
Do you wanna put up a pull request for this? 😅 |
--@rules_rust_bindgen//:clang
, --@rules_rust_bindgen//:libclang
register_toolchains
in Bzlmod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks! For the example though you'll wanna make sure it's running in CI by adding it to .bazelci/presubmit.yml
. Then I think this is good to go!
libclang = ":libclang", | ||
) | ||
|
||
build_test( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you wanna actually have a use of the toolchain in this example through toolchain resolution. Why not copy the simple test here instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using bindgen_test_suite
here isn't straightforward because its deps are dev_dependencies of @rules_rust_bindgen
. I used a symlink to reuse the integration test instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a bindgen_test_suite
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed my commits but I've run into an interesting problem: Somehow the toolchain depends on itself. Maybe you have an idea? ❯ cd examples/bindgen_toolchain
❯ bazel test //...
ERROR: Misconfigured toolchains: //:my_bindgen_toolchain is declared as a toolchain but has inappropriate dependencies. Declared toolchains should be created with the 'toolchain' rule and should not have dependencies that themselves require toolchains.
.-> //:libclang
| toolchain types @@bazel_tools//tools/cpp:toolchain_type
| toolchain type @@bazel_tools//tools/cpp:toolchain_type
| RegisteredToolchains
| //:my_bindgen_toolchain
`-- //:libclang
Target //:clang up-to-date:
bazel-bin/clang.exe
ERROR: Analysis of target '//test/integration/simple:simple' failed; build aborted
INFO: Elapsed time: 0.227s, Critical Path: 0.00s
INFO: 1 process: 5 action cache hit, 1 internal.
ERROR: Build did NOT complete successfully
ERROR: No test targets were found, yet testing was requested |
rust_bindgen_toolchain( | ||
name = "my_bindgen_toolchain", | ||
bindgen = "@rules_rust_bindgen//3rdparty:bindgen", | ||
clang = ":clang", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the native_binary
for this attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@llvm_toolchain_llvm//:bin/clang
is a filegroup, not an executable Label, so it needs to be "converted" with a genrule with executable=True, or a native_binary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would genuinely just duplicate the code (which is not a lot). If I end up adding more tests I don't want them to appear so an example. We have no bindgen example currently so this would effectively become that and should show general use of the bindgen rules but with a custom toolchain (which is super useful info in an example).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, done
llvm.toolchain(llvm_version = "17.0.6") | ||
use_repo(llvm, "llvm_toolchain_llvm") | ||
|
||
register_toolchains("//:my_bindgen_toolchain") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is caused because you haven't registered a toolchain
target but instead a rust_bindgen_toolchain
target. You're missing that intermediate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I think is the solution to #3231 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great point! I missed that one somehow. Now it works 👍
examples/bindgen_toolchain/Readme.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can this be named README.md
? That's the pattern we've used all over the repo.
friendly bump @UebelAndre |
I'm currently in the process of getting rules_ros2 onto Bzlmod. One challenge is that its rust support requires rules_rust_bindgen, which depends on llvm-project, which takes more than GitHub Action's maximum configurable job timeout (10h) to build. See mvukov/rules_ros2#238 (comment)
This PR adds new "bring your own clang" build flags:
--@rules_rust_bindgen//:clang
and--@rules_rust_bindgen//:libclang
. As the added test shows, you can point these at toolchains_llvm's pre-compiled clang.The fancy !!set and << merge key yaml magic is documented in https://github.com/bazelbuild/continuous-integration/tree/master?tab=readme-ov-file#sharing-configuration-between-tasks
Let me know what you think about this. I'm also curious what's the rationale for depending on llvm-project instead of toolchains_llvm in the first place. Maybe instead of making this configurable (and thus adding complexity) rules_rust_bindgen could just be switched over to toolchains_llvm completely?