Replies: 7 comments
-
Unfortunately I don't have any experience with platform specific code so I don't have an opinion about this. Could you @mention some crate authors who do it one way and the other way and we can try to sort it out? I would love to include a guideline related to this. |
Beta Was this translation helpful? Give feedback.
-
Sure! I'll ping some people who are owners or top contributors to some platform specific crates. Hope they have some good insights, or can ping people who do, about what works best in practice! When going through the repos now, I realize more or less all of the big ones does compile to an empty crate on the unsupported platforms. But it might still be valuable to get some input on their experience with this.
|
Beta Was this translation helpful? Give feedback.
-
IMHO it's fine for platform-specific crates to fail on the wrong platforms. Forcing consumers not download platform-specific dependencies when building on the wrong platform is a definite benefit. The biggest problem I have with platform-specific crates is that docs.rs doesn't build their documentation correctly. docs.rs always presents the Linux documentation as the default, and doesn't work at all for any but the most common handful of platforms. |
Beta Was this translation helpful? Give feedback.
-
Your example with build scripts is a bad idea because it breaks cross compilation. Build scripts need to be designed in a cross platform manner and be able to run everywhere. It's just that if cargo tells the build script via environment variables that the target is one the crate doesn't support, then the build script can do nothing. Granted, there are situations where this can't be practically done, such as the While things like |
Beta Was this translation helpful? Give feedback.
-
I would personally recommend:
That way users are warned that you should trim your dependency graph wherever possible, but if mistakes are made they end up being harmless in the long run. |
Beta Was this translation helpful? Give feedback.
-
I've never given it a thought (until now), and no one has ever complained about this on my github tracker... First, I think we should avoid Second, while |
Beta Was this translation helpful? Give feedback.
-
Very nice input so far! I can describe the scenario I'm in which made me start thinking about this. I'm using bindgen to generate bindings in What I did to get rid of this problem was to change my So I was naturally wondering if there was a recommended way to do this. |
Beta Was this translation helpful? Give feedback.
-
Are there any recommendations whether or not a crate that only works on one, or a subset of, platforms, should be written in such a way that they still build on the platforms they don't support or fail compilation?
For example,
winapi
builds on non-windows platforms, but it becomes empty.It might be that a crate needs some stuff in
build.rs
that will only work on the platforms it is intended to support. Would it then be fully OK to let it fail compilation on the other platforms? Or is it recommended to put some effort into making it build, but not contain anything usable? For a crate only working on macOS, this is an example of how one could write theirbuild.rs
:Making a crate fail on the wrong platform requires a user of the crate to depend on it just for the specific platforms. Example:
This is a bit more burdensome, but has the benefit of clearly communicating where it's needed and removes the need for downloading and building the crate on the other platforms.
If there can be any consensus whether or not it's recommended to strive towards building an empty crate or fail compilation, then I think it would be awesome to have that recommendation in the API guidelines.
Beta Was this translation helpful? Give feedback.
All reactions