CARGO_BUILD_TARGET and CARGO_TARGET_<triple>_LINKER are enough for pure
Rust. The linker otherwise defaults to `cc`. This doesn't respect any
linker specified in LDFLAGS, but this is also true for native builds. We
would need to do something with RUSTFLAGS.

The HOST_* variables are for the cc-rs crate, which is often used to
build non-Rust code. It uses the usual variables (CC, CFLAGS, etc) for
the target.

Signed-off-by: James Le Cuirot <ch...@gentoo.org>
---
 eclass/cargo.eclass | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index ef55e0613e61c..9390d488f8327 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -38,7 +38,7 @@ case ${EAPI} in
                ;;
 esac
 
-inherit flag-o-matic multiprocessing toolchain-funcs
+inherit flag-o-matic multiprocessing rust-toolchain toolchain-funcs
 
 [[ ! ${CARGO_OPTIONAL} ]] && BDEPEND="${RUST_DEPEND}"
 
@@ -529,6 +529,21 @@ cargo_src_compile() {
        filter-lto
        tc-export AR CC CXX PKG_CONFIG
 
+       if tc-is-cross-compiler; then
+               export CARGO_BUILD_TARGET=$(rust_abi)
+               local TRIPLE=${CARGO_BUILD_TARGET//-/_}
+               export CARGO_TARGET_"${TRIPLE^^}"_LINKER=$(tc-getCC)
+
+               # Set vars for cc-rs crate.
+               tc-export_build_env
+               export \
+                       HOST_AR=$(tc-getBUILD_AR)
+                       HOST_CC=$(tc-getBUILD_CC)
+                       HOST_CXX=$(tc-getBUILD_CXX)
+                       HOST_CFLAGS=${BUILD_CFLAGS}
+                       HOST_CXXFLAGS=${BUILD_CXXFLAGS}
+       fi
+
        set -- cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
        einfo "${@}"
        "${@}" || die "cargo build failed"
-- 
2.45.1


Reply via email to