Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: toolchain@g.o, "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 1/2] toolchain-funcs.eclass: Add tc-get-compiler-type()
Date: Thu, 23 Jun 2016 09:19:13
Message-Id: 20160623091826.13631-2-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/2] v3: tc-get-compiler-type() & wrappers by "Michał Górny"
1 Add a tc-get-compiler-type() function that can be used to identify
2 the compiler being used, using the preprocessor defines. Alike
3 gcc-*version() routines, it uses CPP (which in turn uses CC).
4
5 The major usage would be applying compiler-specific quirks and limiting
6 gcc version checks to compilers that actually are gcc, since e.g. clang
7 reports gcc version 4.2 -- which would incorrectly cause numerous gcc
8 version checks in ebuilds to fail.
9 ---
10 eclass/tests/toolchain-funcs.sh | 15 +++++++++++++++
11 eclass/toolchain-funcs.eclass | 19 +++++++++++++++++++
12 2 files changed, 34 insertions(+)
13
14 diff --git a/eclass/tests/toolchain-funcs.sh b/eclass/tests/toolchain-funcs.sh
15 index 41c1ae5..0b9b7d7 100755
16 --- a/eclass/tests/toolchain-funcs.sh
17 +++ b/eclass/tests/toolchain-funcs.sh
18 @@ -111,5 +111,20 @@ tc-ld-disable-gold
19 )
20 tend $?
21
22 +tbegin "tc-get-compiler-type (gcc)"
23 +(
24 +export CC=gcc
25 +[[ $(tc-get-compiler-type) == gcc ]]
26 +)
27 +tend $?
28 +
29 +if type -P clang &>/dev/null; then
30 + tbegin "tc-get-compiler-type (clang)"
31 + (
32 + export CC=clang
33 + [[ $(tc-get-compiler-type) == clang ]]
34 + )
35 + tend $?
36 +fi
37
38 texit
39 diff --git a/eclass/toolchain-funcs.eclass b/eclass/toolchain-funcs.eclass
40 index 3398775..4a45f78 100644
41 --- a/eclass/toolchain-funcs.eclass
42 +++ b/eclass/toolchain-funcs.eclass
43 @@ -585,6 +585,25 @@ tc-endian() {
44 esac
45 }
46
47 +# @FUNCTION: tc-get-compiler-type
48 +# @RETURN: keyword identifying the compiler: gcc, clang, unknown
49 +tc-get-compiler-type() {
50 + local code='
51 +#if defined(__clang__)
52 + HAVE_CLANG
53 +#elif defined(__GNUC__)
54 + HAVE_GCC
55 +#endif
56 +'
57 + local res=$($(tc-getCPP "$@") -E -P - <<<"${code}")
58 +
59 + case ${res} in
60 + *HAVE_CLANG*) echo clang;;
61 + *HAVE_GCC*) echo gcc;;
62 + *) echo unknown;;
63 + esac
64 +}
65 +
66 # Internal func. The first argument is the version info to expand.
67 # Query the preprocessor to improve compatibility across different
68 # compilers rather than maintaining a --version flag matrix. #335943
69 --
70 2.9.0