Files
gentoo/eclass/dot-a.eclass
T
Sam JamesandEli Schwartz f4e964aa20 dot-a.eclass: new eclass for handling LTO in static archives
Introduce a new eclass with utility functions for handling LTO bytecode
(or internal representation, IR) inside static archives (.a files).

Static libraries when built with LTO will contain LTO bytecode which is
not portable across compiler versions or compiler vendors. To avoid pessimising
the library and always filtering LTO, we can build it with -ffat-lto-objects
instead, which builds some components twice. The installed part will then
have the LTO contents stripped out, leaving the regular objects in the
static archive.

It's not feasible to make these work otherwise, as we'd need tracking
for whether a library was built by a specific compiler and its version,
and that compatibility can vary based on other factors (e.g. with gcc,
sys-devel/gcc[zstd] controls if it supports zstd compression for LTO). We
also discourage static libraries anyway.

Provide two functions:

* lto-guarantee-fat

  If LTO is currently enabled (as determined by `tc-is-lto`, added in
  2aea6c3ff2), add `-ffat-lto-objects`
  to CFLAGS and CXXFLAGS if supported.

  This guarantees that produced archives are "fat" (contain both IR
  and regular object files) for later pruning.

* strip-lto-bytecode

  Process a given static archive (.a file) and remove its IR component,
  leaving a regular object.

This approach is also taken by Fedora, openSUSE, and Debian/Ubuntu. An
honourable mention to `lto-rebuild` which fulfilled the same task for many
in the LTO overlay too.

We did consider an alternative approach where we'd relink objects using
the driver in src_install (or some hook afterwards), but this would be
more brittle, as we'd need to extract the right arguments to use (see
e.g. the recent Wireshark issues in fad8ff8a45
for how this can be subtle) and not PM-agnostic given we don't have portable
hooks right now (and even if we did, suspect they wouldn't work in a way
that facilitated this). It's also not clear if such an approach would've
worked for Clang.

The tests have been quite helpful in debugging this and making sure things
work as expected. They  both make sure the eclass does what it ought to,
but also try to capture the expected interaction with the toolchain (which is
why we have the skips depending on tooling & versions) to allow us to test
workarounds and make sure we understand the interactions fully: it made
it easy to test e.g. a patch to Binutils to make strip have plugin/LTO
integration (PR21479).

All of this wasn't worth pursuing until H. J. Lu's patches for Binutils
landed, which they have now in binutils-2.44 [0], which made bfd's handling
of mixed objects much more robust.

[0] https://inbox.sourceware.org/binutils/20250112220244.597636-1-hjl.tools@gmail.com/

Bug: https://bugs.gentoo.org/926120
Thanks-to: Arsen Arsenović <arsen@gentoo.org>
Co-authored-by: Eli Schwartz <eschwartz@gentoo.org>
Signed-off-by: Sam James <sam@gentoo.org>
2025-05-06 09:45:06 +01:00

118 lines
3.2 KiB
Bash

# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: dot-a.eclass
# @MAINTAINER:
# Toolchain Ninjas <toolchain@gentoo.org>
# @AUTHOR:
# Sam James <sam@gentoo.org>
# Eli Schwartz <eschwartz@gentoo.org>
# @SUPPORTED_EAPIS: 8
# @BLURB: Functions to handle stripping LTO bytecode out of static archives.
# @DESCRIPTION:
# This eclass provides functions to strip LTO bytecode out of static archives
# (.a files).
#
# Static libraries when built with LTO will contain LTO bytecode which is
# not portable across compiler versions or compiler vendors. To avoid pessimising
# the library and always filtering LTO, we can build it with -ffat-lto-objects
# instead, which builds some components twice. The installed part will then
# have the LTO contents stripped out, leaving the regular objects in the
# static archive.
#
# Use should be passing calling lto-guarantee-fat before configure-time
# and calling strip-lto-bytecode after installation.
#
# Most packages installing static libraries should be using this eclass,
# though it's not strictly necessary if the package filters LTO.
#
# @EXAMPLE:
# @CODE
#
# inherit dot-a
#
# src_configure() {
# lto-guarantee-fat
# econf
# }
#
# src_install() {
# default
# strip-lto-bytecode
# }
case ${EAPI} in
8) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
if [[ -z ${_DOT_A_ECLASS} ]] ; then
_DOT_A_ECLASS=1
inherit flag-o-matic toolchain-funcs
# TODO: QA check
# @FUNCTION: lto-guarantee-fat
# @DESCRIPTION:
# If LTO is enabled, appends -ffat-lto-objects or any other flags needed
# to provide fat LTO objects.
lto-guarantee-fat() {
tc-is-lto || return
# We add this for all languages as LTO obviously can't be done
# if different compilers are used for e.g. C vs C++ anyway.
append-flags $(test-flags-CC -ffat-lto-objects)
}
# @FUNCTION: strip-lto-bytecode
# @USAGE: [library|directory] [...]
# @DESCRIPTION:
# Strips LTO bytecode from libraries (static archives) passed as arguments.
# Defaults to operating on ${ED} as a whole if no arguments are passed.
#
# As an optimisation, if USE=static-libs exists for a package and is disabled,
# the default-searching behaviour with no arguments is suppressed.
strip-lto-bytecode() {
tc-is-lto || return
local files=()
if [[ ${#} -eq 0 ]]; then
if ! in_iuse static-libs || use static-libs ; then
# maybe we are USE=static-libs. Alternatively, maybe the ebuild doesn't
# offer such a choice. In both cases, the user specified the function,
# so we expect to be called on *something*, but nothing was explicitly
# passed. Try scanning ${ED} automatically.
set -- "${ED}"
fi
fi
mapfile -t -d '' files < <(find -H "${@}" -type f \( -name '*.a' -or -name '*.o' \) -print0)
toolchain_type=
tc-is-gcc && toolchain_type=gnu
tc-is-clang && toolchain_type=llvm
local file
for file in "${files[@]}" ; do
case ${toolchain_type} in
gnu)
$(tc-getSTRIP) \
-R .gnu.lto_* \
-R .gnu.debuglto_* \
-N __gnu_lto_v1 \
"${file}" || die "Stripping bytecode in ${file} failed"
;;
llvm)
llvm-bitcode-strip \
-r "${file}" \
-o "${file}" || die "Stripping bytecode in ${file} failed"
;;
*)
;;
esac
done
}
fi