# Generator expression with potentially empty list

**URL:** <https://discourse.cmake.org/t/generator-expression-with-potentially-empty-list/6254>\
**Category:** Code\
**Created:** [August 12, 2022, 5:21pm UTC](https://discourse.cmake.org/t/generator-expression-with-potentially-empty-list/6254 "2022-08-12T17:21:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfeuffer](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jpfeuffer/32/1529_2.png) [@jpfeuffer](https://discourse.cmake.org/u/jpfeuffer)\
**Post date:** [August 12, 2022, 5:21pm UTC](https://discourse.cmake.org/t/generator-expression-with-potentially-empty-list/6254/1 "2022-08-12T17:21:59Z")

</div>

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](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?

```auto
    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.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [August 12, 2022, 10:37pm UTC](https://discourse.cmake.org/t/generator-expression-with-potentially-empty-list/6254/2 "2022-08-12T22:37:47Z")

</div>

Generator expressions with spaces must be quoted (see [this article](https://crascit.com/2022/01/25/quoting-in-cmake/) 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:

```cmake
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
)

```

---

<div class="post-metadata">

**Author:** ![jpfeuffer](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jpfeuffer/32/1529_2.png) [@jpfeuffer](https://discourse.cmake.org/u/jpfeuffer)\
**Post date:** [August 15, 2022, 10:37am UTC](https://discourse.cmake.org/t/generator-expression-with-potentially-empty-list/6254/3 "2022-08-15T10:37:34Z")

</div>

Thank you @craig.scott !

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