LDFLAGS are not currently honoured by Cargo builds at all. It would be particularly advantageous to honour -fuse-ld because alternative linkers
like mold are known to be significantly faster at handling Rust.
As things stand, the eclass sets the linker to CC when cross-compiling,
but it does so erroneously due to a shell quoting issue. If CC includes arguments, an error occurs when setting the CARGO_TARGET_*_LINKER
variable. Even with the right quoting, Cargo still fails because this
variable is not allowed to include arguments. They have to be specified
via RUSTFLAGS instead.
The build host linker is frequently invoked due to the use of build
scripts by some crates. We would therefore also like to configure the
build host linker properly when cross-compiling, but unfortunately there
is currently no way to do this without using a Rust nightly feature.
The build host linker is now configured in the cargo_gen_config function because this configuration is static. Conversely, the target host linker
is configured with environment variables in the cargo_env function
because the target may change.
Some ebuilds already set RUSTFLAGS, so some consideration was given to
how to handle these. When set, Cargo prioritises RUSTFLAGS and similar
over specific configuration for the build and target hosts, so these
must be unset. We can still include their contents in that configuration
for convenience though. CARGO_BUILD_RUSTFLAGS can be used by ebuilds to
set flags only for the build host.
It should not be necessary for ebuilds to figure out which Rust ABI is applicable in order to set flags only for the target host, so the helper
reads from a simple CARGO_TARGET_RUSTFLAGS variable without the triple
for convenience.
Signed-off-by: James Le Cuirot <
chewi@gentoo.org>
---
eclass/cargo.eclass | 65 +++++++++++++++++++++++++++++----------------
1 file changed, 42 insertions(+), 23 deletions(-)
diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index d598af8825adf..6ea37ec5e4e4b 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -109,7 +109,7 @@ ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
#
# If you enable CARGO_OPTIONAL, you have to set BDEPEND on virtual/rust
# for your package and call at least cargo_gen_config manually before using
-# other src_functions of this eclass.
+# other src_functions or cargo_env of this eclass.
# Note that cargo_gen_config is automatically called by cargo_src_unpack.
# @ECLASS_VARIABLE: myfeatures
@@ -259,6 +259,19 @@ cargo_crate_uris() {
cargo_gen_config() {
debug-print-function ${FUNCNAME} "$@"
+ # The default linker is "cc" so override by setting linker to CC in the + # RUSTFLAGS. The given linker cannot include any arguments, so split these
+ # into link-args along with LDFLAGS. Also include extern