Using CMake version 3.20.1, I'm doing something that I consider to be relatively simple: verify that the value of cfg
pass via -DCMAKE_BUILD_TYPE=<cfg>
is a valid configuration.
Referencing the CMake generator expressions docs, it appears that $<CONFIG:cfgs>
is a specialized case of $<IN_LIST:string,list>
where the string is ${CMAKE_CONFIGURATION_TYPES}
. The CMake runtime-error I'm getting reinforces this, as well as telling me that I'm missing something.
What I'm expecting to work is this:
set( CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build Configurations" FORCE )
if( NOT $<CONFIG:${CMAKE_CONFIGURATION_TYPES}> )
# Handle_the_bad_value()
endif()
when called from the command line as cmake .. -DCMAKE_BUILD_TYPE=Debug
, but I get the following error:
CMake Error at CMakeLists.txt:45 (if):
if given arguments:
"NOT" "\$<CONFIG:Debug" "Release>"
Unknown arguments specified
Replacing
$<CONFIG:<cfg_list>
with either:
$<IN_LIST:${CMAKE_BUILD_TYPE},${CMAKE_CONFIGURATION_TYPES}>
or
$<IN_LIST:"Debug","Debug;Release">
gives me the following:
CMake Error at CMakeLists.txt:43 (if):
if given arguments:
"NOT" "\$<IN_LIST:Debug,Debug" "Release>"
Unknown arguments specified
I've played around with various changes to the original syntax before experimenting with $<IN_LIST:str,list>
, but being that I cannot get the IN_LIST
syntax to work, I feel there is something fundamental that I am getting wrong that I'm also failing to grasp from the documentation.
Subsequent experimentation, using:
if( NOT ${CMAKE_BUILD_TYPE} IN_LIST ${CMAKE_CONFIGURATION_TYPES} )
yields:
CMake Error at CMakeLists.txt:43 (if):
if given arguments:
"NOT" "Debug" "IN_LIST" "Debug" "Release"
Unknown arguments specified
So I tried brute force:
if( NOT ( "Debug" IN_LIST "Debug;Release" ) )
which evaluates, but incorrectly, because it does not find "Debug" in the list, so it enters the code within the if-statement. Final try:
if( NOT ( CMAKE_BUILD_TYPE IN_LIST CMAKE_CONFIGURATION_TYPES ) )
Does work as expected. Noted: NOT
should apply to an expression that is within parenthesis. But it still doesn't help me understand the correct syntax for the CONFIG
generator expression.