I have the following CMake code:
add_library(iflib INTERFACE)
target_compile_definitions(iflib INTERFACE
GEN_EXP=$<IF:$<STREQUAL:"test","test">,"EQUAL","DIFFERENT">
)
Which sets the GEN_EXP definition to "EQUAL", as expected.
However, when I compare with a variable:
add_library(iflib INTERFACE)
set(TEST_VAR "test")
target_compile_definitions(iflib INTERFACE
GEN_EXP=$<IF:$<STREQUAL:${TEST_VAR},"test">,"EQUAL","DIFFERENT">
)
The GEN_EXP definition becomes "DIFFERENT".
Is it not possible to do this kind of comparison in Generator Expressions?
>Solution :
With the variable evaluated the generator expression looks like this:
target_compile_definitions(iflib INTERFACE
GEN_EXP=$<IF:$<STREQUAL:test,"test">,"EQUAL","DIFFERENT">
)
depending on whether you want to check for quotes in the variable value or not you either need to add the quotes to the content of the variable
set(TEST_VAR "\"test\"")
or change the generator expression removing the quotes around the value to compare to
target_compile_definitions(iflib INTERFACE
GEN_EXP=$<IF:$<STREQUAL:${TEST_VAR},test>,"EQUAL","DIFFERENT">
)