[CMake] Generator expressions containing spaces

Yves Frederix yves.frederix+cmake at gmail.com
Mon Apr 23 07:07:59 EDT 2018


Hi,

I recently learned that the COMMAND line in a custom command supports
generator expressions. In particular, what makes this powerful is that if
the expression evaluates to the empty string, no corresponding code will be
added to the target (as documented in the docs
<https://cmake.org/cmake/help/v3.11/command/add_custom_command.html?highlight=add_custom_command>
).

While this works very nicely for single-string command, I fail to make it
work for commands consisting of multiple space-separated strings:

```
~/tmp/genex_with_spaces$ cat CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

add_custom_target(foo)
add_custom_command(TARGET foo POST_BUILD
  COMMAND $<1:echo bar>
  )

~/tmp/genex_with_spaces$ grep bar build/CMakeFiles/foo.dir/build.make
                                              "\$$<1:echo" bar>
```

As can be seen, the generator expression is not expanded.

My question is now whether I am doing something wrong (is there a correct
way of dealing with spaces in the context of generator expressions?) or
might this be an inherent limitation of generator expression in general?

As a workaround, the only thing that seems to work is to put each of the
space-separated components of the command in (typically identical) genexes:

```
~/tmp/genex_with_spaces$ cat CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

add_custom_target(foo)
add_custom_command(TARGET foo POST_BUILD
  COMMAND $<1:echo> $<1:bar>
  )

~/tmp/genex_with_spaces$ grep bar build/CMakeFiles/foo.dir/build.make
                                          echo bar
```

Of course, while this works, things becomes very unreadable if the genex is
more complex.

Other things that I have tried but failed:

   - escape the space with a backslash -> this quotes the entire expression
   inside the genex.
   - define the command directly as a list inside the genex -> this removes
   spaces between the different components of the command and quotes that
   result.
   - treat the command as a list, perform string operations to wrap each
   element in the desired regex and convert semicolon to spaces -> again
   results in the command being quoted as a whole.


Any advice is highly appreciated.

Thanks,
Yves
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180423/a57c34a1/attachment.html>


More information about the CMake mailing list