CMake Generator Expression IN_LIST

I have the following CMake setup which is not including the sources as expected. (PLATFORM is set to by a toolchain file, note I’m not using CMAKE_SYSTEM_NAME because one of my platforms (not listed below) isn’t supported by that convention). Note I’m including the multiple variations below since none seem to work for me.

target_sources(myLib
    PRIVATE
        $<IN_LIST:${PLATFORM},"Qnx Linux">:platform-source.cpp>
        $<IN_LIST:${PLATFORM},"Qnx;Linux">:platform-source.cpp>
        # oddly enough this works for target_sources but not for target_link_libraries
        $<IN_LIST:${PLATFORM},Qnx;Linux>:platform-source.cpp>
)

What am I missing here?

$<IN_LIST:...> is a boolean genex, so it returns 0or1. You have to encapsulate this genex in the logical operator $<BOOL:string>. The $< is missing at the beginning of the expression:

target_sources(myLib
    PRIVATE
        $<$<IN_LIST:${PLATFORM},"Qnx Linux">:platform-source.cpp>
)

Sorry that was a typo in my post :frowning:
I do have

target_sources(myLib
    PRIVATE
        $<$<IN_LIST:${PLATFORM},"Qnx Linux">:platform-source.cpp>
        $<$<IN_LIST:${PLATFORM},"Qnx;Linux">:platform-source.cpp>
        # oddly enough this works for target_sources but not for target_link_libraries
        $<$<IN_LIST:${PLATFORM},Qnx;Linux>:platform-source.cpp>
)

Which still doesn’t work as expected.

A basic rule for genex is to encapsulate a genex in quotes.

From your example, the first item is wrong because "Qnx Linux" is not a list. Nor the second because it is encapsulated in quotes.

The third syntax is OK except that, because the genex is not in quotes, it will be expanded during target_sources evaluation and your arguments will be divided in two items: $<$<IN_LIST:${PLATFORM},Qnx and Linux>:platform-source.cpp>

The correct syntax is:

target_sources(myLib
    PRIVATE
        "$<$<IN_LIST:${PLATFORM},Qnx;Linux>:platform-source.cpp>"
)

That worked, thank you!

It would be helpful if that was mentioned in the documentation (https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html). Their examples with target_compile_definitions do not put the generator expression in quotes.