Generator expression with potentially empty list

Hi,

I am trying to modify the example of https://cmake.org/cmake/help/git-stage/manual/cmake-generator-expressions.7.html#genex:TARGET_RUNTIME_DLLS

to work with potentially empty lists in a macro but I just cannot get it working. Does anyone have an idea?

    add_custom_command(TARGET ${t} POST_BUILD
            COMMAND $<$<BOOL:$<TARGET_RUNTIME_DLLS:${t}>>:${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:${t}> $<TARGET_FILE_DIR:${t}>>
            COMMAND_EXPAND_LISTS
            )

This is what I tried so far.

Generator expressions with spaces must be quoted (see this article for more on that). However, in this case, you shouldn’t be using spaces. You should be passing a list, and the COMMAND_EXPAND_LISTS will then ensure the arguments are expanded correctly on the command line.

Using intermediate variables to hold the condition and command will make this much clearer:

set(have_runtime_dlls
    $<BOOL:$<TARGET_RUNTIME_DLLS:${t}>>
)
# This stores the command as a list
set(command
    ${CMAKE_COMMAND} -E copy
    $<TARGET_RUNTIME_DLLS:${t}>
    $<TARGET_FILE_DIR:${t}>
)
add_custom_command(TARGET ${t} POST_BUILD
    COMMAND "$<${have_runtime_dlls}:${command}>"
    COMMAND_EXPAND_LISTS
)
2 Likes

Thank you @craig.scott !

I didn’t know I could do that. It looks so much cleaner and works flawlessly!