-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add clarifications to environment-variables.md that will help people not break their builds when targeting specific CPUs #12367
Conversation
…doing optimized builds RUSTFLAGS environment variable is too broad and should be avoided when doing any cross-compiling (or targeting specific CPU model). This is now clarified here.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @weihanglo (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
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.
Thanks for the effort!
I got some concerns around the change:
- What does “production code” mean here? Is it
--release
build or something else? - If I understand correctly, under some circumstances, proc macros and build scripts also get these rustflags.
build.rustflags
should have explained this already. - In
build.rustflags
doc we already have some "Caution". We'd better consolidating rustflags warning in once place, andbuild.rustflags
looks like a better place to me. - Continue. Not only platform-specific flags could break the build. Any contradictory to what Cargo passes to rustc could potential break any build.
Yep but not only an open issue, it is an unstable feature |
Well yes, an unstable feature exists, but using it is tricky since it requires switching to nightly compiler as well (which is not something users necessarily want to do in production). The actual use case for these environment variables "in production" is such that when you are producing a binary, you normally want it to be built for specific CPU on which it will run. I am not talking arm vs x86 here, more like AMD Ryzen 3 vs Intel Cascade Lake. --release will only go as far as enabling some basic optimizations, but not CPU-specific stuff. CPU specific features are critical for vectorization and some other optimizer tricks that LLVM can apply, and not making use of them is very wasteful. To enable CPU-specific optimizations, one needs to set rustc flags such as -C target-cpu=znver3. Setting them using RUSTFLAGS (which is the "natural" thing to try) results in builds failing with illegal instruction errors, since cargo starts producing procmacros using the architecture specified in RUSTFLAGS. In fact, what is needed is CARGO_BUILD_RUSTFLAGS to be set instead, which will affect the target code but not any auxillary stuff like procmacros. Setting RUSTFLAGS directly is thus somewhat misleading. Unless the cargo profiles specific compiler flags are stable, setting CARGO_BUILD_RUSTFLAGS is also the only way to produce actually optimized builds. One other small note - setting target CPU in cargo.toml is very bad since it would need to be changed for each CPU family in the deployment. I currently have 3 CPU families, so having 3 distinct cargo.toml for them would be very silly and prone to bugs. Instead I have a small shell script that runs cargo with different CARGO_BUILD_RUSTFLAGS settings and different CARGO_TARGET_DIR as well. If you feel there is a better place where this sort of information "belongs" please let me know. |
As aforementioned, the doc of cargo/src/doc/src/reference/config.md Lines 479 to 483 in 5a9f611
|
After reading the doc for build.rustflags (especially without reading the doc for the environment variables) a reader would be even more confused. |
Yes. It is a bit confusing. This paragraph explains that Not sure how to write it down well without adding more complexity (more text usually mean more cognitive overhead). From tooling perspective, personally I do want a simpler way to inspect actual rustflags being passed in. ( |
I feel like we were a bit off rails. There were two issues in the discussion:
For the first one, we have a Caution paragraph in For the second one, we also have I am going to close this, and we can discuss any idea in #12087, like something this comment said #12087 (comment). Thank you for the pull request :) |
Fair enough. I suppose one day the proper targets configuration will become a thing and all this silly environment variable business can RIP. |
Added clarification for use of RUSTFLAGS variable (and some minor tweaks around it)
RUSTFLAGS environment variable is too broad and should be avoided when doing any cross-compiling (or targeting specific CPU model). This is now clarified here.
What does this PR try to resolve?
A confusing lack of information regarding the effect of *RUSTFLAGS environment variables. This is a doc change only.
Additional information
In general, the only reason for this issue to be relevant is that Cargo profiles can not set rustflags (there is an open issue for that already). In the meantime, env variables are the way to go.