• [gentoo-dev] [PATCH] flag-o-matic.eclass: implement append-atomic-flags

    From Sam James@21:1/5 to All on Tue Jul 19 23:30:01 2022
    From: matoro <matoro@users.noreply.github.com>

    My take on implementing bug 820101 as conservatively as possible. This
    will append -latomic only when absolutely necessary (even though it's
    probably not required to be that conservative due to our use of
    --as-needed).

    This will take flags into account when testing as well. For example, if
    you compile with -march=i386, this would require linking libatomic even
    if the same toolchain with a higher -march level would not require it.
    So rebuilding the same package with the same flags may change whether
    -latomic is linked at build time if the CFLAGS change. Another instance
    might be switching from GCC to clang - the former requires explicitly
    linking -latomic, while the latter does not even HAVE libatomic (see bug 820095) as all atomic intrinsics are built-in internally. This function
    would safely detect this and not append -latomic.

    There is an optional parameter [bytes]. You can use this if you want to
    be specific about what size atomic support is required. For example,
    there are several platforms like MIPS where the 32-bit version has 1-,
    2-, and 4-byte atomics builtin but requires libatomic linkage for 8-byte atomics. If your program only requires, say, 4-byte atomics, you can use append-atomic-flags 4 and this will then not attempt to link libatomic
    on 32-bit MIPS.

    I tested using this to solve bug 688574 on 32-bit SPARC.

    Closes: https://bugs.gentoo.org/820101
    Signed-off-by: matoro <matoro@users.noreply.github.com>
    Closes: https://github.com/gentoo/gentoo/pull/26334
    Signed-off-by: Sam James <sam@gentoo.org>
    ---
    eclass/flag-o-matic.eclass | 145 +++++++++++++++++++++++++++++++++++++
    1 file changed, 145 insertions(+)

    diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass
    index 0dd2c1191273..503f23a141a1 100644
    --- a/eclass/flag-o-matic.eclass
    +++ b/eclass/flag-o-matic.eclass
    @@ -875,4 +875,149 @@ no-as-needed() {
    esac
    }

    +# @FUNCTION: _test-compile-PROG
    +# @USAGE: <language> <code>
    +# @INTERNAL
    +# @DESCRIPTION:
    +# Attempts to compile (and possibly link) the given program. The first
    +# <language> parameter corresponds to the standard -x compiler argument.
    +# If the program should additionally be attempted to be linked, the string
    +# "+ld" should be added to the <language> parameter.
    +_test-compile-PROG() {
    + local lang=$1
    + local code=$2
    + shift 2
    +
    + [[ -z "${lang}" ]] && return 1
    + [[ -z "${code}" ]] && return 1
    +
    + local compiler filename_in filename_out args=() libs=()
    + case "${lang}" in
    + c)
    + compiler="$(tc-getCC)"
    + filename_in="${T}/test.c"
    + file