toolchain-funcs.eclass: Add tc-is-lto function

Add a function to check whether the C compiler is using LTO.
To determine this, we compile a dummy source unit.  In the GCC case,
we check whether the resulting object file contains ".gnu.lto*"
sections.  In the clang case, we check whether a valid LLVM bytecode
file was output rather than a regular object.

The goal of this change is to reduce the amount of USE=lto abuse,
and have a consistent cross-package way of enabling LTO via setting
appropriate CFLAGS and CXXFLAGS.

Closes: https://github.com/gentoo/gentoo/pull/34470
Signed-off-by: Michał Górny <mgorny@gentoo.org>
This commit is contained in:
Michał Górny
2023-12-25 15:45:59 +01:00
parent 962257c916
commit 2aea6c3ff2

View File

@@ -1230,4 +1230,25 @@ tc-get-build-ptr-size() {
die "Could not determine CBUILD pointer size"
}
# @FUNCTION: tc-is-lto
# @RETURN: Shell true if we are using LTO, shell false otherwise
tc-is-lto() {
local f="${T}/test-lto.o"
case $(tc-get-compiler-type) in
clang)
$(tc-getCC) ${CFLAGS} -c -o "${f}" -x c - <<<"" || die
# If LTO is used, clang will output bytecode and llvm-bcanalyzer
# will run successfully. Otherwise, it will output plain object
# file and llvm-bcanalyzer will exit with error.
llvm-bcanalyzer "${f}" &>/dev/null && return 0
;;
gcc)
$(tc-getCC) ${CFLAGS} -c -o "${f}" -x c - <<<"" || die
[[ $($(tc-getREADELF) -S "${f}") == *.gnu.lto* ]] && return 0
;;
esac
return 1
}
fi