• [gentoo-dev] [PATCH 2/3] cargo.eclass: Handle LDFLAGS and RUSTFLAGS bet

    From James Le Cuirot@21:1/5 to All on Thu Jul 25 00:10:01 2024
    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.

    We would also like to configure the build host linker properly when cross-compiling, but strangely there is no equivalent linker variable
    for the build host. It can only be set via RUSTFLAGS. For consistency,
    we now use RUSTFLAGS for the target host linker as well.

    Some ebuilds already set RUSTFLAGS, so some consideration was given to
    how to handle these. When set, Cargo prioritises RUSTFLAGS over CARGO_BUILD_RUSTFLAGS and CARGO_TARGET_*_RUSTFLAGS, so we need it unset
    to allow different flags for the build and target hosts. We can still
    include its contents in the latter variables for convenience though.

    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.

    Unfortunately, I have not yet encountered a package that makes use of CARGO_BUILD_RUSTFLAGS while cross-compiling, but as far as I can tell,
    it should work.

    Signed-off-by: James Le Cuirot <chewi@gentoo.org>
    ---
    eclass/cargo.eclass | 47 ++++++++++++++++++++++++++++++++-------------
    1 file changed, 34 insertions(+), 13 deletions(-)

    diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
    index b6d5fe21f0a7b..65eaee6f84e4b 100644
    --- a/eclass/cargo.eclass
    +++ b/eclass/cargo.eclass
    @@ -527,27 +527,48 @@ cargo_src_configure() {
    # @USAGE: Command with its arguments
    # @DESCRIPTION:
    # Run the given command under an environment needed for performing tasks with -# Cargo such as building.
    +# Cargo such as building. RUSTFLAGS is used for both the build and target host.
    +# CARGO_BUILD_RUSTFLAGS and CARGO_TARGET_RUSTFLAGS are used for just the build +# host and target host respectively. Ensure these are set consistently between +# Cargo invocations, otherwise rebuilds will occur.
    cargo_env() {
    filter-lto
    tc-export AR CC CXX PKG_CONFIG

    + # Set vars for cc-rs crate.
    + tc-export_build_env
    + declare -x \
    + HOST_AR=$(tc-getBUILD_AR)
    + HOST_CC=$(tc-getBUILD_CC)
    + HOST_CXX=$(tc-getBUILD_CXX)
    +