Skip to content

Commit

Permalink
toolchain-funcs.eclass: Add tc-get-compiler-type()
Browse files Browse the repository at this point in the history
Add a tc-get-compiler-type() function that can be used to identify
the compiler being used, using the preprocessor defines. Alike
gcc-*version() routines, it uses CPP (which in turn uses CC).

The major usage would be applying compiler-specific quirks and limiting
gcc version checks to compilers that actually are gcc, since e.g. clang
reports gcc version 4.2 -- which would incorrectly cause numerous gcc
version checks in ebuilds to fail.
  • Loading branch information
mgorny committed Jun 26, 2016
1 parent c65f608 commit 2fea606
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
40 changes: 40 additions & 0 deletions eclass/tests/toolchain-funcs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,45 @@ tc-ld-disable-gold
)
tend $?

unset CPP

tbegin "tc-get-compiler-type (gcc)"
(
export CC=gcc
[[ $(tc-get-compiler-type) == gcc ]]
)
tend $?

if type -P clang &>/dev/null; then
tbegin "tc-get-compiler-type (clang)"
(
export CC=clang
[[ $(tc-get-compiler-type) == clang ]]
)
tend $?
fi

if type -P pathcc &>/dev/null; then
tbegin "tc-get-compiler-type (pathcc)"
(
export CC=pathcc
[[ $(tc-get-compiler-type) == pathcc ]]
)
tend $?

tbegin "! tc-is-gcc (pathcc)"
(
export CC=pathcc
! tc-is-gcc
)
tend $?

tbegin "! tc-is-clang (pathcc)"
(
export CC=pathcc
! tc-is-clang
)
tend $?
fi

texit
22 changes: 22 additions & 0 deletions eclass/toolchain-funcs.eclass
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,28 @@ tc-endian() {
esac
}

# @FUNCTION: tc-get-compiler-type
# @RETURN: keyword identifying the compiler: gcc, clang, pathcc, unknown
tc-get-compiler-type() {
local code='
#if defined(__PATHSCALE__)
HAVE_PATHCC
#elif defined(__clang__)
HAVE_CLANG
#elif defined(__GNUC__)
HAVE_GCC
#endif
'
local res=$($(tc-getCPP "$@") -E -P - <<<"${code}")

case ${res} in
*HAVE_PATHCC*) echo pathcc;;
*HAVE_CLANG*) echo clang;;
*HAVE_GCC*) echo gcc;;
*) echo unknown;;
esac
}

# Internal func. The first argument is the version info to expand.
# Query the preprocessor to improve compatibility across different
# compilers rather than maintaining a --version flag matrix. #335943
Expand Down

0 comments on commit 2fea606

Please sign in to comment.