From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id D404B158015 for ; Mon, 25 Dec 2023 15:24:02 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 75ED42BC01F; Mon, 25 Dec 2023 15:23:58 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.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 8549C2BC013 for ; Mon, 25 Dec 2023 15:23:57 +0000 (UTC) From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Subject: [gentoo-dev] [PATCH] toolchain-funcs.eclass: Add tc-is-lto function Date: Mon, 25 Dec 2023 16:23:51 +0100 Message-ID: <20231225152351.1866703-1-mgorny@gentoo.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 8852ea86-6f6e-4c54-8079-8debe838d58c X-Archives-Hash: dfc9e34ef2a06863228fa44aa2299211 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 --- 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