public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Cc: "Michał Górny" <mgorny@gentoo.org>
Subject: [gentoo-dev] [PATCH] toolchain-funcs.eclass: Add tc-is-lto function
Date: Mon, 25 Dec 2023 16:23:51 +0100	[thread overview]
Message-ID: <20231225152351.1866703-1-mgorny@gentoo.org> (raw)

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.

Signed-off-by: Michał Górny <mgorny@gentoo.org>
---
 eclass/toolchain-funcs.eclass | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/eclass/toolchain-funcs.eclass b/eclass/toolchain-funcs.eclass
index 5da93063866b..cde84e6f34c8 100644
--- a/eclass/toolchain-funcs.eclass
+++ b/eclass/toolchain-funcs.eclass
@@ -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
-- 
2.43.0



                 reply	other threads:[~2023-12-25 15:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231225152351.1866703-1-mgorny@gentoo.org \
    --to=mgorny@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox