-
Notifications
You must be signed in to change notification settings - Fork 435
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
proc-macro crates should build for the host instead of the target #205
Comments
A pull request with your workaround could be useful to others. @damienmg do you know if we would be able to dynamically set |
One thing we should keep in mind when fixing this is that some crates in the dependency graph might need to be compiled for the target as well as the host if they are both a direct dependency as well as a transitive dependency from a procedural macro. While this would be easy to solve with a duplicate I will be happy to whip up a PR for this issue once consensus on the right approach has been reached. |
@mfarrugi pulling in @katre and @gregestren who understand how multi-platform configuration works in Bazel. The main thing is that downstream @wildarch: how do that works when it needs to be compiled for both? Why would transitive dependencies needs the procedural macro crate? |
@damienmg the proc-macro could have dependencies, which then need to be compiled for host, not the other way around. |
What I meant is a case like the following:
Then we need to build crate B for both the host and the target. |
This is fine, if C is compiled for the execution platform, then their dependencies too. The only thing is that C should be compiled as a host deps. I don't think this is feasible from the starlark rule context thought. We would have to either declare a different dependencies (which would be weird), or simply do some macro magic although I cannot think of a nice one to do at the moment except if we add a crate_name attribute to rust_library. In that case we would have a Let's wait until @gregestren chime in and tell us all the magic about dynamic configuration and if there is one that allow us to do that without that last ugly hack. |
@mfarrugi I'm not familiar with Rust - can you clarify more precisely exactly the condition you want to trigger? i.e. are you asking if you can make a And what's the specific criteria you want to trigger this on? |
A rust_library can be built such that it's a compile-time code generator
(similar to a Java annotation processor), and not something that is
actually linked into the final output. That's what a proc-macro is. The
trigger is a parameter to rust_library, `crate_type = "proc-macro"`.
So proc-macro libraries should always be built for the host, but then it's
dependencies should also be built for the host (and may need to be built
for the target as well).
…On Thu, Mar 14, 2019, 14:10 Greg ***@***.***> wrote:
@mfarrugi <https://github.com/mfarrugi> I'm not familiar with Rust - can
you clarify more precisely exactly the condition you want to trigger?
i.e. are you asking if you can make a rust_library dynamically set
*itself* to cfg = "host" depending on, say, the value of one of it's
attributes? Or if the library's implementation function can dynamically set cfg
= "host" for one of its deps?
And what's the specific criteria you want to trigger this on?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#205 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACLjePld45RS8pWH6a_ZTr3Edrpp234Nks5vWpCjgaJpZM4btrEg>
.
|
We have the concept of "rule class transitions", which basically lets a rule declare itself as:
But we don't , I believe, have support for Or, if Rust rules fully support platforms I think it'd be possible today to define a transition that sets Alternatively, is it really necessary for both target and host deps to be defined on the same attribute? Can you instead have a binary define Finally, having a dep set to host, then have one of its dep set back to target might be difficult. |
It should be possible to use a transition to set the target platform (via the |
Do rust rules already rely on |
They do! Sounds like the way to go 👍 |
So I made a quick proof-of-concept that implements the ability to explicitly build crates for host using a 1-1 transition. The problem is that support for attribute-based transitions on rules doesn't exist yet according to the tracking document, but @gregestren can correct me if it's been finished already? With current Bazel features I think we'd have to do it with a separate rule that we can later phase out into just the attribute, or wait for the rust feature. If we did it with a separate rule we'd have to expose that in cargo-raze as well ¯\(ツ)/¯ |
@m3rcuriel Cool! Is your POC available somewhere? |
Implementation found here. Ideally we would check attrs for crate type and transition based on that but that's the unfinished feature, I suppose. https://gist.github.com/b44df5e57897c1dfdba658108cf35f71 |
Almost certain more needs to be done to have rustc handle it properly. WIP. |
@juliexxia has been incrementally de-experimentalizing transitions. I can't remember exactly which pieces don't fall under experimental now or what if anything is blocking attribute-based transitions.. She can clarify. That said, that's a nice example of mixing platforms and transitions - this is a good example of the direction we want all rules to go. |
What's de-experimentalized (i.e. you don't have to pass the The reason for the last restriction, not reading configured attributes, is an overkill way to make sure that if But long story short, you should be able to check the attrs for crate_type in the transition. But if any instance of rust_proc_macro (or any rule that uses this rule transition) tries to use a select to set |
https://gist.github.com/6cd8ec00a395dd6c63f63844c810fb66 Working example with Thanks @juliexxia for clearing up the status of this (and also for all your work on configuration in general! ^_^) |
Sweet! Looking good. Happy to review any PRs writing transitions if you need going forward! Just to clarify, you should be able to build rules that use the transition without Up until then, you'll need to pass the flag to build/use the targets. |
If this makes procedural macros and all their dependencies build for the host we should be all good! What would be the path to integrating this, as this currently depends on an experimental flag? Even if bazel implements support for this without the feature flag, distributions may be slow to catch up. Perhaps a PR implementing this could also |
I had an idea but it does not work (to define a custom starlark repository that test bazel version and create a bzl file with the @juliexxia: is there's any plan to help rule author write rules that offer conditional feature? I know about starlark flag but those would not work in that case where the rule is executed at the loading of the starlark file. Also since when is the |
@wildarch / @Collaborators I believe this was fixed in #240 / #272. Should we get this issue closed? |
It looks like this would fix the issue, but I don't have anything to verify with. Maybe it would be good to add a test for this to the CI, and then close this issue once we know for sure it works? |
While experimenting with cross compilation using
rules_rust
I noticed something odd: it seems like bazel builds crates marked with theproc-macro
type for the target platform instead of the host platform. This breaks procedural macros when cross compiling, especially when the target platform does not have a standard library.I guess that we would want
rules_rust
to convince bazel to compile procedural macro crates and their dependencies for the host platform instead of the target platform, but I do not know enough about Bazel to determine if and how this would be possible.For now I came up with an ugly but working solution: I added an extra bool field to
rust_library
namedproc_macro_dep
. If this field is set to true, the--target
argument is omitted from rustc invocations. I can provide the patch in case anyone is interested.The text was updated successfully, but these errors were encountered: