c++ check - In cmake, how can I test if the compiler is Clang?




version set (4)

Just to avoid any mispelling problem, I am using this:

if (CMAKE_CXX_COMPILER_ID MATCHES "[cC][lL][aA][nN][gG]") #Case insensitive match
    set(IS_CLANG_BUILD true)
else ()
    set(IS_CLANG_BUILD false)
endif ()

For making the regex case insensitive, I tried everything here without success (doesn't seem to be supported in CMake).

We have a set of cross platform CMake build scripts, and we support building with MSVC and GCC.

We're trying out Clang, but I can't figure out how to test whether or not the compiler is Clang with our CMake script.

What should I test to see if the compiler is Clang or not? We're currently using MSVC and CMAKE_COMPILER_IS_GNU<LANG> to test for MSVC and GCC, respectively.


The OGRE 3D engine source code uses the following check:

if (CMAKE_CXX_COMPILER MATCHES ".*clang")
    set(CMAKE_COMPILER_IS_CLANGXX 1)
endif ()

A reliable check is to use the CMAKE_<LANG>_COMPILER_ID variables. E.g., to check the C++ compiler:

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  # using Clang
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  # using GCC
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
  # using Intel C++
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  # using Visual Studio C++
endif()

These also work correctly if a compiler wrapper like ccache is used.

As of CMake 3.0.0 the CMAKE_<LANG>_COMPILER_ID value for Apple-provided Clang is now AppleClang. To test for both the Apple-provided Clang and the regular Clang use the following if condition:

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  # using regular Clang or AppleClang
endif()

Also see the AppleClang policy description.


Use the bitwise operators: & |

To set last bit in 000b:

foo = foo | 001b

To check last bit in foo:

if ( foo & 001b ) ....

To clear last bit in foo:

foo = foo & 110b

I used XXXb for clarity. You'll probably be working with HEX representation, depending on the data structure in which you're packing bits.





c++ c cmake clang