• [gentoo-dev] [PATCH v4 0/6] cargo.eclass: Handle LDFLAGS and RUSTFLAGS

    From James Le Cuirot@21:1/5 to All on Thu Jul 25 20:10:02 2024
    Sorry for yet another iteration of this. I noticed that the build host flags were not being applied to rustc when cross-compiling. Upon investigation, I found that this isn't actually possible right now. I have reworked it to be close to what we need when it does become possible.

    James Le Cuirot (6):
    cargo.eclass: Use newer Cargo config file name
    cargo.eclass: Add cargo_env helper and use it in compile, test,
    install
    cargo.eclass: Handle LDFLAGS and RUSTFLAGS better
    cargo.eclass: Explicitly tell rustc not to strip binaries
    cargo.eclass: Shadow flag variables so that LTO filtering remains
    local
    distutils-r1.eclass: Use cargo_env when appropriate for flag handling

    eclass/cargo.eclass | 93 ++++++++++++++++++++++++++------------
    eclass/distutils-r1.eclass | 16 ++-----
    2 files changed, 69 insertions(+), 40 deletions(-)

    --
    2.45.2

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Le Cuirot@21:1/5 to All on Thu Jul 25 20:10:02 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.

    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
  • From James Le Cuirot@21:1/5 to All on Thu Jul 25 23:30:02 2024
    This is just like python_get_sitedir, but it returns the stdlib
    directory such as /usr/lib/python3.12. This is useful for locating the sysconfigdata file.

    Signed-off-by: James Le Cuirot <chewi@gentoo.org>
    ---
    eclass/python-utils-r1.eclass | 23 +++++++++++++++++++++++
    1 file changed, 23 insertions(+)

    diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass index c47565fa1db2a..b544f52312880 100644
    --- a/eclass/python-utils-r1.eclass
    +++ b/eclass/python-utils-r1.eclass
    @@ -332,6 +332,17 @@ _python_export() {
    export PYTHON=${BROOT-${EPREFIX}}/usr/bin/${impl}
    debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
    ;;
    + PYTHON_STDLIB)
    + [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
    + PYTHON_STDLIB=$(
    + "${PYTHON}" - "${EPREFIX}/usr" <<-EOF || die
    + import sys, sysconfig
    + print(sysconfig.get_path("stdlib", vars={"installed_base": sys.argv[1]}))
    + EOF
    + )
    + export PYTHON_STDLIB
    + debug-print "${FUNCNAME}: PYTHON_STDLIB = ${PYTHON_STDLIB}"
    + ;;
    PYTHON_SITEDIR)
    [[ -n ${PYTHON} ]] || die "PYTHON needs to be set for ${var} to be exported, or requested before it"
    PY