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 3/4] toolchain-funcs.eclass: Add tc-get-compiler-type()
Date: Wed, 22 Jun 2016 20:09:01
Message-Id: 20160622200654.6961-4-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/4] toolchain-funcs.eclass: fix for version checks (v2) + compiler identification 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..116bc43 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 + set -- $($(tc-getCPP "$@") -E -P - <<<"CPP_WORKS __GNUC__ __clang__")
51 +
52 + # CPP_WORKS shouldn't be substituted -- so if it's not there,
53 + # cpp is probably broken
54 + if [[ $1 != CPP_WORKS ]]; then
55 + echo unknown
56 + # Check which of the defines were substituted
57 + elif [[ $3 != __clang__ ]]; then
58 + echo clang
59 + elif [[ $2 != __GNUC__ ]]; then
60 + echo gcc
61 + else
62 + echo unknown
63 + fi
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

Replies