-
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
Automatically detect the appropriate linker to use when cross-compiling #4133
Comments
I've definitely often wanted to do this but Cargo's not really in a position to do so. It's never been quite clear where this logic would go. |
I just spent some time debugging some obscure build failure, before realizing that this was the root cause (a non-native But it looks like other people previously stepped on this, again with musl: rust-lang/rust#40049 (comment). |
I think it is OK to put it in Cargo if we can't identify any better place to put it. The kind of issue it would fix has tripped me up multiple times. Also, people tend to blame ring when things fail to link because often the first linker errors reported are in ring. |
What about adding the logic to rustc itself? Basically, what's already in cc-rs here. Since rustc is the one calling the linker, it could select the right linker when it's being told to cross-compile. |
If this is implemented, could we please default to |
Since Clang uses LLVM does it not have a |
This issue comes into play when cross compiling Cargo projects using scripted cross compile build systems. Most C Makefile or CMake projects seem to rely primarily on $CC and $LD to find the correct compiler and linker to use. This makes it easy to cross compile most projects just by setting those flags. I tried using IMHO, Cargo does seem the correct place for something like this if it's already going to support |
Any new thoughts on this? I am currently specifying the corresponding gcc linker in the |
Since cargo already has --target option it makes a lot of sense to make $(TRIPLET)-cc the default linker, as it's pretty obvious that the default one will not work. |
I think this question is relevant: https://users.rust-lang.org/t/same-arch-cross-compile-linker-mixup/82337 When cross-compiling to the host arch but different libs (CC, LD env vars set appropriately) cargo/rust tries to use the same linker for build_script_build targets as it does for the final target which is wrong. It ignores $(LD) unless but something like --config target.$triple.linker= can be used (no arguments allowed, grr) but that linker is also used for build_script_build binaries which run on the host instead of the target system (and so linking or running may fail). I think that --config target.$triple.linker=... should apply only to the target and the usual linker should apply for build_script_build binaries -- the same as it does for differing-arch cross-compiles. How to detect a cross-compile? I thought it was by specifying --target, but it seems that isn't sufficient. My workaround was a linker wrapper with heuristics to guess which context it was being called in. (I'm using plain cargo without any build script commands of my own) |
In case it helps, this is the linker wrapper that I am using, and then pass it to cargo with:
|
Fix #1011 by avoiding the use of `crate-type = ["cdylib"]` in native builds. It was causing some linker errors when cross-compiling from x64-linux to aarch64-linux, I believe the root cause is rust-lang/cargo#4133 i.e. cargo failing to select the right target linker during cross builds. The `cdylib` crate type is only relevant when building wasms, so this change just lowers that functionality into the specific commands that build wasms. --------- Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com>
In order to cross-compile the Debian cargo package I have to explicitly set
RUSTFLAGS += -C linker=$(DEB_HOST_GNU_TYPE)-gcc
, see here. I originally didn't think too much of this, however rustbuild does not require me to set this sort of thing when cross-compiling rustc, it automatically detects the appropriate linker to use. I wonder if it's possible to add similar detection logic to Cargo?The text was updated successfully, but these errors were encountered: