-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
There is no way to set RUSTFLAGS for build scripts (or proc macros) when cross-compiling #4423
Comments
This is currently intentional as it is the conservative choice of how to interpret |
So the fact that whether Actually the code says this is just a hack:
It seems that what we really want is two environment variables; one applied to plugins/build scripts and one applied to the "actual" code. (And maybe a third applied to both.) However, that's currently not easy to do because the function setting the flags has no idea whether this is for the host or target. Correct? That's what this comment sounds like. Judging from trouble that xargo has wrt. plugins, I assume the problem is that when I just run One "easy" fix for this problem would be to double-down on the existing hack, and change The alternative I guess is to fix cargo such that |
Browsing through the code, I have found other code using |
er just to be clear, none of this behavior is a bug today, it's all intentional. RUSTFLAGS is intended to only apply to the "final artifact" which when cross compiling doesn't account for build scripts and such. We left ourselves room to grow other environment variables, though, so we can certainly add some more! |
But that's not what happens. If I run just
|
Well anyway I submitted #4428 which tries to not touch this existing logic, but is just enough to fix my problem. Some other day I'll try to distill my trouble with this logic into a separate bugreport. ;) |
Why does it account for build scripts and plugins when not cross compiling? Cross-compiling to the host and compiling to the host are the same thing, therefore I think that I find the current behavior of
We can keep EDIT: an alternative is to allow finer grained rustflags |
@gnzlbg for historical reasons |
Could something be done about it in the 2018 edition? Probably would need
an RFC, but that’s only worth writing if this can be technically changed
without “too much hassle”.
|
Perhaps! No matter what though it needs to be a smooth breaking change, if any. |
Could someone clarify if the following is impossible given the above decision? I have a dependency which relies on proc_macro2 being compiled with I'd like to run I am open to doing things like: forking proc_macro2 (seems easiest), or manually listing out all of the rustc commands I need to execute (this seems possible, but I tried to execute all of the rustc commands printed when I ran cargo build --verbose, but it did not work. So I am presumably missing something.) Thank you for your help in advance! |
@rbalicki2 hm I think that may be accidental poor planning on our part! Currently if you're cross compiling you'll want to configure |
Thanks! I will try that. |
@Boscop noticed that For example, if you have A workaround is to run |
(proc-macros and their deps, too.)
That avoids the rebuild issue but it seems the rustflags aren't getting applied, so it's not really a working workaround. The resulting exe still links to the crt dynamically when I have this [build]
target = "x86_64-pc-windows-msvc"
[target.'cfg(all(target_arch = "x86_64", not(debug_assertions)))']
rustflags = ["-Ctarget-feature=+crt-static", "-Ctarget-cpu=haswell"] So it has the same effect as no rustflags setting, that's why it "fixes" the rebuild issue. Due to this (#5777) issue it doesn't seem to be possible right now to set different rustflags in release mode, so I switched to setting them in my justfile as env var in my release build recipe for now.. |
The workaround also works for cases where rustflags are applied, e.g. [build]
target = "x86_64-pc-windows-msvc"
[target.'cfg(target_arch = "x86_64")']
rustflags = ["-Ctarget-feature=+crt-static", "-Ctarget-cpu=haswell"] The fact that there's no way to set rustflags for release builds only is a separate issue, as you note, but the workaround prevents rebuilds whether you hit that bug or not. |
Ah yes, originally I wanted to use those rust flags only for release, but I guess it's ok if they are also used for debug, but do they make the build take longer? But for this use case I'd prefer to build my release build into I hope it will soon be possible to use different rustflags for different profiles.. |
For Rust targets, Rust will invoke the linker and add (among others) the -lSystem flag. This works fine for the default linker, but if we use a custom linker (e.g. g++, or even /usr/bin/ld), then we must provide that path to the System library. Adding -L<path> to RUSTFLAGS works fine for regular libraries, but fails for build-scripts (See rust-lang/cargo#4423 ) because there is no way to set RUSTFLAGS for build scripts when cross-compiling. This is mostly an issue because we always specify the target, even when building for host, but that is already sufficient for the RUSTFLAGS to not apply to the build-script-build. Target specific RUSTFLAGS (i.e CARGO_TARGET_<triple>_RUSTFLAGS also don't seem to affect buildscripts. Setting LIBRARY_PATH=<path> applies to normal targets and build-scripts too, and seems to fix the issue. My previous fix probably only fixed the case where a rust library gets linked into a foreign library or executable.
For Rust targets, Rust will invoke the linker and add (among others) the -lSystem flag. This works fine for the default linker, but if we use a custom linker (e.g. g++, or even /usr/bin/ld), then we must provide that path to the System library. Adding -L<path> to RUSTFLAGS works fine for regular libraries, but fails for build-scripts (See rust-lang/cargo#4423 ) because there is no way to set RUSTFLAGS for build scripts when cross-compiling. This is mostly an issue because we always specify the target, even when building for host, but that is already sufficient for the RUSTFLAGS to not apply to the build-script-build. Target specific RUSTFLAGS (i.e CARGO_TARGET_<triple>_RUSTFLAGS also don't seem to affect buildscripts. Setting LIBRARY_PATH=<path> applies to normal targets and build-scripts too, and seems to fix the issue. My previous fix probably only fixed the case where a rust library gets linked into a foreign library or executable.
… compilation of proc macros
… nightly To workaround the following Cargo issue with cross compiling, force enable rustc_version crate when testing Linux corss targets: rust-lang/cargo#4423
The `RUSTFLAGS` were getting applied to build scripts, which caused them to crash with SIGILL. According to this issue, RUSTFLAGS won't be applied to build scripts when cross-compiling by passing the `--target` attribute: rust-lang/cargo#4423 This attempts to work around the problem by explicitly passing: --target x86_64-unknown-linux-gnu
The `RUSTFLAGS` were getting applied to build scripts, which caused them to crash with SIGILL. According to this issue, RUSTFLAGS won't be applied to build scripts when cross-compiling by passing the `--target` attribute: rust-lang/cargo#4423 This attempts to work around the problem by explicitly passing: --target x86_64-unknown-linux-gnu
It's meant to be only applied to the final artifacts, so it's problematic during cross-compile: rust-lang/cargo#4423
We see conflicting information regarding this env var above...
Could some check and confirm what the actual current behavior is here? |
https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#target-applies-to-host seems to confirm that |
The cargo documentation
environment-variables.md
saysThis is wrong, unfortunately. The flag is not passed to all compiler instances: When cross-compiling (i.e., when
--target
is set), the flag is not passed to build scripts. It seems there is currently no way to pass anything to build scripts when--target
is set, which clearly is a feature gap. Cargo should provide a way to set flags for build scripts even when cross-compiling.One may be tempted to set
CARGO_TARGET_<target>_RUSTFLAGS
ortarget.<target>.rustflags
, but that does not work either.Also see #9453, #9452.
The text was updated successfully, but these errors were encountered: