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 D770A158095 for ; Mon, 10 Oct 2022 20:52:47 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B8F9AE0901; Mon, 10 Oct 2022 20:52:46 +0000 (UTC) Received: from smtp.gentoo.org (smtp.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 A3169E0901 for ; Mon, 10 Oct 2022 20:52:46 +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 602C8340FB6 for ; Mon, 10 Oct 2022 20:52:45 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 90BE6523 for ; Mon, 10 Oct 2022 20:52:43 +0000 (UTC) From: "Michał Górny" 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" Message-ID: <1665435154.2170307e8e09491f88b0c5f41d42aa096d1b3632.mgorny@gentoo> Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/, eclass/tests/ X-VCS-Repository: repo/gentoo X-VCS-Files: eclass/tests/toolchain-funcs.sh eclass/toolchain-funcs.eclass X-VCS-Directories: eclass/tests/ eclass/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: 2170307e8e09491f88b0c5f41d42aa096d1b3632 X-VCS-Branch: master Date: Mon, 10 Oct 2022 20:52:43 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 46b34c73-07b8-42e9-9fc3-0ec83737d156 X-Archives-Hash: 73162496132158685b43d3c134ba796f commit: 2170307e8e09491f88b0c5f41d42aa096d1b3632 Author: Michał Górny gentoo org> AuthorDate: Fri Oct 7 15:02:28 2022 +0000 Commit: Michał Górny gentoo org> CommitDate: Mon Oct 10 20:52:34 2022 +0000 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=2170307e toolchain-funcs.eclass: Add tc-get-cxx-stdlib() to get C++ stdlib Add a new tc-get-cxx-stdlib() that attempts to get the C++ stdlib variant used by the current C++ compiler. Currently it supports libc++ and libstdc++ (GCC's stdlib). Signed-off-by: Michał Górny gentoo.org> eclass/tests/toolchain-funcs.sh | 22 ++++++++++++++++++++++ eclass/toolchain-funcs.eclass | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/eclass/tests/toolchain-funcs.sh b/eclass/tests/toolchain-funcs.sh index 56379b10cded..5a35a44ce018 100755 --- a/eclass/tests/toolchain-funcs.sh +++ b/eclass/tests/toolchain-funcs.sh @@ -198,4 +198,26 @@ for compiler in gcc clang not-really-a-compiler; do fi done +if type -P gcc &>/dev/null; then + tbegin "tc-get-cxx-stdlib (gcc)" + [[ $(CXX=g++ tc-get-cxx-stdlib) == libstdc++ ]] + tend $? +fi + +if type -P clang &>/dev/null; then + for stdlib in libc++ libstdc++; do + if clang++ -stdlib=${stdlib} -x c++ -E -P - &>/dev/null \ + <<<'#include ' + then + tbegin "tc-get-cxx-stdlib (clang, ${stdlib})" + [[ $(CXX=clang++ CXXFLAGS="-stdlib=${stdlib}" tc-get-cxx-stdlib) == ${stdlib} ]] + tend $? + fi + done + + tbegin "tc-get-cxx-stdlib (clang, invalid)" + ! CXX=clang++ CXXFLAGS="-stdlib=invalid" tc-get-cxx-stdlib + tend $? +fi + texit diff --git a/eclass/toolchain-funcs.eclass b/eclass/toolchain-funcs.eclass index 48bf11606c4a..92494158201e 100644 --- a/eclass/toolchain-funcs.eclass +++ b/eclass/toolchain-funcs.eclass @@ -1173,4 +1173,40 @@ gen_usr_ldscript() { done } +# @FUNCTION: tc-get-cxx-stdlib +# @DESCRIPTION: +# Attempt to identify the C++ standard library used by the compiler. +# If the library is identified, the function returns 0 and prints one +# of the following: +# +# - ``libc++`` for ``sys-libs/libcxx`` +# - ``libstdc++`` for ``sys-devel/gcc``'s libstdc++ +# +# If the library is not recognized, the function returns 1. +tc-get-cxx-stdlib() { + local code='#include + +#if defined(_LIBCPP_VERSION) + HAVE_LIBCXX +#elif defined(__GLIBCXX__) + HAVE_LIBSTDCPP +#endif +' + local res=$( + $(tc-getCXX) ${CXXFLAGS} ${CPPFLAGS} -x c++ -E -P - \ + <<<"${code}" 2>/dev/null + ) + + case ${res} in + *HAVE_LIBCXX*) + echo libc++;; + *HAVE_LIBSTDCPP*) + echo libstdc++;; + *) + return 1;; + esac + + return 0 +} + fi