In CMake, how can I test if the compiler is Clang?

C++CCmakeClang

C++ Problem Overview


We have a set of cross-platform CMake build scripts, and we support building with Visual C++ 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 Visual C++ and GCC, respectively.

C++ Solutions


Solution 1 - C++

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.

CMake 3.15 has added support for both the clang-cl and the regular clang front end. You can determine the front end variant by inspecting the variable CMAKE_<LANG>_COMPILER_FRONTEND_VARIANT:

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
    # using clang with clang-cl front end
  elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
    # using clang with regular front end
  endif()
endif()

Solution 2 - C++

This is a slightly more detailed answer for cmake newbies, modified from sakra's answer. The minimum version of 3.1 seems to be important as it changes the way CMake processes the quoted "MSVC" string (according to policy CMP0054).

cmake_minimum_required(VERSION 3.1)
project(MyProject CXX)

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  MESSAGE("Clang")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  MESSAGE("GNU")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
  MESSAGE("Intel")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  MESSAGE("MSVC")
endif()

Solution 3 - C++

If your cmake_minimum_required VERSION is less than 3.1, then you have to use quoted variable to determine compiler, if together with STREQUAL command, i.e.

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  MESSAGE("MSVC")
endif()

Or, if you don't like quoted stuff, you can use MATCHES command:

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  MESSAGE("MSVC")
endif()

And if you specify cmake_minimum_required VERSION >= 3.1, then you can happily use STREQUAL without quotes:

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  MESSAGE("MSVC")
endif()

the cmake 3.1 version issue, is documented here: https://cmake.org/cmake/help/latest/policy/CMP0054.html

Solution 4 - C++

You can test for Clang and it's frontends like this:

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") # clang-cl
    # ...
  elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU") # clang native
    # ...
  endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") # both
    # ...
endif()

Solution 5 - C++

Just to avoid any misspelling problem, I am using Case-insensitive compare, like:

string( TOLOWER "${CMAKE_CXX_COMPILER_ID}" COMPILER_ID )
if (COMPILER_ID STREQUAL "clang")
	set(IS_CLANG_BUILD true)
else ()
	set(IS_CLANG_BUILD false)
endif ()

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionleedm777View Question on Stackoverflow
Solution 1 - C++sakraView Answer on Stackoverflow
Solution 2 - C++SeareneView Answer on Stackoverflow
Solution 3 - C++ChrisZZView Answer on Stackoverflow
Solution 4 - C++Jens A. KochView Answer on Stackoverflow
Solution 5 - C++AntonioView Answer on Stackoverflow