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 1/3] toolchain-funcs.eclass: Add tc-get-cxx-stdlib() to get C++ stdlib
Date: Sat,  8 Oct 2022 11:43:27 +0200	[thread overview]
Message-ID: <20221008094329.316318-2-mgorny@gentoo.org> (raw)
In-Reply-To: <20221008094329.316318-1-mgorny@gentoo.org>

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 <mgorny@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 <ciso646>'
+		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 <ciso646>
+
+#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
-- 
2.38.0



  reply	other threads:[~2022-10-08  9:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-08  9:43 [gentoo-dev] [PATCH 0/3] toolchain-funcs.eclass: support for querying C++ stdlib and compiler runtime Michał Górny
2022-10-08  9:43 ` Michał Górny [this message]
2022-10-08  9:43 ` [gentoo-dev] [PATCH 2/3] toolchain-funcs.eclass: Add tc-get-c-rtlib() to get CC runtime Michał Górny
2022-10-08  9:43 ` [gentoo-dev] [PATCH 3/3] eclass/tests/toolchain-funcs.sh: Handle missing ld.gold gracefully Michał Górny
2022-10-09 21:30 ` [gentoo-dev] [PATCH 0/3] toolchain-funcs.eclass: support for querying C++ stdlib and compiler runtime Sam James

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=20221008094329.316318-2-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