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@gentoo.org>
This commit is contained in:
James Le Cuirot
2024-05-31 22:49:47 +01:00
parent e274b7b2eb
commit 375da3684e

View File

@@ -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"