From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1583778-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id AA308158015
	for <garchives@archives.gentoo.org>; Tue, 26 Dec 2023 14:02:29 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id C7E452BC017;
	Tue, 26 Dec 2023 14:02:28 +0000 (UTC)
Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id B14A92BC017
	for <gentoo-commits@lists.gentoo.org>; Tue, 26 Dec 2023 14:02:28 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id CCB4D335DC3
	for <gentoo-commits@lists.gentoo.org>; Tue, 26 Dec 2023 14:02:27 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 3A3FE9C4
	for <gentoo-commits@lists.gentoo.org>; Tue, 26 Dec 2023 14:02:26 +0000 (UTC)
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" <mgorny@gentoo.org>
Message-ID: <1703599329.2aea6c3ff2181ad96187e456a3307609fd288d4c.mgorny@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
X-VCS-Repository: repo/gentoo
X-VCS-Files: eclass/toolchain-funcs.eclass
X-VCS-Directories: eclass/
X-VCS-Committer: mgorny
X-VCS-Committer-Name: Michał Górny
X-VCS-Revision: 2aea6c3ff2181ad96187e456a3307609fd288d4c
X-VCS-Branch: master
Date: Tue, 26 Dec 2023 14:02:26 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: b75e9c52-ba49-4fd1-ac29-116c9da072fa
X-Archives-Hash: c5ffdb2617de0a21d9c90864b05b55f7

commit:     2aea6c3ff2181ad96187e456a3307609fd288d4c
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Mon Dec 25 14:45:59 2023 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Tue Dec 26 14:02:09 2023 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=2aea6c3f

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 <AT> 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