Escaping generator expressions in ExternalProject_Add

As far as I can tell, you’re able to pass generator expressions to CMake through the command-line and have them be evaluated appropriately. This comes in handy when wanting to set CMAKE_MSVC_RUNTIME_LIBRARY for some third-party library while using a multi-config generator like Ninja Multi-Config:

cmake -B ./build -G "Ninja Multi-Config" -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Foo,Bar>:Debug>
cmake --build ./build --config Foo

How do you achieve the same thing with ExternalProject_Add and its CMAKE_ARGS without the generator expression being evaluated in the calling project?

I’ve tried replacing every closing angle bracket with $<ANGLE-R> in hopes that it would somehow defer the evaluation of the generator expression, but without luck. My every attempt at trying to escape the generator expression has resulted in the generator expressions either still being evaluated in the calling project (i.e. not getting escaped) or seemingly just turned into a token soup that CMake can’t understand and it falls back to the default value of MultiThreaded$<$<CONFIG:Debug>:Debug>DLL.

Fighting with ExternalProject on escaping sucks. I’ve done it a lot, but not for genexes. It might be better to write it to a file and then use -C path/to/file as a cache initialization file.

set(CMAKE_MSVC_RUNTIME_LIBRARY "…" CACHE STRING "")

Writing it to an initial cache file instead does seem to do the trick. It’s not ideal, but it’ll have to do. Thank you!