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




version set (4)

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.

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 ()

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).


So far as I can tell, there is no command-line option to list which architectures a given clang binary supports, and even running strings on it doesn't really help. Clang is essentially just a C to LLVM translator, and it's LLVM itself that deals with the nitty-gritty of generating actual machine code, so it's not entirely surprising that Clang isn't paying much attention to the underlying architecture.

As others have already noted, you can ask llc which architectures it supports. This isn't all that helpful not just because these LLVM components might not be installed, but because of the vagaries of search paths and packaging systems, your llc and clang binaries may not correspond to the same version of LLVM.

However, for the sake of argument, let's say that you compiled both LLVM and Clang yourself or that you're otherwise happy to accept your LLVM binaries as good enough:

  • llc --version will give a list of all architectures it supports. By default, it is compiled to support all architectures. What you may think of as a single architecture such as ARM may have several LLVM architectures such as regular ARM, Thumb and AArch64. This is mainly for implementation convenience because the different execution modes have very different instruction encodings and semantics.
  • For each of the architectures listed, llc -march=ARCH -mattr=help will list "available CPUs" and "available features". The CPUs are generally just a convenient way of setting a default collection of features.

But now for the bad news. There is no convenient table of triples in Clang or LLVM that can be dumped, because the architecture-specific backends have the option of parsing the triple string into an llvm::Triple object (defined in include/llvm/ADT/Triple.h). In other words, to dump all available triples requires solving the Halting Problem. See, for example, llvm::ARM_MC::ParseARMTriple(...) which special-cases parsing the string "generic".

Ultimately, though, the "triple" is mostly a backwards-compatibility feature to make Clang a drop-in replacement for GCC, so you generally don't need to pay much attention to it unless you are porting Clang or LLVM to a new platform or architecture. Instead, you will probably find the output of llc -march=arm -mattr=help and boggling at the huge array of different ARM features to be more useful in your investigations.

Good luck with your research!





c++ c cmake clang