Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 1/3] toolchain-funcs.eclass: Add tc-get-cxx-stdlib() to get C++ stdlib
Date: Sat, 08 Oct 2022 09:43:54
Message-Id: 20221008094329.316318-2-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/3] toolchain-funcs.eclass: support for querying C++ stdlib and compiler runtime by "Michał Górny"
1 Add a new tc-get-cxx-stdlib() that attempts to get the C++ stdlib
2 variant used by the current C++ compiler. Currently it supports libc++
3 and libstdc++ (GCC's stdlib).
4
5 Signed-off-by: Michał Górny <mgorny@g.o>
6 ---
7 eclass/tests/toolchain-funcs.sh | 22 ++++++++++++++++++++
8 eclass/toolchain-funcs.eclass | 36 +++++++++++++++++++++++++++++++++
9 2 files changed, 58 insertions(+)
10
11 diff --git a/eclass/tests/toolchain-funcs.sh b/eclass/tests/toolchain-funcs.sh
12 index 56379b10cded..5a35a44ce018 100755
13 --- a/eclass/tests/toolchain-funcs.sh
14 +++ b/eclass/tests/toolchain-funcs.sh
15 @@ -198,4 +198,26 @@ for compiler in gcc clang not-really-a-compiler; do
16 fi
17 done
18
19 +if type -P gcc &>/dev/null; then
20 + tbegin "tc-get-cxx-stdlib (gcc)"
21 + [[ $(CXX=g++ tc-get-cxx-stdlib) == libstdc++ ]]
22 + tend $?
23 +fi
24 +
25 +if type -P clang &>/dev/null; then
26 + for stdlib in libc++ libstdc++; do
27 + if clang++ -stdlib=${stdlib} -x c++ -E -P - &>/dev/null \
28 + <<<'#include <ciso646>'
29 + then
30 + tbegin "tc-get-cxx-stdlib (clang, ${stdlib})"
31 + [[ $(CXX=clang++ CXXFLAGS="-stdlib=${stdlib}" tc-get-cxx-stdlib) == ${stdlib} ]]
32 + tend $?
33 + fi
34 + done
35 +
36 + tbegin "tc-get-cxx-stdlib (clang, invalid)"
37 + ! CXX=clang++ CXXFLAGS="-stdlib=invalid" tc-get-cxx-stdlib
38 + tend $?
39 +fi
40 +
41 texit
42 diff --git a/eclass/toolchain-funcs.eclass b/eclass/toolchain-funcs.eclass
43 index 48bf11606c4a..92494158201e 100644
44 --- a/eclass/toolchain-funcs.eclass
45 +++ b/eclass/toolchain-funcs.eclass
46 @@ -1173,4 +1173,40 @@ gen_usr_ldscript() {
47 done
48 }
49
50 +# @FUNCTION: tc-get-cxx-stdlib
51 +# @DESCRIPTION:
52 +# Attempt to identify the C++ standard library used by the compiler.
53 +# If the library is identified, the function returns 0 and prints one
54 +# of the following:
55 +#
56 +# - ``libc++`` for ``sys-libs/libcxx``
57 +# - ``libstdc++`` for ``sys-devel/gcc``'s libstdc++
58 +#
59 +# If the library is not recognized, the function returns 1.
60 +tc-get-cxx-stdlib() {
61 + local code='#include <ciso646>
62 +
63 +#if defined(_LIBCPP_VERSION)
64 + HAVE_LIBCXX
65 +#elif defined(__GLIBCXX__)
66 + HAVE_LIBSTDCPP
67 +#endif
68 +'
69 + local res=$(
70 + $(tc-getCXX) ${CXXFLAGS} ${CPPFLAGS} -x c++ -E -P - \
71 + <<<"${code}" 2>/dev/null
72 + )
73 +
74 + case ${res} in
75 + *HAVE_LIBCXX*)
76 + echo libc++;;
77 + *HAVE_LIBSTDCPP*)
78 + echo libstdc++;;
79 + *)
80 + return 1;;
81 + esac
82 +
83 + return 0
84 +}
85 +
86 fi
87 --
88 2.38.0