commit:     375da3684e685c6fd8367ffcf37e090526d55e4d
Author:     James Le Cuirot <chewi <AT> gentoo <DOT> org>
AuthorDate: Fri May 31 21:49:47 2024 +0000
Commit:     James Le Cuirot <chewi <AT> gentoo <DOT> org>
CommitDate: Wed Jun 12 13:18:01 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=375da368

cargo.eclass: Enable cross-compiling by setting environment variables

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 <chewi <AT> gentoo.org>

 eclass/cargo.eclass | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
index 72c740cda906..40d98211ce7f 100644
--- a/eclass/cargo.eclass
+++ b/eclass/cargo.eclass
@@ -34,7 +34,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}"
 
@@ -525,6 +525,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"

Reply via email to