Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMAKE COMPILER_CXX_ID behaviour

Tags:

c++

cmake

Does

$<COMPILER_CXX_ID:Clang>

will return 1 for Clang and AppleClang

According to the doc I would say yes but I'm not sure...

1 if the CMake-id of the CXX compiler matches comp, otherwise 0

src: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html

src: https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html

like image 862
Mizux Avatar asked Sep 13 '25 11:09

Mizux


1 Answers

TLDR: When using $<CXX_COMPILER_ID:Clang> and having CMP0025 to NEW then AppleClang won't match it.

Matching both

First I suppose the policy CMP0025 is set to NEW to get AppleClang on MacOS with clang provided by Xcode

if (POLICY CMP0025)
  cmake_policy(SET CMP0025 NEW)
endif()

To match both compilers with CMP0025 set to NEW you have two options

Option 1

first create a variable:

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  set(USING_CLANG ON)
else()
  set(USING_CLANG OFF)
endif()

note: notice the use of MATCHES instead of EQUALS

Then you can use it in your generator expression:

target_compile_definition(target PUBLIC
  $<$<BOOL:${USING_CLANG}>:-DUSE_CLANG>)

Option 2

You may also use:

target_compile_definition(target PUBLIC
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-DUSE_CLANG>

Annexe

You can find a working example at https://github.com/Mizux/cmp0025

note: Don't hesitate to look at the Travis-CI log (by clicking on the build badge)

like image 100
Mizux Avatar answered Sep 16 '25 00:09

Mizux