[CMake] Generator expressions containing spaces

CHEVRIER, Marc marc.chevrier at sap.com
Mon Apr 23 08:11:25 EDT 2018


The space is used to separate arguments passed to COMMAND. So your generator expression is splitted before evaluation and elements are no longer valid generator expression.

So, to solve your problem, encapsulate the generator expression inside quotes. And apply the following advices for correct result:

  *   Separate command from args
  *   Use variable to list your arguments and add option COMMAND_EXPAND_LISTS (available with version 3.8) to avoid “spurious” semi-colons in the result

Your example reworked:

Set (args foo bar)
add_custom_target(foo)
add_custom_command(TARGET foo POST_BUILD
  COMMAND $<1:echo> "$<1 :${args}>"
  COMMAND_EXPAND_LISTS
  )


From: CMake <cmake-bounces at cmake.org> on behalf of Yves Frederix <yves.frederix+cmake at gmail.com>
Date: Monday 23 April 2018 at 13:08
To: "cmake at cmake.org" <cmake at cmake.org>
Subject: [CMake] Generator expressions containing spaces

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/6f150f95/attachment-0001.html>


More information about the CMake mailing list